From 835241baf271585ca85b2ecd7f1fe4633f8ec1a8 Mon Sep 17 00:00:00 2001 From: Ryan Kavanagh Date: Sat, 6 Nov 2021 20:11:15 -0400 Subject: Initial client that can validate tokens and get listens --- lib/listenbrainz.ml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/listenbrainz.ml (limited to 'lib/listenbrainz.ml') diff --git a/lib/listenbrainz.ml b/lib/listenbrainz.ml new file mode 100644 index 0000000..df04d31 --- /dev/null +++ b/lib/listenbrainz.ml @@ -0,0 +1,26 @@ +open Lwt + +type query = + | Get_listens of string + | Get_validate_token + +let query_uri query = + 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" + +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 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_listens username = + do_query (Get_listens username) >|= fun (_resp, body) -> + body |> (Listenbrainz_j.get_user_listens_of_string) -- cgit v1.2.3