aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdelyn Breedlove <[email protected]>2019-02-27 07:28:13 -0700
committerAdelyn Breedlove <[email protected]>2019-02-27 07:28:13 -0700
commita72847bc206779252bb7f487b76654de564a6d7e (patch)
tree031273fec2abf64ce7f527d6c4bdce31cbe5111d
parentCorrect User-Agent (diff)
downloaddisml-a72847bc206779252bb7f487b76654de564a6d7e.tar.xz
disml-a72847bc206779252bb7f487b76654de564a6d7e.zip
Some improvements to rate limits
-rw-r--r--lib/http/http.ml217
-rw-r--r--lib/http/http.mli2
-rw-r--r--lib/http/rl.ml31
-rw-r--r--lib/http/rl.mli6
4 files changed, 142 insertions, 114 deletions
diff --git a/lib/http/http.ml b/lib/http/http.ml
index 98abd6c..9feb652 100644
--- a/lib/http/http.ml
+++ b/lib/http/http.ml
@@ -19,11 +19,11 @@ module Base = struct
let process_request_headers () =
let h = Header.init () in
- Header.add_list h [
- "User-Agent", "DiscordBot (https://gitlab.com/Mishio595/disml, v0.2.3)";
- "Authorization", ("Bot " ^ !Client_options.token);
- "Content-Type", "application/json";
- "Connection", "keep-alive";
+ Header.add_list h
+ [ "User-Agent", "DiscordBot (https://gitlab.com/Mishio595/disml, v0.2.3)"
+ ; "Authorization", ("Bot " ^ !Client_options.token)
+ ; "Content-Type", "application/json"
+ ; "Connection", "keep-alive"
]
let process_response path ((resp:Response.t), body) =
@@ -42,26 +42,19 @@ module Base = struct
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
+ let limit, rlm = Rl.get_rl m path !rl in
+ rl := rlm;
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)
+ | `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)
>>= process_response path
in if limit.remaining > 0 then process ()
else
@@ -71,293 +64,293 @@ module Base = struct
end
let get_gateway () =
- Base.request `GET Endpoints.gateway
+ Base.request `Get Endpoints.gateway
let get_gateway_bot () =
- Base.request `GET Endpoints.gateway_bot
+ Base.request `Get Endpoints.gateway_bot
let get_channel channel_id =
- Base.request `GET (Endpoints.channel channel_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
+ Base.request `Get (Endpoints.channel channel_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
let modify_channel channel_id body =
- Base.request ~body `PATCH (Endpoints.channel channel_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
+ Base.request ~body `Patch (Endpoints.channel channel_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
let delete_channel channel_id =
- Base.request `DELETE (Endpoints.channel channel_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
+ Base.request `Delete (Endpoints.channel channel_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
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)
+ Base.request ~query:[(kind, string_of_int id); ("limit", string_of_int limit)] `Get (Endpoints.channel_messages channel_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:Message_t.of_yojson_exn)
let get_message channel_id message_id =
- Base.request `GET (Endpoints.channel_message channel_id message_id) >>| Result.map ~f:Message_t.of_yojson_exn
+ Base.request `Get (Endpoints.channel_message channel_id message_id) >>| Result.map ~f:Message_t.of_yojson_exn
let create_message channel_id body =
- Base.request ~body:body `POST (Endpoints.channel_messages channel_id) >>| Result.map ~f:Message_t.of_yojson_exn
+ Base.request ~body:body `Post (Endpoints.channel_messages channel_id) >>| Result.map ~f:Message_t.of_yojson_exn
let create_reaction channel_id message_id emoji =
- Base.request `PUT (Endpoints.channel_reaction_me channel_id message_id emoji) >>| Result.map ~f:ignore
+ Base.request `Put (Endpoints.channel_reaction_me channel_id message_id emoji) >>| Result.map ~f:ignore
let delete_own_reaction channel_id message_id emoji =
- Base.request `DELETE (Endpoints.channel_reaction_me channel_id message_id emoji) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.channel_reaction_me channel_id message_id emoji) >>| Result.map ~f:ignore
let delete_reaction channel_id message_id emoji user_id =
- Base.request `DELETE (Endpoints.channel_reaction channel_id message_id emoji user_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.channel_reaction channel_id message_id emoji user_id) >>| Result.map ~f:ignore
let get_reactions channel_id message_id emoji =
- Base.request `GET (Endpoints.channel_reactions_get channel_id message_id emoji)
+ Base.request `Get (Endpoints.channel_reactions_get channel_id message_id emoji)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:User_t.of_yojson_exn)
let delete_reactions channel_id message_id =
- Base.request `DELETE (Endpoints.channel_reactions_delete channel_id message_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.channel_reactions_delete channel_id message_id) >>| Result.map ~f:ignore
let edit_message channel_id message_id body =
- Base.request ~body `PATCH (Endpoints.channel_message channel_id message_id) >>| Result.map ~f:Message_t.of_yojson_exn
+ Base.request ~body `Patch (Endpoints.channel_message channel_id message_id) >>| Result.map ~f:Message_t.of_yojson_exn
let delete_message channel_id message_id =
- Base.request `DELETE (Endpoints.channel_message channel_id message_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.channel_message channel_id message_id) >>| Result.map ~f:ignore
let bulk_delete channel_id body =
- Base.request ~body `POST (Endpoints.channel_bulk_delete channel_id) >>| Result.map ~f:ignore
+ Base.request ~body `Post (Endpoints.channel_bulk_delete channel_id) >>| Result.map ~f:ignore
let edit_channel_permissions channel_id overwrite_id body =
- Base.request ~body `PUT (Endpoints.channel_permission channel_id overwrite_id) >>| Result.map ~f:ignore
+ Base.request ~body `Put (Endpoints.channel_permission channel_id overwrite_id) >>| Result.map ~f:ignore
let get_channel_invites channel_id =
- Base.request `GET (Endpoints.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)
+ 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) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.channel_permission channel_id overwrite_id) >>| Result.map ~f:ignore
let broadcast_typing channel_id =
- Base.request `POST (Endpoints.channel_typing channel_id) >>| Result.map ~f:ignore
+ Base.request `Post (Endpoints.channel_typing channel_id) >>| Result.map ~f:ignore
let get_pinned_messages channel_id =
- Base.request `GET (Endpoints.channel_pins channel_id)
+ Base.request `Get (Endpoints.channel_pins channel_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:Message_t.of_yojson_exn)
let pin_message channel_id message_id =
- Base.request `PUT (Endpoints.channel_pin channel_id message_id) >>| Result.map ~f:ignore
+ Base.request `Put (Endpoints.channel_pin channel_id message_id) >>| Result.map ~f:ignore
let unpin_message channel_id message_id =
- Base.request `DELETE (Endpoints.channel_pin channel_id message_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.channel_pin channel_id message_id) >>| Result.map ~f:ignore
let group_recipient_add channel_id user_id =
- Base.request `PUT (Endpoints.group_recipient channel_id user_id) >>| Result.map ~f:ignore
+ Base.request `Put (Endpoints.group_recipient channel_id user_id) >>| Result.map ~f:ignore
let group_recipient_remove channel_id user_id =
- Base.request `DELETE (Endpoints.group_recipient channel_id user_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.group_recipient channel_id user_id) >>| Result.map ~f:ignore
let get_emojis guild_id =
- Base.request `GET (Endpoints.guild_emojis guild_id)
+ Base.request `Get (Endpoints.guild_emojis guild_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:Emoji.of_yojson_exn)
let get_emoji guild_id emoji_id =
- Base.request `GET (Endpoints.guild_emoji guild_id emoji_id) >>| Result.map ~f:Emoji.of_yojson_exn
+ Base.request `Get (Endpoints.guild_emoji guild_id emoji_id) >>| Result.map ~f:Emoji.of_yojson_exn
let create_emoji guild_id body =
- Base.request ~body `POST (Endpoints.guild_emojis guild_id) >>| Result.map ~f:Emoji.of_yojson_exn
+ Base.request ~body `Post (Endpoints.guild_emojis guild_id) >>| Result.map ~f:Emoji.of_yojson_exn
let edit_emoji guild_id emoji_id body =
- Base.request ~body `PATCH (Endpoints.guild_emoji guild_id emoji_id) >>| Result.map ~f:Emoji.of_yojson_exn
+ Base.request ~body `Patch (Endpoints.guild_emoji guild_id emoji_id) >>| Result.map ~f:Emoji.of_yojson_exn
let delete_emoji guild_id emoji_id =
- Base.request `DELETE (Endpoints.guild_emoji guild_id emoji_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.guild_emoji guild_id emoji_id) >>| Result.map ~f:ignore
let create_guild body =
- Base.request ~body `POST Endpoints.guilds >>| Result.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap))
+ Base.request ~body `Post Endpoints.guilds >>| Result.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap))
let get_guild guild_id =
- Base.request `GET (Endpoints.guild guild_id) >>| Result.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap))
+ Base.request `Get (Endpoints.guild guild_id) >>| Result.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap))
let edit_guild guild_id body =
- Base.request ~body `PATCH (Endpoints.guild guild_id) >>| Result.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap))
+ Base.request ~body `Patch (Endpoints.guild guild_id) >>| Result.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap))
let delete_guild guild_id =
- Base.request `DELETE (Endpoints.guild guild_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.guild guild_id) >>| Result.map ~f:ignore
let get_guild_channels guild_id =
- Base.request `GET (Endpoints.guild_channels guild_id)
+ Base.request `Get (Endpoints.guild_channels guild_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:(fun g -> Channel_t.(channel_wrapper_of_yojson_exn g |> wrap)))
let create_guild_channel guild_id body =
- Base.request ~body `POST (Endpoints.guild_channels guild_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
+ Base.request ~body `Post (Endpoints.guild_channels guild_id) >>| Result.map ~f:(fun c -> Channel_t.(channel_wrapper_of_yojson_exn c |> wrap))
let modify_guild_channel_positions guild_id body =
- Base.request ~body `PATCH (Endpoints.guild_channels guild_id) >>| Result.map ~f:ignore
+ Base.request ~body `Patch (Endpoints.guild_channels guild_id) >>| Result.map ~f:ignore
let get_member guild_id user_id =
- Base.request `GET (Endpoints.guild_member guild_id user_id) >>| Result.map ~f:(fun m -> Member_t.(member_of_yojson_exn m |> wrap ~guild_id))
+ Base.request `Get (Endpoints.guild_member guild_id user_id) >>| Result.map ~f:(fun m -> Member_t.(member_of_yojson_exn m |> wrap ~guild_id))
let get_members guild_id =
- Base.request `GET (Endpoints.guild_members guild_id)
+ Base.request `Get (Endpoints.guild_members guild_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:(fun m -> Member_t.(member_of_yojson_exn m |> wrap ~guild_id)))
let add_member guild_id user_id body =
- Base.request ~body `PUT (Endpoints.guild_member guild_id user_id)
+ Base.request ~body `Put (Endpoints.guild_member guild_id user_id)
>>| Result.map ~f:(fun m -> Member_t.(member_of_yojson_exn m |> wrap ~guild_id))
let edit_member guild_id user_id body =
- Base.request ~body `PATCH (Endpoints.guild_member guild_id user_id) >>| Result.map ~f:ignore
+ Base.request ~body `Patch (Endpoints.guild_member guild_id user_id) >>| Result.map ~f:ignore
let remove_member guild_id user_id body =
- Base.request ~body `DELETE (Endpoints.guild_member guild_id user_id) >>| Result.map ~f:ignore
+ Base.request ~body `Delete (Endpoints.guild_member guild_id user_id) >>| Result.map ~f:ignore
let change_nickname guild_id body =
- Base.request ~body `PATCH (Endpoints.guild_me_nick guild_id)
+ 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) >>| Result.map ~f:ignore
+ Base.request `Put (Endpoints.guild_member_role guild_id user_id role_id) >>| Result.map ~f:ignore
let remove_member_role guild_id user_id role_id =
- Base.request `DELETE (Endpoints.guild_member_role guild_id user_id role_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.guild_member_role guild_id user_id role_id) >>| Result.map ~f:ignore
let get_bans guild_id =
- Base.request `GET (Endpoints.guild_bans guild_id)
+ Base.request `Get (Endpoints.guild_bans guild_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:Ban_t.of_yojson_exn)
let get_ban guild_id user_id =
- Base.request `GET (Endpoints.guild_ban guild_id user_id) >>| Result.map ~f:Ban_t.of_yojson_exn
+ Base.request `Get (Endpoints.guild_ban guild_id user_id) >>| Result.map ~f:Ban_t.of_yojson_exn
let guild_ban_add guild_id user_id body =
- Base.request ~body `PUT (Endpoints.guild_ban guild_id user_id) >>| Result.map ~f:ignore
+ Base.request ~body `Put (Endpoints.guild_ban guild_id user_id) >>| Result.map ~f:ignore
let guild_ban_remove guild_id user_id body =
- Base.request ~body `DELETE (Endpoints.guild_ban guild_id user_id) >>| Result.map ~f:ignore
+ Base.request ~body `Delete (Endpoints.guild_ban guild_id user_id) >>| Result.map ~f:ignore
let get_roles guild_id =
- Base.request `GET (Endpoints.guild_roles guild_id)
+ Base.request `Get (Endpoints.guild_roles guild_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:(fun r -> Role_t.(role_of_yojson_exn r |> wrap ~guild_id)))
let guild_role_add guild_id body =
- Base.request ~body `POST (Endpoints.guild_roles guild_id) >>| Result.map ~f:(fun r -> Role_t.(role_of_yojson_exn r |> wrap ~guild_id))
+ Base.request ~body `Post (Endpoints.guild_roles guild_id) >>| Result.map ~f:(fun r -> Role_t.(role_of_yojson_exn r |> wrap ~guild_id))
let guild_roles_edit guild_id body =
- Base.request ~body `PATCH (Endpoints.guild_roles guild_id)
+ Base.request ~body `Patch (Endpoints.guild_roles guild_id)
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:(fun r -> Role_t.(role_of_yojson_exn r |> wrap ~guild_id)))
let guild_role_edit guild_id role_id body =
- Base.request ~body `PATCH (Endpoints.guild_role guild_id role_id) >>| Result.map ~f:(fun r -> Role_t.(role_of_yojson_exn r |> wrap ~guild_id))
+ Base.request ~body `Patch (Endpoints.guild_role guild_id role_id) >>| Result.map ~f:(fun r -> Role_t.(role_of_yojson_exn r |> wrap ~guild_id))
let guild_role_remove guild_id role_id =
- Base.request `DELETE (Endpoints.guild_role guild_id role_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.guild_role guild_id role_id) >>| Result.map ~f:ignore
let guild_prune_count guild_id days =
- Base.request ~query:[("days", Int.to_string days)] `GET (Endpoints.guild_prune guild_id)
+ Base.request ~query:[("days", Int.to_string days)] `Get (Endpoints.guild_prune guild_id)
>>| Result.map ~f:(fun c -> Yojson.Safe.Util.(member "pruned" c |> to_int))
let guild_prune_start guild_id days =
- Base.request ~query:[("days", Int.to_string days)] `POST (Endpoints.guild_prune guild_id)
+ Base.request ~query:[("days", Int.to_string days)] `Post (Endpoints.guild_prune guild_id)
>>| Result.map ~f:(fun c -> Yojson.Safe.Util.(member "pruned" c |> to_int))
let get_guild_voice_regions guild_id =
- Base.request `GET (Endpoints.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)
+ Base.request `Get (Endpoints.guild_invites guild_id)
let get_integrations guild_id =
- Base.request `GET (Endpoints.guild_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) >>| Result.map ~f:ignore
+ Base.request ~body `Post (Endpoints.guild_integrations guild_id) >>| Result.map ~f:ignore
let edit_integration guild_id integration_id body =
- Base.request ~body `POST (Endpoints.guild_integration guild_id integration_id) >>| Result.map ~f:ignore
+ Base.request ~body `Post (Endpoints.guild_integration guild_id integration_id) >>| Result.map ~f:ignore
let delete_integration guild_id integration_id =
- Base.request `DELETE (Endpoints.guild_integration guild_id integration_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.guild_integration guild_id integration_id) >>| Result.map ~f:ignore
let sync_integration guild_id integration_id =
- Base.request `POST (Endpoints.guild_integration_sync guild_id integration_id) >>| Result.map ~f:ignore
+ Base.request `Post (Endpoints.guild_integration_sync guild_id integration_id) >>| Result.map ~f:ignore
let get_guild_embed guild_id =
- Base.request `GET (Endpoints.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)
+ Base.request ~body `Patch (Endpoints.guild_embed guild_id)
let get_vanity_url guild_id =
- Base.request `GET (Endpoints.guild_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)
+ Base.request `Get (Endpoints.invite invite_code)
let delete_invite invite_code =
- Base.request `DELETE (Endpoints.invite invite_code)
+ Base.request `Delete (Endpoints.invite invite_code)
let get_current_user () =
- Base.request `GET Endpoints.me >>| Result.map ~f:User_t.of_yojson_exn
+ Base.request `Get Endpoints.me >>| Result.map ~f:User_t.of_yojson_exn
let edit_current_user body =
- Base.request ~body `PATCH Endpoints.me >>| Result.map ~f:User_t.of_yojson_exn
+ Base.request ~body `Patch Endpoints.me >>| Result.map ~f:User_t.of_yojson_exn
let get_guilds () =
- Base.request `GET Endpoints.me_guilds
+ Base.request `Get Endpoints.me_guilds
>>| Result.map ~f:(fun l -> Yojson.Safe.Util.to_list l |> List.map ~f:(fun g -> Guild_t.(pre_of_yojson_exn g |> wrap)))
let leave_guild guild_id =
- Base.request `DELETE (Endpoints.me_guild guild_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.me_guild guild_id) >>| Result.map ~f:ignore
let get_private_channels () =
- Base.request `GET Endpoints.me_channels
+ Base.request `Get Endpoints.me_channels
let create_dm body =
- Base.request ~body `POST Endpoints.me_channels
+ Base.request ~body `Post Endpoints.me_channels
let create_group_dm body =
- Base.request ~body `POST Endpoints.me_channels
+ Base.request ~body `Post Endpoints.me_channels
let get_connections () =
- Base.request `GET Endpoints.me_connections
+ Base.request `Get Endpoints.me_connections
let get_user user_id =
- Base.request `GET (Endpoints.user user_id) >>| Result.map ~f:User_t.of_yojson_exn
+ Base.request `Get (Endpoints.user user_id) >>| Result.map ~f:User_t.of_yojson_exn
let get_voice_regions () =
- Base.request `GET Endpoints.regions
+ Base.request `Get Endpoints.regions
let create_webhook channel_id body =
- Base.request ~body `POST (Endpoints.webhooks_channel channel_id)
+ Base.request ~body `Post (Endpoints.webhooks_channel channel_id)
let get_channel_webhooks channel_id =
- Base.request `GET (Endpoints.webhooks_channel channel_id)
+ Base.request `Get (Endpoints.webhooks_channel channel_id)
let get_guild_webhooks guild_id =
- Base.request `GET (Endpoints.webhooks_guild guild_id)
+ Base.request `Get (Endpoints.webhooks_guild guild_id)
let get_webhook webhook_id =
- Base.request `GET (Endpoints.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)
+ Base.request `Get (Endpoints.webhook_token webhook_id token)
let edit_webhook webhook_id body =
- Base.request ~body `PATCH (Endpoints.webhook webhook_id)
+ 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)
+ Base.request ~body `Patch (Endpoints.webhook_token webhook_id token)
let delete_webhook webhook_id =
- Base.request `DELETE (Endpoints.webhook webhook_id) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.webhook webhook_id) >>| Result.map ~f:ignore
let delete_webhook_with_token webhook_id token =
- Base.request `DELETE (Endpoints.webhook_token webhook_id token) >>| Result.map ~f:ignore
+ Base.request `Delete (Endpoints.webhook_token webhook_id token) >>| Result.map ~f:ignore
let execute_webhook webhook_id token body =
- Base.request ~body `POST (Endpoints.webhook_token webhook_id token)
+ 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)
+ 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)
+ 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)
+ Base.request ~body `Get (Endpoints.guild_audit_logs guild_id)
let get_application_info () =
- Base.request `GET (Endpoints.application_information) \ No newline at end of file
+ Base.request `Get (Endpoints.application_information) \ No newline at end of file
diff --git a/lib/http/http.mli b/lib/http/http.mli
index fe587c7..b043854 100644
--- a/lib/http/http.mli
+++ b/lib/http/http.mli
@@ -17,7 +17,7 @@ module Base : sig
val request :
?body:Yojson.Safe.t ->
?query:(string * string) list ->
- [> `DELETE | `GET | `PATCH | `POST | `PUT ] ->
+ [ `Delete | `Get | `Patch | `Post | `Put ] ->
string ->
Yojson.Safe.t Deferred.Or_error.t
end
diff --git a/lib/http/rl.ml b/lib/http/rl.ml
index 6c61953..b8c57e8 100644
--- a/lib/http/rl.ml
+++ b/lib/http/rl.ml
@@ -9,8 +9,27 @@ type rl = {
reset: int;
} [@@deriving sexp]
+(* TODO improve route getting, use Date header *)
type t = ((rl, read_write) Mvar.t) RouteMap.t
+let r_msg = Tyre.(str "/channel/" *> pos_int <&> str "/messages/" *> pos_int)
+let r_message_delete = Tyre.compile r_msg
+let r_emoji = Tyre.(compile (r_msg <&> str "/reactions/" *> pcre "[\w\d:]+" <* (str "/@me" <|> str "/" *> pos_int)))
+
+let route_of_path meth path =
+ match meth with
+ | `Delete -> begin
+ match Tyre.exec r_message_delete path with
+ | Ok (cid, _) -> Printf.sprintf "/channel/%d/messages" cid
+ | Error _ -> path
+ end
+ | `Put -> begin
+ match Tyre.exec r_emoji path with
+ | Ok ((cid, mid), _) -> Printf.sprintf "channel/%d/messages/%d/reactions" cid mid
+ | Error _ -> path
+ end
+ | _ -> path
+
let rl_of_header h =
let module C = Cohttp.Header in
match C.get h "X-RateLimit-Limit", C.get h "X-RateLimit-Remaining", C.get h "X-RateLimit-Reset" with
@@ -27,4 +46,14 @@ let update = RouteMap.update
let find = RouteMap.find
let find_exn m s = match find m s with
| Some r -> r
- | None -> raise (Not_found_s (String.sexp_of_t s)) \ No newline at end of file
+ | None -> raise (Not_found_s (String.sexp_of_t s))
+
+let get_rl meth path rl =
+ let route = route_of_path meth path in
+ match RouteMap.find rl route with
+ | Some r -> r, rl
+ | None ->
+ let data = Mvar.create () in
+ Mvar.set data default;
+ let rl = RouteMap.add_exn rl ~key:route ~data in
+ data, rl \ No newline at end of file
diff --git a/lib/http/rl.mli b/lib/http/rl.mli
index a0facb3..54bc5ee 100644
--- a/lib/http/rl.mli
+++ b/lib/http/rl.mli
@@ -16,6 +16,12 @@ type rl = {
(** Type representing the specific case of {!RouteMap}. *)
type t = ((rl, read_write) Mvar.t) RouteMap.t
+val get_rl :
+ [ `Get | `Delete | `Post | `Patch | `Put ] ->
+ string ->
+ t ->
+ (rl, read_write) Mvar.t * t
+
(** Converts Cohttp header data into ratelimit information.
@return Some of ratelimit information or None on bad headers
*)