diff options
Diffstat (limited to 'lib/listenbrainz.ml')
-rw-r--r-- | lib/listenbrainz.ml | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/lib/listenbrainz.ml b/lib/listenbrainz.ml index df04d31..5f016b9 100644 --- a/lib/listenbrainz.ml +++ b/lib/listenbrainz.ml @@ -1,26 +1,35 @@ open Lwt +open Cohttp_lwt +open Cohttp_lwt_unix -type query = - | Get_listens of string - | Get_validate_token +type token = string -let query_uri query = +let endpoint_uri q = let base_uri = "https://api.listenbrainz.org/1/" in - let mk_uri q = Uri.of_string (base_uri ^ q) in - match query with - | Get_listens username -> mk_uri ("user/" ^ username ^ "/listens") - | Get_validate_token -> mk_uri "validate-token" + Uri.of_string (base_uri ^ q) -let do_query query : (Cohttp.Response.t * string) Lwt.t = - Cohttp_lwt_unix.Client.get (query_uri query) >>= fun (resp, body) -> - Cohttp_lwt.Body.to_string body >|= fun body -> - (resp, body) +let auth_headers token = Cohttp.Header.add (Cohttp.Header.init ()) + "Authorization" + ("Token " ^ token) -let get_validate_token () = - do_query Get_validate_token >|= fun (_resp, body) -> - let parsed = Listenbrainz_j.get_validate_token_of_string body in - (parsed.valid, parsed) +let get_validate_token token = + let headers = Some (auth_headers token) in + let uri = endpoint_uri "validate-token" in + Client.get ?headers uri >>= fun (_resp, body) -> + Body.to_string body >|= + Listenbrainz_j.get_validate_token_of_string let get_listens username = - do_query (Get_listens username) >|= fun (_resp, body) -> - body |> (Listenbrainz_j.get_user_listens_of_string) + let uri = endpoint_uri ("user/" ^ username ^ "/listens") in + Client.get uri >>= fun (_resp, body) -> + (Body.to_string body) >|= + Listenbrainz_j.get_user_listens_of_string + +let post_submit_listens token listens = + let headers = Some (auth_headers token) in + let uri = endpoint_uri "submit-listens" in + let body = Some (`String (Listenbrainz_j.string_of_payload_listens listens)) in + Client.post ?headers ?body uri >>= fun (resp, body) -> + Body.to_string body >|= + Listenbrainz_j.post_submit_listens_of_string >|= fun parsed -> + (resp, parsed) |