aboutsummaryrefslogtreecommitdiff
path: root/lib/http.ml
diff options
context:
space:
mode:
authorMishio595 <[email protected]>2018-10-14 08:29:52 -0600
committerMishio595 <[email protected]>2018-10-14 08:29:52 -0600
commitc42603da68df3610d063d7b232b53bb45e0183e1 (patch)
tree9666bd4f51e7e6679c6dd5fa6cd97ee29fb3117d /lib/http.ml
downloaddisml-c42603da68df3610d063d7b232b53bb45e0183e1.tar.xz
disml-c42603da68df3610d063d7b232b53bb45e0183e1.zip
Initial commit
Diffstat (limited to 'lib/http.ml')
-rw-r--r--lib/http.ml43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/http.ml b/lib/http.ml
new file mode 100644
index 0000000..ba73fd9
--- /dev/null
+++ b/lib/http.ml
@@ -0,0 +1,43 @@
+open Lwt.Infix
+open Cohttp
+open Cohttp_lwt_unix
+
+module Base = struct
+ exception Invalid_Method
+
+ let base_url = "https://discordapp.com/api/v7"
+ let cdn_url = "https://cdn.discordapp.com"
+
+ let process_url path =
+ Uri.of_string (base_url ^ path)
+
+ let process_request_body body =
+ body
+ |> Yojson.Basic.to_string
+ |> Cohttp_lwt.Body.of_string
+
+ let process_request_headers () =
+ let token = try
+ Sys.getenv "DISCORD_TOKEN"
+ with Not_found -> failwith "Please provide a token" in
+ let h = Header.init_with "User-Agent" "Animus v0.1.0" in
+ let h = Header.add h "Authorization" ("Bot " ^ token) in
+ Header.add h "Content-Type" "application/json"
+
+ (* TODO Finish processor *)
+ let process_response (resp, body) =
+ body |> Cohttp_lwt.Body.to_string >|= Yojson.Basic.from_string
+
+ let request ?(body=`Null) m path =
+ let uri = process_url path in
+ let headers = process_request_headers () in
+ let body = process_request_body body in
+ (match m with
+ | `DELETE -> Client.delete ~headers ~body uri
+ | `GET -> Client.get ~headers uri
+ | `PATCH -> Client.patch ~headers ~body uri
+ | `POST -> Client.post ~headers ~body uri
+ | `PUT -> Client.put ~headers ~body uri
+ | _ -> raise Invalid_Method)
+ >|= process_response
+end \ No newline at end of file