aboutsummaryrefslogtreecommitdiff
path: root/lib/listenbrainz.ml
diff options
context:
space:
mode:
authorRyan Kavanagh <rak@rak.ac>2021-11-06 20:11:15 -0400
committerRyan Kavanagh <rak@rak.ac>2021-11-06 20:11:15 -0400
commit835241baf271585ca85b2ecd7f1fe4633f8ec1a8 (patch)
tree39f8eef24d9a4c92a5eb554eb211fa3fc43db160 /lib/listenbrainz.ml
parentAdd mli file for listenbrainz_util (diff)
Initial client that can validate tokens and get listens
Diffstat (limited to 'lib/listenbrainz.ml')
-rw-r--r--lib/listenbrainz.ml26
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)