|
1 | 1 | open Lwt.Infix; |
| 2 | +module Client = Piaf.Client.Oneshot; |
2 | 3 |
|
3 | | -let send_request = (~meth=`GET, ~additional_headers=[], ~body=?, uri) => { |
4 | | - open Httpaf; |
5 | | - open Httpaf_lwt_unix; |
6 | | - let response_handler = (notify_response_received, response, response_body) => |
7 | | - Lwt.wakeup_later( |
8 | | - notify_response_received, |
9 | | - Ok((response, response_body)), |
10 | | - ); |
11 | | - |
12 | | - let error_handler = (notify_response_received, error) => |
13 | | - Lwt.wakeup_later(notify_response_received, Error(error)); |
14 | | - |
15 | | - let host = Uri.host_with_default(uri); |
16 | | - let port = |
17 | | - switch (Uri.port(uri)) { |
18 | | - | None => "80" |
19 | | - | Some(p) => string_of_int(p) |
20 | | - }; |
21 | | - |
22 | | - Lwt_unix.getaddrinfo(host, port, [Unix.(AI_FAMILY(PF_INET))]) |
23 | | - >>= ( |
24 | | - addresses => { |
25 | | - let socket = Lwt_unix.socket(Unix.PF_INET, Unix.SOCK_STREAM, 0); |
26 | | - Lwt_unix.connect(socket, List.hd(addresses).Unix.ai_addr) |
27 | | - >>= ( |
28 | | - () => { |
29 | | - Client.create_connection(socket) |
30 | | - >>= ( |
31 | | - connection => { |
32 | | - let content_length = |
33 | | - switch (body) { |
34 | | - | None => "0" |
35 | | - | Some(body) => string_of_int(String.length(body)) |
36 | | - }; |
37 | | - |
38 | | - let request_headers = |
39 | | - Request.create( |
40 | | - meth, |
41 | | - Uri.path_and_query(uri), |
42 | | - ~headers= |
43 | | - Headers.of_list( |
44 | | - [("Host", host), ("Content-Length", content_length)] |
45 | | - @ additional_headers, |
46 | | - ), |
47 | | - ); |
48 | | - |
49 | | - let (response_received, notify_response_received) = Lwt.wait(); |
50 | | - let response_handler = |
51 | | - response_handler(notify_response_received); |
52 | | - let error_handler = error_handler(notify_response_received); |
53 | | - |
54 | | - let request_body = |
55 | | - Client.request( |
56 | | - connection, |
57 | | - request_headers, |
58 | | - ~error_handler, |
59 | | - ~response_handler, |
60 | | - ); |
61 | | - |
62 | | - switch (body) { |
63 | | - | Some(body) => Body.write_string(request_body, body) |
64 | | - | None => () |
65 | | - }; |
66 | | - Body.flush(request_body, () => Body.close_writer(request_body)); |
67 | | - response_received; |
68 | | - } |
69 | | - ); |
70 | | - } |
71 | | - ); |
72 | | - } |
73 | | - ); |
74 | | -}; |
75 | | - |
76 | | -let read_response = response_body => { |
77 | | - let buf = Buffer.create(1024); |
78 | | - let (body_read, notify_body_read) = Lwt.wait(); |
79 | | - let rec read_fn = () => |
80 | | - Httpaf.Body.schedule_read( |
81 | | - response_body, |
82 | | - ~on_eof=() => Lwt.wakeup_later(notify_body_read, Buffer.contents(buf)), |
83 | | - ~on_read= |
84 | | - (response_fragment, ~off, ~len) => { |
85 | | - let response_fragment_bytes = Bytes.create(len); |
86 | | - Lwt_bytes.blit_to_bytes( |
87 | | - response_fragment, |
88 | | - off, |
89 | | - response_fragment_bytes, |
90 | | - 0, |
91 | | - len, |
92 | | - ); |
93 | | - Buffer.add_bytes(buf, response_fragment_bytes); |
94 | | - read_fn(); |
95 | | - }, |
96 | | - ); |
97 | | - |
98 | | - read_fn(); |
99 | | - body_read; |
100 | | -}; |
101 | | - |
102 | | -let my_handler = (reqd, _context) => { |
| 4 | +let my_handler = (_request, _context) => { |
103 | 5 | let uri = |
104 | 6 | Uri.of_string( |
105 | 7 | "http://api.giphy.com/v1/gifs/random?tag=cat&api_key=hamBGlVDz0XI5tYtxTuPgudCVhHSNX8q&limit=1", |
106 | 8 | ); |
107 | | - send_request(uri) |
| 9 | + Client.get(uri) |
108 | 10 | >>= ( |
109 | 11 | fun |
110 | | - | Ok((_response, body)) => |
111 | | - read_response(body) |
112 | | - >>= ( |
113 | | - body_str => { |
114 | | - open Yojson.Safe; |
115 | | - let body_json = Yojson.Safe.from_string(body_str); |
116 | | - let img_url = |
117 | | - body_json |
118 | | - |> Util.member("data") |
119 | | - |> Util.member("images") |
120 | | - |> Util.member("original") |
121 | | - |> Util.member("url") |
122 | | - |> Util.to_string; |
123 | | - let body = Printf.sprintf("<img src=\"%s\">", img_url); |
124 | | - let response = |
125 | | - Httpaf.Response.create( |
126 | | - ~headers= |
127 | | - Httpaf.Headers.of_list([("content-type", "text/html")]), |
128 | | - `OK, |
129 | | - ); |
130 | | - Lwt.return(Now.Reqd.respond_with_string(reqd, response, body)); |
131 | | - } |
132 | | - ) |
| 12 | + | Ok(response) => { |
| 13 | + Piaf.Body.to_string(response.body) |
| 14 | + >>= ( |
| 15 | + fun |
| 16 | + | Ok(body_str) => { |
| 17 | + open Yojson.Safe; |
| 18 | + let body_json = Yojson.Safe.from_string(body_str); |
| 19 | + let img_url = |
| 20 | + body_json |
| 21 | + |> Util.member("data") |
| 22 | + |> Util.member("images") |
| 23 | + |> Util.member("original") |
| 24 | + |> Util.member("url") |
| 25 | + |> Util.to_string; |
| 26 | + let body = Printf.sprintf("<img src=\"%s\">", img_url); |
| 27 | + let response = |
| 28 | + Piaf.Response.of_string( |
| 29 | + ~body, |
| 30 | + ~headers= |
| 31 | + Piaf.Headers.of_list([("content-type", "text/html")]), |
| 32 | + `OK, |
| 33 | + ); |
| 34 | + Lwt.return_ok(response); |
| 35 | + } |
| 36 | + | Error(_) => Lwt.return(Error("Failed for some reason")) |
| 37 | + ); |
| 38 | + } |
133 | 39 | | Error(_) => Lwt.return(Error("Failed for some reason")) |
134 | 40 | ); |
135 | 41 | }; |
|
0 commit comments