Skip to content

Commit

Permalink
eio(client): add get,head,delete,post,put and patch
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Jun 9, 2022
1 parent a6e6c41 commit d79e7e2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
22 changes: 20 additions & 2 deletions cohttp-eio/src/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ let response reader =
commit reader;
Http.Response.make ~version ~status ~headers ()

let connect ?(version = `HTTP_1_1) ?(headers = Http.Header.init ())
?(body = Body.Empty) env sw stream meth uri =
let connect ?(meth = `GET) ?(version = `HTTP_1_1)
?(headers = Http.Header.init ()) ?(body = Body.Empty) env sw stream uri =
let sock = Net.connect ~sw (Stdenv.net env) stream in
let writer = Writer.create (sock :> Flow.sink) in
Fiber.fork ~sw (fun () -> Writer.run writer);
Expand All @@ -52,3 +52,21 @@ let connect ?(version = `HTTP_1_1) ?(headers = Http.Header.init ())
let reader = Reader.create 0x1000 (sock :> Flow.source) in
let response = response reader in
(response, reader)

let get ?version ?headers env sw stream uri =
connect ~meth:`GET ?version ?headers env sw stream uri

let head ?version ?headers env sw stream uri =
connect ~meth:`HEAD ?version ?headers env sw stream uri

let delete ?version ?headers env sw stream uri =
connect ~meth:`DELETE ?version ?headers env sw stream uri

let post ?version ?headers ?body env sw stream uri =
connect ~meth:`POST ?version ?headers ?body env sw stream uri

let put ?version ?headers ?body env sw stream uri =
connect ~meth:`PUT ?version ?headers ?body env sw stream uri

let patch ?version ?headers ?body env sw stream uri =
connect ~meth:`PATCH ?version ?headers ?body env sw stream uri
65 changes: 61 additions & 4 deletions cohttp-eio/src/cohttp_eio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ module Server : sig
(** [read_chunked request chunk_handler] is [updated_headers] if
"Transfer-Encoding" header value is "chunked" in [headers] and all chunks
in [reader] are read successfully. [updated_headers] is the updated
headers as specified by the chunked encoding algorithm in
https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.3. Otherwise it
is [Error err] where [err] is the error text.
headers as specified by the chunked encoding algorithm in https:
//datatracker.ietf.org/doc/html/rfc7230#section-4.1.3. Otherwise it is
[Error err] where [err] is the error text.
@raise Invalid_argument
if [Transfer-Encoding] header in [headers] is not specified as "chunked" *)
Expand Down Expand Up @@ -158,14 +158,71 @@ end
module Client : sig
type response = Http.Response.t * Reader.t

val get :
?version:Http.Version.t ->
?headers:Http.Header.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
response

val head :
?version:Http.Version.t ->
?headers:Http.Header.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
response

val delete :
?version:Http.Version.t ->
?headers:Http.Header.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
response

val post :
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
response

val put :
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
response

val patch :
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Uri.t ->
response

val connect :
?meth:Http.Method.t ->
?version:Http.Version.t ->
?headers:Http.Header.t ->
?body:Body.t ->
Eio.Stdenv.t ->
Eio.Switch.t ->
Eio.Net.Sockaddr.stream ->
Http.Method.t ->
Uri.t ->
response
end

0 comments on commit d79e7e2

Please sign in to comment.