aboutsummaryrefslogtreecommitdiff
path: root/lib/listenbrainz.ml
blob: 5f016b90e3c81406486c80c44075b2bc796ab939 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
open Lwt
open Cohttp_lwt
open Cohttp_lwt_unix

type token = string

let endpoint_uri q =
  let base_uri = "https://api.listenbrainz.org/1/" in
  Uri.of_string (base_uri ^ q)

let auth_headers token = Cohttp.Header.add (Cohttp.Header.init ())
    "Authorization"
    ("Token " ^ token)

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 =
  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)