diff options
| author | Adelyn Breedlove <[email protected]> | 2019-02-27 07:28:13 -0700 |
|---|---|---|
| committer | Adelyn Breedlove <[email protected]> | 2019-02-27 07:28:13 -0700 |
| commit | a72847bc206779252bb7f487b76654de564a6d7e (patch) | |
| tree | 031273fec2abf64ce7f527d6c4bdce31cbe5111d /lib/http/rl.ml | |
| parent | Correct User-Agent (diff) | |
| download | disml-a72847bc206779252bb7f487b76654de564a6d7e.tar.xz disml-a72847bc206779252bb7f487b76654de564a6d7e.zip | |
Some improvements to rate limits
Diffstat (limited to 'lib/http/rl.ml')
| -rw-r--r-- | lib/http/rl.ml | 31 |
1 files changed, 30 insertions, 1 deletions
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 |