diff options
Diffstat (limited to 'lib/listenbrainz.ml')
-rw-r--r-- | lib/listenbrainz.ml | 26 |
1 files changed, 26 insertions, 0 deletions
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) |