Skip to content

Instantly share code, notes, and snippets.

@co-dan

co-dan/req.ml Secret

Created October 25, 2018 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save co-dan/b5b1019afc3363eaf2e8d987c185de07 to your computer and use it in GitHub Desktop.
Save co-dan/b5b1019afc3363eaf2e8d987c185de07 to your computer and use it in GitHub Desktop.
open Lwt
(* cannot load this file up in utop without the next two lines *)
(* #require "conduit"
* #require "conduit-lwt-unix" *)
open Conduit
open Conduit_lwt_unix
let resolve_addr (s: string) : endp Lwt.t =
let open Resolver_lwt_unix in
let uri = Uri.of_string s in
Resolver_lwt.resolve_uri ~uri system
let client_of_addr (s: string) : client Lwt.t =
resolve_addr s >>= endp_to_client ~ctx:default_ctx
(* TODO: errror handling etc. Conflicts with Lwt_io.with_connection? *)
let with_connection (s: string) (f : ic -> oc -> 'a Lwt.t) : 'a Lwt.t =
let open Lwt_io in
client_of_addr s >>= fun cl ->
connect ~ctx:default_ctx cl >>= fun (_,ic,oc) ->
f ic oc >>= fun r ->
close oc >>= fun _ ->
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment