Source file request_utils.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
type cohttp_request_data = {
meth: Cohttp.Code.meth;
body: Cohttp_lwt.Body.t option;
request_headers: Cohttp.Header.t;
uri: Uri.t;
}
let make_request_data meth ?data ?params ? ?auth url =
let open Request in
let = Cohttp.Header.init () in
let =
Cohttp.Header.add request_headers "accept-encoding" "gzip"
in
let body, =
let body, =
match data with
| Some (Form data) ->
( Some (Utils.data_to_body data),
[ ("Content-Type", "application/x-www-form-urlencoded") ] )
| Some (Json json) ->
( Some (Utils.json_to_body json),
[ ("Content-Type", "application/json") ] )
| Some (Raw s) -> (Some (Cohttp_lwt.Body.of_string s), [])
| None -> (None, [])
in
(body, Cohttp.Header.add_list request_headers new_headers_list)
in
let =
match auth with
| Some (Basic (username, password)) ->
Cohttp.Header.add_authorization request_headers
(`Basic (username, password))
| Some (Bearer s) ->
Cohttp.Header.add_authorization request_headers (`Other ("Bearer " ^ s))
| None -> request_headers
in
let =
match headers with
| Some ->
Cohttp.Header.add_list request_headers specified_headers
| None -> request_headers
in
let uri =
match params with
| Some params -> Uri.(add_query_params' (Uri.of_string url) params)
| None -> Uri.of_string url
in
{ meth; body; request_headers; uri }
let is_meth_chunked = function
| `HEAD -> false
| `GET -> false
| `DELETE -> false
| _ -> true
let request_of_request_data { meth; body; ; uri } =
let body = Option.value ~default:`Empty body in
let chunked = is_meth_chunked meth in
match chunked with
| true ->
Cohttp.Request.make_for_client ~headers:request_headers ~chunked meth uri
|> Lwt.return
| false ->
let open Lwt in
Cohttp_lwt.Body.length body >|= fun (body_length, _buf) ->
Cohttp.Request.make_for_client ~headers:request_headers ~chunked
~body_length meth uri