Skip to content

Commit 09c2d01

Browse files
committed
eio(client): Client.call expects conn, host and resource_path as paramters
1 parent 7c6519b commit 09c2d01

File tree

5 files changed

+67
-78
lines changed

5 files changed

+67
-78
lines changed

cohttp-eio/examples/client1.ml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
open Eio
22
open Cohttp_eio
33

4-
let conn env sw resource_path =
4+
let () =
5+
Eio_main.run @@ fun env ->
6+
Switch.run @@ fun sw ->
57
let hostname, port = ("www.example.org", 80) in
68
let he = Unix.gethostbyname hostname in
79
let addr = `Tcp (Eio_unix.Ipaddr.of_unix he.h_addr_list.(0), port) in
8-
let flow = Net.connect ~sw env#net addr in
10+
let conn = Net.connect ~sw env#net addr in
911
let host = (hostname, Some port) in
10-
(resource_path, host, flow)
11-
12-
let () =
13-
Eio_main.run @@ fun env ->
14-
Switch.run @@ fun sw ->
15-
let res = Client.get (conn env sw) "/" in
12+
let res = Client.get ~conn host "/" in
1613
print_string @@ Client.read_fixed res

cohttp-eio/examples/docker_client.ml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ module Client = Cohttp_eio.Client
55
module Response = Http.Response
66
module Status = Http.Status
77

8-
let conn env sw resource_path =
9-
let hostname = "docker" in
10-
let addr = `Unix "/var/run/docker.sock" in
11-
let flow = Net.connect ~sw env#net addr in
12-
let host = (hostname, None) in
13-
(resource_path, host, flow)
14-
158
let () =
169
Eio_main.run @@ fun env ->
1710
Switch.run @@ fun sw ->
18-
let res = Client.get (conn env sw) "/version" in
11+
let addr = `Unix "/var/run/docker.sock" in
12+
let conn = Net.connect ~sw env#net addr in
13+
let res = Client.get ~conn ("docker", None) "/version" in
1914
let code = fst res |> Response.status |> Status.to_int in
2015
Printf.printf "Response code: %d\n" code;
2116
Printf.printf "Headers: %s\n"

cohttp-eio/src/client.ml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ module Buf_write = Eio.Buf_write
44
type response = Http.Response.t * Buf_read.t
55
type host = string * int option
66
type resource_path = string
7-
type ('a, 'b) conn = 'a -> (resource_path * host * #Eio.Flow.two_way as 'b)
87

9-
type ('a, 'b) body_disallowed_call =
8+
type 'a body_disallowed_call =
109
?version:Http.Version.t ->
1110
?headers:Http.Header.t ->
12-
('a, 'b) conn ->
13-
'a ->
11+
conn:(#Eio.Flow.two_way as 'a) ->
12+
host ->
13+
resource_path ->
1414
response
1515
(** [body_disallowed_call] denotes HTTP client calls where a request is not
1616
allowed to have a request body. *)
1717

18-
type ('a, 'b) body_allowed_call =
18+
type 'a body_allowed_call =
1919
?version:Http.Version.t ->
2020
?headers:Http.Header.t ->
2121
?body:Body.t ->
22-
('a, 'b) conn ->
23-
'a ->
22+
conn:(#Eio.Flow.two_way as 'a) ->
23+
host ->
24+
resource_path ->
2425
response
2526

2627
(* Request line https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1 *)
@@ -62,43 +63,42 @@ let response buf_read =
6263
(* Generic HTTP call *)
6364

6465
let call ?(meth = `GET) ?(version = `HTTP_1_1) ?(headers = Http.Header.init ())
65-
?(body = Body.Empty) conn_fn uri =
66-
let resource_path, (host_name, host_port), flow = conn_fn uri in
66+
?(body = Body.Empty) ~conn host resource_path =
6767
let host =
68-
match host_port with
69-
| Some port -> host_name ^ ":" ^ string_of_int port
70-
| None -> host_name
68+
match host with
69+
| host, Some port -> host ^ ":" ^ string_of_int port
70+
| host, None -> host
7171
in
72-
Buf_write.with_flow ~initial_size:0x1000 flow (fun writer ->
72+
Buf_write.with_flow ~initial_size:0x1000 conn (fun writer ->
7373
let headers = Http.Header.add_unless_exists headers "Host" host in
7474
write_request writer (meth, version, headers, resource_path, body);
7575
let reader =
76-
Eio.Buf_read.of_flow ~initial_size:0x1000 ~max_size:max_int flow
76+
Eio.Buf_read.of_flow ~initial_size:0x1000 ~max_size:max_int conn
7777
in
7878
let response = response reader in
7979
(response, reader))
8080

8181
(* HTTP Calls with Body Disallowed *)
8282

83-
let get ?version ?headers conn_fn uri =
84-
call ~meth:`GET ?version ?headers conn_fn uri
83+
let get ?version ?headers ~conn host resource_path =
84+
call ~meth:`GET ?version ?headers ~conn host resource_path
8585

86-
let head ?version ?headers stream uri =
87-
call ~meth:`HEAD ?version ?headers stream uri
86+
let head ?version ?headers ~conn host resource_path =
87+
call ~meth:`HEAD ?version ?headers ~conn host resource_path
8888

89-
let delete ?version ?headers stream uri =
90-
call ~meth:`DELETE ?version ?headers stream uri
89+
let delete ?version ?headers ~conn host resource_path =
90+
call ~meth:`DELETE ?version ?headers ~conn host resource_path
9191

9292
(* HTTP Calls with Body Allowed *)
9393

94-
let post ?version ?headers ?body stream uri =
95-
call ~meth:`POST ?version ?headers ?body stream uri
94+
let post ?version ?headers ?body ~conn host resource_path =
95+
call ~meth:`POST ?version ?headers ?body ~conn host resource_path
9696

97-
let put ?version ?headers ?body stream uri =
98-
call ~meth:`PUT ?version ?headers ?body stream uri
97+
let put ?version ?headers ?body ~conn host resource_path =
98+
call ~meth:`PUT ?version ?headers ?body ~conn host resource_path
9999

100-
let patch ?version ?headers ?body stream uri =
101-
call ~meth:`PATCH ?version ?headers ?body stream uri
100+
let patch ?version ?headers ?body ~conn host resource_path =
101+
call ~meth:`PATCH ?version ?headers ?body ~conn host resource_path
102102

103103
(* Response Body *)
104104

cohttp-eio/src/cohttp_eio.mli

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module Server : sig
101101
val not_found_handler : handler
102102
end
103103

104+
(** [Client] is a HTTP/1.1 client. *)
104105
module Client : sig
105106
type response = Http.Response.t * Eio.Buf_read.t
106107

@@ -112,25 +113,23 @@ module Client : sig
112113
(** Represents HTTP request resource path, e.g. "/shop/purchase",
113114
"/shop/items", "/shop/categories/" etc. *)
114115

115-
type ('a, 'b) conn = 'a -> (resource_path * host * #Eio.Flow.two_way as 'b)
116-
(** [('a, 'b conn)] is [(resource_path, host, flow)]. [flow] is the Eio flow
117-
value which is connected to the [host]. *)
118-
119-
type ('a, 'b) body_disallowed_call =
116+
type 'a body_disallowed_call =
120117
?version:Http.Version.t ->
121118
?headers:Http.Header.t ->
122-
('a, 'b) conn ->
123-
'a ->
119+
conn:(#Eio.Flow.two_way as 'a) ->
120+
host ->
121+
resource_path ->
124122
response
125123
(** [body_disallowed_call] denotes HTTP client calls where a request is not
126124
allowed to have a request body. *)
127125

128-
type ('a, 'b) body_allowed_call =
126+
type 'a body_allowed_call =
129127
?version:Http.Version.t ->
130128
?headers:Http.Header.t ->
131129
?body:Body.t ->
132-
('a, 'b) conn ->
133-
'a ->
130+
conn:(#Eio.Flow.two_way as 'a) ->
131+
host ->
132+
resource_path ->
134133
response
135134
(** [body_allowed_call] denotes HTTP client calls where a request can
136135
optionally have a request body. *)
@@ -142,21 +141,22 @@ module Client : sig
142141
?version:Http.Version.t ->
143142
?headers:Http.Header.t ->
144143
?body:Body.t ->
145-
('a, 'b) conn ->
146-
'a ->
144+
conn:#Eio.Flow.two_way ->
145+
host ->
146+
resource_path ->
147147
response
148148

149149
(** {1 HTTP Calls with Body Disallowed} *)
150150

151-
val get : ('a, 'b) body_disallowed_call
152-
val head : ('a, 'b) body_disallowed_call
153-
val delete : ('a, 'b) body_disallowed_call
151+
val get : 'a body_disallowed_call
152+
val head : 'a body_disallowed_call
153+
val delete : 'a body_disallowed_call
154154

155155
(** {1 HTTP Calls with Body Allowed} *)
156156

157-
val post : ('a, 'b) body_allowed_call
158-
val put : ('a, 'b) body_allowed_call
159-
val patch : ('a, 'b) body_allowed_call
157+
val post : 'a body_allowed_call
158+
val put : 'a body_allowed_call
159+
val patch : 'a body_allowed_call
160160

161161
(** {1 Response Body} *)
162162

cohttp-eio/tests/test_client.ml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@ module Stdenv = Eio.Stdenv
33
module Switch = Eio.Switch
44
open Cohttp_eio
55

6-
let conn env sw port resource_path =
7-
let addr = `Tcp (Net.Ipaddr.V4.loopback, port) in
8-
let flow = Net.connect ~sw env#net addr in
9-
let host = ("localhost", Some port) in
10-
(resource_path, host, flow)
11-
12-
let get env sw port =
6+
let get conn host =
137
let res =
148
Client.get
159
~headers:(Http.Header.of_list [ ("Accept", "application/json") ])
16-
(conn env sw port) "/get"
10+
~conn host "/get"
1711
in
1812
print_string @@ Client.read_fixed res
1913

20-
let post env sw port =
14+
let post conn host =
2115
let content = "hello world!" in
2216
let content_length = String.length content |> string_of_int in
2317
let res =
@@ -27,7 +21,7 @@ let post env sw port =
2721
[
2822
("Accept", "application/json"); ("Content-Length", content_length);
2923
])
30-
~body:(Body.Fixed content) (conn env sw port) "/post"
24+
~body:(Body.Fixed content) ~conn host "/post"
3125
in
3226
print_string @@ Client.read_fixed res
3327

