From d79e7e26cdfd41b646592a565cd9a3a3d4fc2264 Mon Sep 17 00:00:00 2001 From: Bikal Lem Date: Thu, 9 Jun 2022 16:30:11 +0100 Subject: [PATCH] eio(client): add get,head,delete,post,put and patch --- cohttp-eio/src/client.ml | 22 ++++++++++-- cohttp-eio/src/cohttp_eio.mli | 65 ++++++++++++++++++++++++++++++++--- 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/cohttp-eio/src/client.ml b/cohttp-eio/src/client.ml index f16198599e..53c0f03cfb 100644 --- a/cohttp-eio/src/client.ml +++ b/cohttp-eio/src/client.ml @@ -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); @@ -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 diff --git a/cohttp-eio/src/cohttp_eio.mli b/cohttp-eio/src/cohttp_eio.mli index 6335934378..4025b8f2e8 100644 --- a/cohttp-eio/src/cohttp_eio.mli +++ b/cohttp-eio/src/cohttp_eio.mli @@ -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" *) @@ -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