Skip to content

Commit 6983f0a

Browse files
committed
Merge pull request #23 from SGrondin/master
OCaml updates
2 parents b50e5ea + 60470e2 commit 6983f0a

11 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/targets/ocaml/cohttp.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ module.exports = function (source, options) {
77
indent: ' '
88
}, options)
99

10+
var methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options']
1011
var code = []
1112

1213
code.push('open Cohttp_lwt_unix')
14+
code.push('open Cohttp')
1315
code.push('open Lwt')
1416
code.push('')
1517

@@ -32,16 +34,16 @@ module.exports = function (source, options) {
3234
// Add body
3335
if (source.postData.text) {
3436
// Just text
35-
code.push(util.format('let body = %s in', JSON.stringify(source.postData.text)))
37+
code.push(util.format('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text)))
3638
}
3739

3840
// Do the request
3941
code.push('')
4042

41-
code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri',
43+
code.push(util.format('Client.call %s%s%s uri',
4244
headers.length ? '~headers ' : '',
4345
source.postData.text ? '~body ' : '',
44-
source.method
46+
(methods.indexOf(this.source.method.toLowerCase()) >= 0 ? ('`' + this.source.method.toUpperCase()) : '(Code.method_of_string "' + this.source.method + '")')
4547
))
4648

4749
// Catch result
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
56
let headers = Header.init ()
67
|> fun h -> Header.add h "content-type" "application/x-www-form-urlencoded"
78
in
8-
let body = "foo=bar&hello=world" in
9+
let body = Cohttp_lwt_body.of_string "foo=bar&hello=world" in
910

10-
Client.call ~headers ~body (Code.method_of_string "POST") uri
11+
Client.call ~headers ~body `POST uri
1112
>>= fun (res, body_stream) ->
1213
(* Do stuff with the result *)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
56
let headers = Header.init ()
67
|> fun h -> Header.add h "content-type" "application/json"
78
in
8-
let body = "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }" in
9+
let body = Cohttp_lwt_body.of_string "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }" in
910

10-
Client.call ~headers ~body (Code.method_of_string "POST") uri
11+
Client.call ~headers ~body `POST uri
1112
>>= fun (res, body_stream) ->
1213
(* Do stuff with the result *)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
56
let headers = Header.init ()
67
|> fun h -> Header.add h "cookie" "foo=bar; bar=baz"
78
in
89

9-
Client.call ~headers (Code.method_of_string "POST") uri
10+
Client.call ~headers `POST uri
1011
>>= fun (res, body_stream) ->
1112
(* Do stuff with the result *)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value" in
@@ -7,8 +8,8 @@ let headers = Header.init ()
78
|> fun h -> Header.add h "accept" "application/json"
89
|> fun h -> Header.add h "content-type" "application/x-www-form-urlencoded"
910
in
10-
let body = "foo=bar" in
11+
let body = Cohttp_lwt_body.of_string "foo=bar" in
1112

12-
Client.call ~headers ~body (Code.method_of_string "POST") uri
13+
Client.call ~headers ~body `POST uri
1314
>>= fun (res, body_stream) ->
1415
(* Do stuff with the result *)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
@@ -7,6 +8,6 @@ let headers = Header.init ()
78
|> fun h -> Header.add h "x-foo" "Bar"
89
in
910

10-
Client.call ~headers (Code.method_of_string "GET") uri
11+
Client.call ~headers `GET uri
1112
>>= fun (res, body_stream) ->
1213
(* Do stuff with the result *)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
56
let headers = Header.init ()
67
|> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001"
78
in
8-
let body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--" in
9+
let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--" in
910

10-
Client.call ~headers ~body (Code.method_of_string "POST") uri
11+
Client.call ~headers ~body `POST uri
1112
>>= fun (res, body_stream) ->
1213
(* Do stuff with the result *)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
56
let headers = Header.init ()
67
|> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001"
78
in
8-
let body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--" in
9+
let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--" in
910

10-
Client.call ~headers ~body (Code.method_of_string "POST") uri
11+
Client.call ~headers ~body `POST uri
1112
>>= fun (res, body_stream) ->
1213
(* Do stuff with the result *)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har" in
56
let headers = Header.init ()
67
|> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001"
78
in
8-
let body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--" in
9+
let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--" in
910

10-
Client.call ~headers ~body (Code.method_of_string "POST") uri
11+
Client.call ~headers ~body `POST uri
1112
>>= fun (res, body_stream) ->
1213
(* Do stuff with the result *)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
open Cohttp_lwt_unix
2+
open Cohttp
23
open Lwt
34

45
let uri = Uri.of_string "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value" in
56

6-
Client.call (Code.method_of_string "GET") uri
7+
Client.call `GET uri
78
>>= fun (res, body_stream) ->
89
(* Do stuff with the result *)

0 commit comments

Comments
 (0)