@@ -36,7 +30,7 @@ let post env sw port =
3630
Read from text file "chunks.txt" and write each line as a chunk. We add some
3731
chunk extensions to the first chunk. This is purely for demonstrative effect
3832
and for testing purposes rather than for any such specific requirement. *)
39-
let post_chunk env sw port =
33+
let post_chunk conn host =
4034
let rec body_writer chan chunks f =
4135
match In_channel.input_line chan with
4236
| Some data ->
@@ -80,17 +74,17 @@ let post_chunk env sw port =
8074
])
8175
~body:
8276
(Body.Chunked { body_writer = body_writer chan 0; trailer_writer })
83-
(conn env sw port) "/handle_chunk")
77+
~conn host "/handle_chunk")
8478
|> Client.read_fixed
8579
|> print_string
8680

8781
(* Read chunk and dump to a "client_chunks2.txt" *)
88-
let get_chunk env sw port =
82+
let get_chunk env conn host =
8983
let write_chunk_to_file flow chunk =
9084
let data = Format.asprintf "%a\n\n" Body.pp_chunk chunk in
9185
Eio.Flow.copy_string data flow
9286
in
93-
let res = Client.get (conn env sw port) "/get_chunk" in
87+
let res = Client.get ~conn host "/get_chunk" in
9488
let path = Eio.Path.(Stdenv.cwd env / "client_chunks2.txt") in
9589
Eio.Path.with_open_out ~append:false ~create:(`Or_truncate 0o666) path
9690
(fun flow ->
@@ -114,9 +108,12 @@ let () =
114108

115109
Eio_main.run @@ fun env ->
116110
Switch.run @@ fun sw ->
111+
let addr = `Tcp (Net.Ipaddr.V4.loopback, !port) in
112+
let conn = Net.connect ~sw env#net addr in
113+
let host = ("localhost", Some !port) in
117114
match !t with
118-
| "get" -> get env sw !port
119-
| "post" -> post env sw !port
120-
| "post_chunk" -> post_chunk env sw !port
121-
| "get_chunk" -> get_chunk env sw !port
115+
| "get" -> get conn host
116+
| "post" -> post conn host
117+
| "post_chunk" -> post_chunk conn host
118+
| "get_chunk" -> get_chunk env conn host
122119
| _ -> print_string "Usage: test-client [get|post]"

0 commit comments

Comments
 (0)