aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/listenbrainz.ml45
-rw-r--r--lib/listenbrainz.mli9
2 files changed, 32 insertions, 22 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)
diff --git a/lib/listenbrainz.mli b/lib/listenbrainz.mli
index e3997bd..6037c08 100644
--- a/lib/listenbrainz.mli
+++ b/lib/listenbrainz.mli
@@ -1,7 +1,8 @@
-type query
+ type token = string
-val do_query : query -> (Cohttp.Response.t * string) Lwt.t
-
-val get_validate_token : unit -> (bool * Listenbrainz_t.get_validate_token) Lwt.t
+val get_validate_token : token -> Listenbrainz_t.get_validate_token Lwt.t
val get_listens : string -> Listenbrainz_t.get_user_listens Lwt.t
+
+val post_submit_listens : token -> Listenbrainz_t.payload_listens ->
+ (Cohttp.Response.t * Listenbrainz_t.post_submit_listens) Lwt.t