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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
open Lwt
open Cohttp
open Cohttp_lwt_unix
open Utils
module V1 = struct
open Types_and_parser.V1
open Combined_syntax
type token = string
type error = Cohttp_lwt_unix.Response.t * Cohttp_lwt.Body.t
let token_of_string = Fun.id
let get ?(api_url = "https://api.hackmd.io/v1/") url token =
let =
Header.init () |> fun ->
Header.add header "Authorization" @@ "Bearer " ^ token
in
let uri = Uri.of_string (api_url ^ url) in
Client.get ~headers uri >>= fun (resp, body) ->
let code = resp |> Response.status |> Code.code_of_status in
if code != 200 then Lwt.return @@ Error (resp, body)
else
body |> Cohttp_lwt.Body.to_string >|= Yojson.Safe.from_string
>|= Result.ok
let update
(f :
?ctx:Net.ctx ->
?body:Cohttp_lwt.Body.t ->
?chunked:bool ->
?headers:Header.t ->
'a ->
'b) g expected_code ?(api_url = "https://api.hackmd.io/v1/") url body
token =
let =
let add a b h = Header.add h a b in
Header.init ()
|> add "Authorization" @@ "Bearer " ^ token
|> add "Content-Type" "application/json"
in
let uri = Uri.of_string (api_url ^ url) in
let body =
let ( >>| ) a b = Option.map b a in
body >>| Yojson.Safe.to_string >>| Cohttp_lwt.Body.of_string
in
f ?body ~headers uri >>= fun (resp, body) ->
let code = resp |> Response.status |> Code.code_of_status in
if code != expected_code then Lwt.return @@ Error (resp, body)
else body |> Cohttp_lwt.Body.to_string >|= g >|= Result.ok
let post = update Client.post Yojson.Safe.from_string 201
let patch = update Client.patch Fun.id 202
let delete = update Client.delete Fun.id 204
let user ?(api_url = "https://api.hackmd.io/v1/") token =
let++ body = get ~api_url "me" token in
user_of_yojson body
let notes ?(api_url = "https://api.hackmd.io/v1/") token =
let++ body = get ~api_url "notes" token in
body |> Yojson.Safe.Util.to_list |> List.map note_summary_of_yojson
let note ?(api_url = "https://api.hackmd.io/v1/") token note_id =
let url = Format.sprintf "notes/%s" (string_of_note_id note_id) in
let++ body = get ~api_url url token in
note_of_yojson body
let teams ?(api_url = "https://api.hackmd.io/v1/") token =
let++ body = get ~api_url "teams" token in
body |> Yojson.Safe.Util.to_list |> List.map team_of_yojson
let team_notes ?(api_url = "https://api.hackmd.io/v1/") token team_path =
let url = Format.sprintf "teams/%s/notes" (string_of_team_path team_path) in
let++ body = get ~api_url url token in
body |> Yojson.Safe.Util.to_list |> List.map note_of_yojson
let create_note ?(api_url = "https://api.hackmd.io/v1/") token new_note =
let body = Option.map yojson_of_new_note new_note in
let++ answer = post ~api_url "notes" body token in
note_of_yojson answer
let update_note ?(api_url = "https://api.hackmd.io/v1/") token note_id
update_note =
let url = Format.sprintf "notes/%s" (string_of_note_id note_id) in
let body = Option.map yojson_of_update_note update_note in
let++ answer = patch ~api_url url body token in
answer
let delete_note ?(api_url = "https://api.hackmd.io/v1/") token note_id =
let url = Format.sprintf "notes/%s" (string_of_note_id note_id) in
let++ answer = delete ~api_url url None token in
answer
let history ?(api_url = "https://api.hackmd.io/v1/") token =
let++ body = get ~api_url "history" token in
body |> Yojson.Safe.Util.to_list |> List.map note_summary_of_yojson
let create_note_in_team ?(api_url = "https://api.hackmd.io/v1/") token
team_path new_note =
let url = Format.sprintf "teams/%s/notes" (string_of_team_path team_path) in
let body = Option.map yojson_of_new_note new_note in
let++ answer = post ~api_url url body token in
note_of_yojson answer
let update_note_in_team ?(api_url = "https://api.hackmd.io/v1/") token
team_path note_id update_note =
let url =
Format.sprintf "teams/%s/notes/%s"
(string_of_team_path team_path)
(string_of_note_id note_id)
in
let body = Option.map yojson_of_update_note update_note in
let++ answer = patch ~api_url url body token in
answer
let delete_note_in_team ?(api_url = "https://api.hackmd.io/v1/") token
team_path note_id =
let url =
Format.sprintf "teams/%s/notes/%s"
(string_of_team_path team_path)
(string_of_note_id note_id)
in
let++ answer = delete ~api_url url None token in
answer
end