package bitcoin-cohttp-lwt

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file bitcoin_cohttp_lwt.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(********************************************************************************)
(*  Bitcoin_cohttp.ml
    Copyright (c) 2013 Vincent Bernardoff <vb@luminar.eu.org>
*)
(********************************************************************************)

(** Offers an implementation of a {!Bitcoin.HTTPCLIENT} using Cohttp's
    [Cohttp_lwt_unix.Client].
*)

(********************************************************************************)
(** {1 Exceptions}                                                              *)
(********************************************************************************)

exception No_response

(********************************************************************************)
(** {1 Private modules}                                                         *)
(********************************************************************************)

module C = Cohttp
module CU = Cohttp_lwt_unix
module CB = Cohttp_lwt.Body

(********************************************************************************)
(** {1 Public modules}                                                          *)
(********************************************************************************)

module Httpclient : Bitcoin.HTTPCLIENT with type 'a Monad.t = 'a Lwt.t = struct
  module Monad = Lwt

  let ( >>= ) = Lwt.bind

  let post_string ~headers ~inet_addr:_ ~host ~port ~uri request =
    let headers = C.Header.of_list headers in
    let uri = Uri.make ~scheme:"http" ~host ~port ~path:uri () in
    Lwt.try_bind
      (fun () -> CU.Client.call ~chunked:false ~headers ~body:(CB.of_string request) `POST uri)
      (fun (_, b) -> CB.to_string b)
      (fun exn ->
        Lwt_io.printf "Cohttp_lwt_unix.Client.call: caught %s\n" (Printexc.to_string exn)
        >>= fun () -> Lwt.fail exn)
end
OCaml

Innovation. Community. Security.