diff --git a/src/targets/ocaml/cohttp.js b/src/targets/ocaml/cohttp.js index c913a0d3c..06be0f417 100644 --- a/src/targets/ocaml/cohttp.js +++ b/src/targets/ocaml/cohttp.js @@ -7,9 +7,11 @@ module.exports = function (source, options) { indent: ' ' }, options) + var methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options'] var code = [] code.push('open Cohttp_lwt_unix') + code.push('open Cohttp') code.push('open Lwt') code.push('') @@ -32,16 +34,16 @@ module.exports = function (source, options) { // Add body if (source.postData.text) { // Just text - code.push(util.format('let body = %s in', JSON.stringify(source.postData.text))) + code.push(util.format('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text))) } // Do the request code.push('') - code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri', + code.push(util.format('Client.call %s%s%s uri', headers.length ? '~headers ' : '', source.postData.text ? '~body ' : '', - source.method + (methods.indexOf(this.source.method.toLowerCase()) >= 0 ? ('`' + this.source.method.toUpperCase()) : '(Code.method_of_string "' + this.source.method + '")') )) // Catch result diff --git a/test/fixtures/output/ocaml/cohttp/application-form-encoded.ml b/test/fixtures/output/ocaml/cohttp/application-form-encoded.ml index bca4c1bd1..866d83fe7 100644 --- a/test/fixtures/output/ocaml/cohttp/application-form-encoded.ml +++ b/test/fixtures/output/ocaml/cohttp/application-form-encoded.ml @@ -1,12 +1,13 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in let headers = Header.init () |> fun h -> Header.add h "content-type" "application/x-www-form-urlencoded" in -let body = "foo=bar&hello=world" in +let body = Cohttp_lwt_body.of_string "foo=bar&hello=world" in -Client.call ~headers ~body (Code.method_of_string "POST") uri +Client.call ~headers ~body `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/application-json.ml b/test/fixtures/output/ocaml/cohttp/application-json.ml index 69008081b..aea3469a0 100644 --- a/test/fixtures/output/ocaml/cohttp/application-json.ml +++ b/test/fixtures/output/ocaml/cohttp/application-json.ml @@ -1,12 +1,13 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in let headers = Header.init () |> fun h -> Header.add h "content-type" "application/json" in -let body = "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }" in +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 -Client.call ~headers ~body (Code.method_of_string "POST") uri +Client.call ~headers ~body `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/cookies.ml b/test/fixtures/output/ocaml/cohttp/cookies.ml index 7cb0f9fdd..725e2f657 100644 --- a/test/fixtures/output/ocaml/cohttp/cookies.ml +++ b/test/fixtures/output/ocaml/cohttp/cookies.ml @@ -1,4 +1,5 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in @@ -6,6 +7,6 @@ let headers = Header.init () |> fun h -> Header.add h "cookie" "foo=bar; bar=baz" in -Client.call ~headers (Code.method_of_string "POST") uri +Client.call ~headers `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/full.ml b/test/fixtures/output/ocaml/cohttp/full.ml index 87f1fc16f..7514dd2ba 100644 --- a/test/fixtures/output/ocaml/cohttp/full.ml +++ b/test/fixtures/output/ocaml/cohttp/full.ml @@ -1,4 +1,5 @@ open Cohttp_lwt_unix +open Cohttp open Lwt 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 () |> fun h -> Header.add h "accept" "application/json" |> fun h -> Header.add h "content-type" "application/x-www-form-urlencoded" in -let body = "foo=bar" in +let body = Cohttp_lwt_body.of_string "foo=bar" in -Client.call ~headers ~body (Code.method_of_string "POST") uri +Client.call ~headers ~body `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/headers.ml b/test/fixtures/output/ocaml/cohttp/headers.ml index d13f5c8f4..c0d8f02a2 100644 --- a/test/fixtures/output/ocaml/cohttp/headers.ml +++ b/test/fixtures/output/ocaml/cohttp/headers.ml @@ -1,4 +1,5 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in @@ -7,6 +8,6 @@ let headers = Header.init () |> fun h -> Header.add h "x-foo" "Bar" in -Client.call ~headers (Code.method_of_string "GET") uri +Client.call ~headers `GET uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/multipart-data.ml b/test/fixtures/output/ocaml/cohttp/multipart-data.ml index fdeda971c..de7a3b291 100644 --- a/test/fixtures/output/ocaml/cohttp/multipart-data.ml +++ b/test/fixtures/output/ocaml/cohttp/multipart-data.ml @@ -1,12 +1,13 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in let headers = Header.init () |> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001" in -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 +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 -Client.call ~headers ~body (Code.method_of_string "POST") uri +Client.call ~headers ~body `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/multipart-file.ml b/test/fixtures/output/ocaml/cohttp/multipart-file.ml index 04653d20c..9eb3fbaab 100644 --- a/test/fixtures/output/ocaml/cohttp/multipart-file.ml +++ b/test/fixtures/output/ocaml/cohttp/multipart-file.ml @@ -1,12 +1,13 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in let headers = Header.init () |> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001" in -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 +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 -Client.call ~headers ~body (Code.method_of_string "POST") uri +Client.call ~headers ~body `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/multipart-form-data.ml b/test/fixtures/output/ocaml/cohttp/multipart-form-data.ml index 607c6c5ec..b6b5e9ba6 100644 --- a/test/fixtures/output/ocaml/cohttp/multipart-form-data.ml +++ b/test/fixtures/output/ocaml/cohttp/multipart-form-data.ml @@ -1,12 +1,13 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in let headers = Header.init () |> fun h -> Header.add h "content-type" "multipart/form-data; boundary=---011000010111000001101001" in -let body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--" in +let body = Cohttp_lwt_body.of_string "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--" in -Client.call ~headers ~body (Code.method_of_string "POST") uri +Client.call ~headers ~body `POST uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/query.ml b/test/fixtures/output/ocaml/cohttp/query.ml index 508836681..8634f1ab1 100644 --- a/test/fixtures/output/ocaml/cohttp/query.ml +++ b/test/fixtures/output/ocaml/cohttp/query.ml @@ -1,8 +1,9 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value" in -Client.call (Code.method_of_string "GET") uri +Client.call `GET uri >>= fun (res, body_stream) -> (* Do stuff with the result *) diff --git a/test/fixtures/output/ocaml/cohttp/short.ml b/test/fixtures/output/ocaml/cohttp/short.ml index ad85a96fb..189bc3f53 100644 --- a/test/fixtures/output/ocaml/cohttp/short.ml +++ b/test/fixtures/output/ocaml/cohttp/short.ml @@ -1,8 +1,9 @@ open Cohttp_lwt_unix +open Cohttp open Lwt let uri = Uri.of_string "http://mockbin.com/har" in -Client.call (Code.method_of_string "GET") uri +Client.call `GET uri >>= fun (res, body_stream) -> (* Do stuff with the result *)