-
-
Save hannesm/2d2bd883d929bb15ba1f5f82a230b9f0 to your computer and use it in GitHub Desktop.
https fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Mirage | |
let client = | |
let packages = [ package "cohttp-mirage"; package "duration" ; package ~sublibs:["mirage"] "tls" ] in | |
foreign | |
~packages | |
"Unikernel.Client" @@ time @-> resolver @-> conduit @-> job | |
let () = | |
let stack = generic_stackv4 default_network in | |
let res_dns = resolver_dns stack in | |
let conduit = conduit_direct stack in | |
let job = [ client $ default_time $ res_dns $ conduit ] in | |
register "https-fetch" job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Lwt.Infix | |
open Mirage_types_lwt | |
let uri = Uri.of_string "https://nqsb.io" | |
module Client (T: TIME) (RES: Resolver_lwt.S) (CON: Conduit_mirage.S) = struct | |
let https_fetch resolver ctx = | |
Logs.info (fun m -> m "fetching %a" Uri.pp_hum uri) ; | |
let ctx = Cohttp_mirage.Client.ctx resolver ctx in | |
Cohttp_mirage.Client.get ~ctx uri >>= fun (response, body) -> | |
Cohttp_lwt.Body.to_string body >>= fun body -> | |
Logs.info (fun m -> m "%s" (Sexplib.Sexp.to_string_hum (Cohttp.Response.sexp_of_t response))) ; | |
Logs.info (fun m -> m "Received body length: %d" (String.length body)) ; | |
Lwt.return_unit | |
let start _time res ctx = | |
Conduit_mirage.with_tls ctx >>= fun ctx -> | |
https_fetch res ctx | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment