aboutsummaryrefslogtreecommitdiff
path: root/lib/http.ml
blob: 5f8e4e6393d6c328054616aab520f76a61a36eca (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
open Core
open Async
open Cohttp

module Base = struct
    exception Invalid_Method
    exception Bad_response_headers

    let rl = ref Rl.empty

    let base_url = "https://discordapp.com/api/v7"

    let process_url path =
        Uri.of_string (base_url ^ path)

    let process_request_body body =
        body
        |> Yojson.Safe.to_string
        |> Cohttp_async.Body.of_string

    let process_request_headers () =
        let h = Header.init () in
        Header.add_list h [
            "User-Agent", "Dis.ml v0.1.0";
            "Authorization", ("Bot " ^ !Config.token);
            "Content-Type", "application/json";
        ]

    let process_response path ((resp:Response.t), body) =
        (match Response.headers resp
        |> Rl.rl_of_header with
        | Some r -> Mvar.put (Rl.find_exn !rl path) r
        | None -> raise Bad_response_headers)
        >>= fun () ->
        match resp |> Response.status |> Code.code_of_status with
        | 200 -> body |> Cohttp_async.Body.to_string >>| Yojson.Safe.from_string >>= Deferred.Or_error.return
        | code ->
            body |> Cohttp_async.Body.to_string >>= fun body ->
            Deferred.Or_error.errorf "Unsuccessful response received: %d - %s" code body

    let request ?(body=`Null) ?(query=[]) m path =
        rl := Rl.update ~f:(function
            | None ->
                let r = Mvar.create () in
                Mvar.set r Rl.default;
                r
            | Some r -> r
        ) !rl path;
        let limit = Rl.find_exn !rl path in
        Mvar.take limit >>= fun limit ->
        let process () =
            let uri = Uri.add_query_params' (process_url path) query in
            let headers = process_request_headers () in
            let body = process_request_body body in
            (match m with
            | `DELETE -> Cohttp_async.Client.delete ~headers ~body uri
            | `GET -> Cohttp_async.Client.get ~headers uri
            | `PATCH -> Cohttp_async.Client.patch ~headers ~body uri
            | `POST -> Cohttp_async.Client.post ~headers ~body uri
            | `PUT -> Cohttp_async.Client.put ~headers ~body uri
            | _ -> raise Invalid_Method)
            >>= process_response path
        in if limit.remaining > 0 then process ()
        else Clock.at (Core.Time.(Span.of_int_sec limit.reset |> of_span_since_epoch)) >>= process
end

let get_gateway () =
    Base.request `GET Endpoints.gateway

let get_gateway_bot () =
    Base.request `GET Endpoints.gateway_bot

let get_channel channel_id =
    Base.request `GET (Endpoints.channel channel_id)

let modify_channel channel_id body =
    Base.request ~body `PATCH (Endpoints.channel channel_id)

let delete_channel channel_id =
    Base.request `DELETE (Endpoints.channel channel_id)

let get_messages channel_id limit (kind, id) =
    Base.request ~query:[(kind, string_of_int id); ("limit", string_of_int limit)] `GET (Endpoints.channel_messages channel_id)

let get_message channel_id message_id =
    Base.request `GET (Endpoints.channel_message channel_id message_id)

let create_message channel_id body =
    Base.request ~body:body `POST (Endpoints.channel_messages channel_id)

let create_reaction channel_id message_id emoji =
    Base.request `PUT (Endpoints.channel_reaction_me channel_id message_id emoji)

let delete_own_reaction channel_id message_id emoji =
    Base.request `DELETE (Endpoints.channel_reaction_me channel_id message_id emoji)

let delete_reaction channel_id message_id emoji user_id =
    Base.request `DELETE (Endpoints.channel_reaction channel_id message_id emoji user_id)

let get_reactions channel_id message_id emoji =
    Base.request `GET (Endpoints.channel_reactions_get channel_id message_id emoji)

let delete_reactions channel_id message_id =
    Base.request `DELETE (Endpoints.channel_reactions_delete channel_id message_id)

let edit_message channel_id message_id body =
    Base.request ~body `PATCH (Endpoints.channel_message channel_id message_id)

let delete_message channel_id message_id =
    Base.request `DELETE (Endpoints.channel_message channel_id message_id)

let bulk_delete channel_id body =
    Base.request ~body `POST (Endpoints.channel_bulk_delete channel_id)

let edit_channel_permissions channel_id overwrite_id body =
    Base.request ~body `PUT (Endpoints.channel_permission channel_id overwrite_id)

let get_channel_invites channel_id =
    Base.request `GET (Endpoints.channel_invites channel_id)

let create_channel_invite channel_id body =
    Base.request ~body `POST (Endpoints.channel_invites channel_id)

let delete_channel_permission channel_id overwrite_id =
    Base.request `DELETE (Endpoints.channel_permission channel_id overwrite_id)

let broadcast_typing channel_id =
    Base.request `POST (Endpoints.channel_typing channel_id)

let get_pinned_messages channel_id =
    Base.request `GET (Endpoints.channel_pins channel_id)

let pin_message channel_id message_id =
    Base.request `PUT (Endpoints.channel_pin channel_id message_id)

let unpin_message channel_id message_id =
    Base.request `DELETE (Endpoints.channel_pin channel_id message_id)

let group_recipient_add channel_id user_id =
    Base.request `PUT (Endpoints.group_recipient channel_id user_id)

let group_recipient_remove channel_id user_id =
    Base.request `DELETE (Endpoints.group_recipient channel_id user_id)

let get_emojis guild_id =
    Base.request `GET (Endpoints.guild_emojis guild_id)

let get_emoji guild_id emoji_id =
    Base.request `GET (Endpoints.guild_emoji guild_id emoji_id)

let create_emoji guild_id body =
    Base.request ~body `POST (Endpoints.guild_emojis guild_id)

let edit_emoji guild_id emoji_id body =
    Base.request ~body `PATCH (Endpoints.guild_emoji guild_id emoji_id)

let delete_emoji guild_id emoji_id =
    Base.request `DELETE (Endpoints.guild_emoji guild_id emoji_id)

let create_guild body =
    Base.request ~body `POST Endpoints.guilds

let get_guild guild_id =
    Base.request `GET (Endpoints.guild guild_id)

let edit_guild guild_id body =
    Base.request ~body `PATCH (Endpoints.guild guild_id)

let delete_guild guild_id =
    Base.request `DELETE (Endpoints.guild guild_id)

let get_guild_channels guild_id =
    Base.request `GET (Endpoints.guild_channels guild_id)

let create_guild_channel guild_id body =
    Base.request ~body `POST (Endpoints.guild_channels guild_id)

let modify_guild_channel_positions guild_id body =
    Base.request ~body `PATCH (Endpoints.guild_channels guild_id)

let get_member guild_id user_id =
    Base.request `GET (Endpoints.guild_member guild_id user_id)

let get_members guild_id =
    Base.request `GET (Endpoints.guild_members guild_id)

let add_member guild_id user_id body =
    Base.request ~body `PUT (Endpoints.guild_member guild_id user_id)

let edit_member guild_id user_id body =
    Base.request ~body `PATCH (Endpoints.guild_member guild_id user_id)

let remove_member guild_id user_id body =
    Base.request ~body `DELETE (Endpoints.guild_member guild_id user_id)

let change_nickname guild_id body =
    Base.request ~body `PATCH (Endpoints.guild_me_nick guild_id)

let add_member_role guild_id user_id role_id =
    Base.request `PUT (Endpoints.guild_member_role guild_id user_id role_id)

let remove_member_role guild_id user_id role_id =
    Base.request `DELETE (Endpoints.guild_member_role guild_id user_id role_id)

let get_bans guild_id =
    Base.request `GET (Endpoints.guild_bans guild_id)

let get_ban guild_id user_id =
    Base.request `GET (Endpoints.guild_ban guild_id user_id)

let guild_ban_add guild_id user_id body =
    Base.request ~body `PUT (Endpoints.guild_ban guild_id user_id)

let guild_ban_remove guild_id user_id body =
    Base.request ~body `DELETE (Endpoints.guild_ban guild_id user_id)

let get_roles guild_id =
    Base.request `GET (Endpoints.guild_roles guild_id)

let guild_role_add guild_id body =
    Base.request ~body `POST (Endpoints.guild_roles guild_id)

let guild_roles_edit guild_id body =
    Base.request ~body `PATCH (Endpoints.guild_roles guild_id)

let guild_role_edit guild_id role_id body =
    Base.request ~body `PATCH (Endpoints.guild_role guild_id role_id)

let guild_role_remove guild_id role_id =
    Base.request `DELETE (Endpoints.guild_role guild_id role_id)

let guild_prune_count guild_id days =
    Base.request ~query:[("days", Int.to_string days)] `GET (Endpoints.guild_prune guild_id)

let guild_prune_start guild_id days =
    Base.request ~query:[("days", Int.to_string days)] `POST (Endpoints.guild_prune guild_id)

let get_guild_voice_regions guild_id =
    Base.request `GET (Endpoints.guild_voice_regions guild_id)

let get_guild_invites guild_id =
    Base.request `GET (Endpoints.guild_invites guild_id)

let get_integrations guild_id =
    Base.request `GET (Endpoints.guild_integrations guild_id)

let add_integration guild_id body =
    Base.request ~body `POST (Endpoints.guild_integrations guild_id)

let edit_integration guild_id integration_id body =
    Base.request ~body `POST (Endpoints.guild_integration guild_id integration_id)

let delete_integration guild_id integration_id =
    Base.request `DELETE (Endpoints.guild_integration guild_id integration_id)

let sync_integration guild_id integration_id =
    Base.request `POST (Endpoints.guild_integration_sync guild_id integration_id)

let get_guild_embed guild_id =
    Base.request `GET (Endpoints.guild_embed guild_id)

let edit_guild_embed guild_id body =
    Base.request ~body `PATCH (Endpoints.guild_embed guild_id)

let get_vanity_url guild_id =
    Base.request `GET (Endpoints.guild_vanity_url guild_id)

let get_invite invite_code =
    Base.request `GET (Endpoints.invite invite_code)

let delete_invite invite_code =
    Base.request `DELETE (Endpoints.invite invite_code)

let get_current_user () =
    Base.request `GET Endpoints.me

let edit_current_user body =
    Base.request ~body `PATCH Endpoints.me

let get_guilds () =
    Base.request `GET Endpoints.me_guilds

let leave_guild guild_id =
    Base.request `DELETE (Endpoints.me_guild guild_id)

let get_private_channels () =
    Base.request `GET Endpoints.me_channels

let create_dm body =
    Base.request ~body `POST Endpoints.me_channels

let create_group_dm body =
    Base.request ~body `POST Endpoints.me_channels

let get_connections () =
    Base.request `GET Endpoints.me_connections

let get_user user_id =
    Base.request `GET (Endpoints.user user_id)

let get_voice_regions () =
    Base.request `GET Endpoints.regions

let create_webhook channel_id body =
    Base.request ~body `POST (Endpoints.webhooks_channel channel_id)

let get_channel_webhooks channel_id =
    Base.request `GET (Endpoints.webhooks_channel channel_id)

let get_guild_webhooks guild_id =
    Base.request `GET (Endpoints.webhooks_guild guild_id)

let get_webhook webhook_id =
    Base.request `GET (Endpoints.webhook webhook_id)

let get_webhook_with_token webhook_id token =
    Base.request `GET (Endpoints.webhook_token webhook_id token)

let edit_webhook webhook_id body =
    Base.request ~body `PATCH (Endpoints.webhook webhook_id)

let edit_webhook_with_token webhook_id token body =
    Base.request ~body `PATCH (Endpoints.webhook_token webhook_id token)

let delete_webhook webhook_id =
    Base.request `DELETE (Endpoints.webhook webhook_id)

let delete_webhook_with_token webhook_id token =
    Base.request `DELETE (Endpoints.webhook_token webhook_id token)

let execute_webhook webhook_id token body =
    Base.request ~body `POST (Endpoints.webhook_token webhook_id token)

let execute_slack_webhook webhook_id token body =
    Base.request ~body `POST (Endpoints.webhook_slack webhook_id token)

let execute_git_webhook webhook_id token body =
    Base.request ~body `POST (Endpoints.webhook_git webhook_id token)

let get_audit_logs guild_id body =
    Base.request ~body `GET (Endpoints.guild_audit_logs guild_id)