aboutsummaryrefslogtreecommitdiff
path: root/source/misskey.d
blob: 823dbe030a89c4842bec6f7570eb796739108c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module misskey;
import requests;
import std.logger;
import std.format;
import std.conv;
import jsonizer;

class MisskeyException : Exception
{

	string path;
	Response response;
	this(
		Response response,
		string path,
		string msg,
		string file = __FILE__, size_t line = __LINE__)
	{
		super("at path " ~ path ~ ": " ~ msg, file, line);
		this.response = response;
		this.path = path;
	}
}

class MisskeyClient
{
	string instanceUrl;
	string token;
	this(string instanceUrl, string token)
	{
		this.instanceUrl = instanceUrl;
		this.token = token;
		assert(token.length != 0);
		assert(instanceUrl.length != 0);
	}

	T request(string method, string path, T, R)(R body)
	{
		static assert(path[0] == '/');
		auto req = Request();
		req.addHeaders([
			"user-agent": "boobbot",
			"authorization": format("Bearer %s", this.token)
		]);
		auto fullPath = format("%s/api%s", this.instanceUrl, path);
		auto reqBody = toJSONString!R(body);
		infof("Executing request to %s with headers %s", fullPath, req.headers);
		Response response = mixin("req." ~ method)(
			fullPath,
			reqBody,
			"application/json"
		);
		if (response.code / 100 != 2)
			throw new MisskeyException(response, path, format("non 200 status code: %s", response
					.code));
		return fromJSONString!T(to!string(response.responseBody));
	}

	CreateNoteResponse createNote(BasicNote note)
	{
		return this.request!("post", "/notes/create", CreateNoteResponse, BasicNote)(note);
	}

}

class BasicNote
{
	mixin JsonizeMe;

	@jsonize(Jsonize.opt)
	{
		string visibility;
		string text;
		string id;
	}

	this()
	{
	}

	this(string visibility, string text)
	{
		this.visibility = visibility;
		this.text = text;
		this.id = "";
	}

}

class CreateNoteResponse
{
	mixin JsonizeMe;

	@jsonize
	BasicNote createdNote;
}

/*
curl https://sk.amy.rip/api/notes/create \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer aaaaaaaaaa' \
  --data '{
  "visibility": "public",
  "visibleUserIds": [
    ""
  ],
  "cw": null,
  "localOnly": false,
  "reactionAcceptance": null,
  "noExtractMentions": false,
  "noExtractHashtags": false,
  "noExtractEmojis": false,
  "replyId": null,
  "renoteId": null,
  "channelId": null,
  "text": null,
  "fileIds": [
    ""
  ],
  "mediaIds": [
    ""
  ],
  "poll": {
    "choices": [
      ""
    ],
    "multiple": true,
    "expiresAt": null,
    "expiredAfter": null
  }
}'
*/