-
Notifications
You must be signed in to change notification settings - Fork 1
/
blue_http.ml
100 lines (90 loc) · 2.59 KB
/
blue_http.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
open Core
open Async
module Client = Client
module For_testing = struct
module Pool = Pool
end
type tags = (string * string) list
let set_default_max_redirects = Redirect.set_default_max_redirects
let maybe_with_client ?client f =
match client with
| Some client -> f client
| None ->
let client = Client.create () in
Monitor.protect
(fun () -> f client)
~finally:(fun () ->
(* We can't wait for the client to close because it doesn't finish until the body is fully read *)
Client.close client |> don't_wait_for;
Deferred.unit)
;;
let call_stream
?(tags = [])
?max_redirects
?interrupt
?headers
?chunked
?body
?client
meth
uri
=
let tags =
if List.exists tags ~f:(fun (k, _) -> String.Caseless.("request_uuid" = k))
then tags
else (
let uuid = Uuid_unix.create () |> Uuid.to_string in
("request_uuid", uuid) :: tags)
in
Logger.with_tags tags
@@ fun () ->
maybe_with_client ?client
@@ fun client ->
Timing.run_with_timing ~label:"total_time_call_stream"
@@ fun () ->
Redirect.with_redirects ?max_redirects uri
@@ fun uri -> Client.call ?interrupt ?headers ?chunked ?body client meth uri
;;
let request_stream ?tags ?max_redirects ?interrupt ?chunked ?body ?uri ?client req =
let uri =
match uri with
| Some t -> t
| None -> Cohttp.Request.uri req
and headers = Cohttp.Request.headers req
and meth = Cohttp.Request.meth req in
call_stream ?tags ?interrupt ~headers ?chunked ?body ?client ?max_redirects meth uri
;;
let call ?tags ?max_redirects ?interrupt ?headers ?chunked ?body ?client meth uri =
let%bind res, body =
call_stream ?tags ?max_redirects ?interrupt ?headers ?chunked ?body ?client meth uri
in
Cohttp_async.Body.to_string body >>| fun body -> res, body
;;
let request ?tags ?max_redirects ?interrupt ?chunked ?body ?uri ?client req =
let%bind res, body =
request_stream ?tags ?max_redirects ?interrupt ?chunked ?body ?uri ?client req
in
Cohttp_async.Body.to_string body >>| fun body -> res, body
;;
let call_ignore_body
?tags
?max_redirects
?interrupt
?headers
?chunked
?body
?client
meth
uri
=
let%bind res, body =
call_stream ?tags ?max_redirects ?interrupt ?headers ?chunked ?body ?client meth uri
in
Cohttp_async.Body.drain body >>| fun () -> res
;;
let request_ignore_body ?tags ?max_redirects ?interrupt ?chunked ?body ?uri ?client req =
let%bind res, body =
request_stream ?tags ?max_redirects ?interrupt ?chunked ?body ?uri ?client req
in
Cohttp_async.Body.drain body >>| fun () -> res
;;