aboutsummaryrefslogblamecommitdiff
path: root/lib/listenbrainz.ml
blob: df04d3129609d46690994b4fa9a2f0dcc37d9f79 (plain) (tree)

























                                                                      
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)