Source file ezRequest_lwt.ml
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
open EzAPI
module type S = EzReq_lwt_S.S
module type Interface = EzReq_lwt_S.Interface
let (>|=) = Lwt.(>|=)
let return = Lwt.return
type 'a api_error = 'a EzReq_lwt_S.api_error =
| KnownError of { code : int ; error : 'a }
| UnknownError of { code : int ; msg : string option }
type ('output, 'error) api_result = ('output, 'error) EzReq_lwt_S.api_result
let handle_error kn = function
| KnownError {code; error} -> code, kn error
| UnknownError {code; msg} -> code, msg
let string_of_error kn = function
| KnownError {code; error} ->
let content = match kn error with None -> "" | Some s -> ": " ^ s in
Printf.sprintf "Error %d%s" code content
| UnknownError {code; msg} ->
let content = match msg with None -> "" | Some s -> ": " ^ s in
Printf.sprintf "Unknown Error %d%s" code content
let request_reply_hook = ref (fun () -> ())
let before_hook = ref (fun () -> ())
let decode_result io err_encodings = function
| Error (code, None) -> Error (UnknownError { code ; msg = None })
| Error (code, Some msg) ->
(match err_encodings ~code with
| None -> Error (UnknownError { code ; msg = Some msg })
| Some encoding ->
try Error (
KnownError { code ; error = EzEncoding.destruct encoding msg })
with _ -> Error (UnknownError { code ; msg = Some msg })
)
| Ok res ->
match IO.from_string io (fun x -> x) res with
| Ok s -> Ok s
| Error e -> Error (UnknownError {
code = -3;
msg = Some (EzEncoding.error_to_string ~from:res e) })
let handle_result service res =
let err_encodings = Service.error service.s in
let encoding = Service.output service.s in
decode_result encoding err_encodings res
let any_get = ref (fun ?meth:_m ?headers:_ ?msg:_ _url ->
return (Error (-2, Some "No http client loaded"))
)
let any_post = ref (fun ?meth:_m ?content_type:_ ?content:_ ?headers:_ ?msg:_ _url ->
return (Error (-2, Some "No http client loaded"))
)
module Make(S : Interface) : S = struct
let init () =
any_get := S.get;
any_post := S.post
let internal_get ?meth ? ?msg (URL url) =
EzAPI.warnings (fun s -> Printf.kprintf EzDebug.log "EzRequest.warning: %s" s);
let meth = match meth with None -> None | Some m -> Some (
String.uppercase_ascii @@ Meth.to_string m) in
S.get ?meth ?headers ?msg url >|= fun code ->
!request_reply_hook ();
code
let internal_post ?meth ?content_type ?content ? ?msg (URL url) =
EzAPI.warnings (fun s -> Printf.kprintf EzDebug.log "EzRequest.warning: %s" s);
let meth = match meth with None -> None | Some m -> Some (
String.uppercase_ascii @@ Meth.to_string m) in
S.post ?meth ?content_type ?content ?headers ?msg url >|= fun code ->
!request_reply_hook ();
code
let add_hook f =
let old_hook = !before_hook in
before_hook := (fun () -> old_hook (); f ())
let add_reply_hook f =
let old_hook = !request_reply_hook in
request_reply_hook := (fun () -> old_hook (); f ())
let get = internal_get
let post = internal_post
module Raw = struct
let get0 ?(post=false) ? ?(params=[]) ?msg
api (service: ('output, 'error, 'security) EzAPI.service0) =
!before_hook ();
let meth = Service.meth service.s in
if post then
let url = forge0 api service [] in
let content = encode_params service.s params in
let content_type = Url.content_type in
internal_post ~meth ~content ~content_type ?headers ?msg url >|=
handle_result service
else
let url = EzAPI.forge0 api service params in
internal_get ~meth ?headers ?msg url >|= handle_result service
let get1 ?(post=false) ? ?(params=[]) ?msg
api (service : ('arg,'output, 'error, 'security) EzAPI.service1) (arg : 'arg) =
!before_hook ();
let meth = Service.meth service.s in
if post then
let url = forge1 api service arg [] in
let content = encode_params service.s params in
let content_type = Url.content_type in
internal_post ~meth ~content ~content_type ?headers ?msg url >|=
handle_result service
else
let url = EzAPI.forge1 api service arg params in
internal_get ~meth ?headers ?msg url >|= handle_result service
let get2 ?(post=false) ? ?(params=[]) ?msg
api (service : ('arg1, 'arg2, 'output, 'error, 'security) EzAPI.service2)
(arg1 : 'arg1) (arg2 : 'arg2) =
!before_hook ();
let meth = Service.meth service.s in
if post then
let url = forge2 api service arg1 arg2 [] in
let content = encode_params service.s params in
let content_type = Url.content_type in
internal_post ~meth ~content ~content_type ?headers ?msg url >|=
handle_result service
else
let url = EzAPI.forge2 api service arg1 arg2 params in
internal_get ~meth ?headers ?msg url >|= handle_result service
let post0 :
type i.
?headers:(string * string) list ->
?params:(Param.t * param_value) list ->
?msg:string ->
?url_encode:bool ->
input:i ->
EzAPI.base_url ->
(i, 'output, 'error, 'security) EzAPI.post_service0 ->
('output, 'error) api_result Lwt.t =
fun ? ?(params=[]) ?msg ?(url_encode=false) ~input api service ->
!before_hook ();
let meth = Service.meth service.s in
let input_encoding = Service.input service.s in
let url = forge0 api service params in
let content, content_type = match input_encoding with
| Empty -> "", "application/json"
| Raw [] -> input, "application/octet-stream"
| Raw (h :: _) -> input, Mime.to_string h
| Json enc ->
if not url_encode then
EzEncoding.construct enc input, "application/json"
else
Url.encode_obj enc input, Url.content_type in
internal_post ~meth ~content ~content_type ?headers ?msg url >|=
handle_result service
let post1 :
type i.
?headers:(string * string) list ->
?params:(Param.t * param_value) list ->
?msg:string ->
?url_encode:bool ->
input:i ->
EzAPI.base_url ->
('arg, i, 'output, 'error, 'security) EzAPI.post_service1 -> 'arg ->
('output, 'error) api_result Lwt.t =
fun ? ?(params=[]) ?msg ?(url_encode=false) ~input api service arg ->
!before_hook ();
let meth = Service.meth service.s in
let input_encoding = Service.input service.s in
let url = forge1 api service arg params in
let content, content_type = match input_encoding with
| Empty -> "", "application/json"
| Raw [] -> input, "application/octet-stream"
| Raw (h :: _) -> input, Mime.to_string h
| Json enc ->
if not url_encode then
EzEncoding.construct enc input, "application/json"
else
Url.encode_obj enc input, Url.content_type in
internal_post ~meth ~content ~content_type ?headers ?msg url >|=
handle_result service
let post2 :
type i.
?headers:(string * string) list ->
?params:(Param.t * param_value) list ->
?msg:string ->
?url_encode:bool ->
input:i ->
EzAPI.base_url ->
('arg1, 'arg2, i, 'output, 'error, 'security) EzAPI.post_service2 -> 'arg1 -> 'arg2 ->
('output, 'error) api_result Lwt.t =
fun ? ?(params=[]) ?msg ?(url_encode=false) ~input api service arg1 arg2 ->
!before_hook ();
let meth = Service.meth service.s in
let input_encoding = Service.input service.s in
let url = forge2 api service arg1 arg2 params in
let content, content_type = match input_encoding with
| Empty -> "", "application/json"
| Raw [] -> input, "application/octet-stream"
| Raw (h :: _) -> input, Mime.to_string h
| Json enc ->
if not url_encode then
EzEncoding.construct enc input, "application/json"
else
Url.encode_obj enc input, Url.content_type in
internal_post ~meth ~content ~content_type ?headers ?msg url >|=
handle_result service
end
include Raw
module Legacy = struct
type ('output, 'error, 'security) service0 =
('output) Legacy.service0
constraint 'security = [< Security.scheme ]
type ('arg, 'output, 'error, 'security) service1 =
('arg, 'output) Legacy.service1
constraint 'security = [< Security.scheme ]
type ('arg1, 'arg2, 'output, 'error, 'security) service2 =
('arg1, 'arg2, 'output) Legacy.service2
constraint 'security = [< Security.scheme ]
type ('input, 'output, 'error, 'security) post_service0 =
('input, 'output) Legacy.post_service0
constraint 'security = [< Security.scheme ]
type ('arg, 'input, 'output, 'error, 'security) post_service1 =
('arg, 'input, 'output) Legacy.post_service1
constraint 'security = [< Security.scheme ]
type ('arg1, 'arg2, 'input, 'output, 'error, 'security) post_service2 =
('arg1, 'arg2, 'input, 'output) Legacy.post_service2
constraint 'security = [< Security.scheme ]
open EzAPI.Legacy
let unresultize = function
| Ok res -> Ok res
| Error UnknownError { code ; msg } -> Error (code, msg)
| Error KnownError _ -> assert false
let get0 ?post ? ?params ?msg
api (service: 'output EzAPI.Legacy.service0) =
get0 ?post ?headers ?params ?msg api service
>|= unresultize
let get1 ?post ? ?params ?msg
api (service : ('arg,'output) service1) (arg : 'arg) =
get1 ?post ?headers ?params ?msg api service arg
>|= unresultize
let get2 ?post ? ?params ?msg
api (service : ('arg1, 'arg2, 'output) service2) (arg1 : 'arg1) (arg2 : 'arg2) =
get2 ?post ?headers ?params ?msg api service arg1 arg2
>|= unresultize
let post0 ? ?params ?msg ?url_encode ~(input : 'input)
api (service : ('input,'output) post_service0) =
post0 ?headers ?params ?msg ?url_encode ~input api service
>|= unresultize
let post1 ? ?params ?msg ?url_encode ~(input : 'input)
api (service : ('arg, 'input,'output) post_service1) (arg : 'arg) =
post1 ?headers ?params ?msg ?url_encode ~input api service arg
>|= unresultize
let post2 ? ?params ?msg ?url_encode ~(input : 'input)
api (service : ('arg1, 'arg2, 'input, 'output) post_service2)
(arg1 : 'arg1) (arg2 : 'arg2) =
post2 ?headers ?params ?msg ?url_encode ~input api service arg1 arg2
>|= unresultize
end
end
module ANY = Make(struct
let get ?meth ? ?msg url = !any_get ?meth ?headers ?msg url
let post ?meth ?content_type ?content ? ?msg url =
!any_post ?meth ?content_type ?content ?headers ?msg url
end)