Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/targets/ocaml/cohttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/output/ocaml/cohttp/application-form-encoded.ml
Original file line number Diff line number Diff line change
@@ -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 *)
5 changes: 3 additions & 2 deletions test/fixtures/output/ocaml/cohttp/application-json.ml
Original file line number Diff line number Diff line change
@@ -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 *)
3 changes: 2 additions & 1 deletion test/fixtures/output/ocaml/cohttp/cookies.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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 "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 *)
5 changes: 3 additions & 2 deletions test/fixtures/output/ocaml/cohttp/full.ml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 *)
3 changes: 2 additions & 1 deletion test/fixtures/output/ocaml/cohttp/headers.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Cohttp_lwt_unix
open Cohttp
open Lwt

let uri = Uri.of_string "http://mockbin.com/har" in
Expand All @@ -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 *)
5 changes: 3 additions & 2 deletions test/fixtures/output/ocaml/cohttp/multipart-data.ml
Original file line number Diff line number Diff line change
@@ -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 *)
5 changes: 3 additions & 2 deletions test/fixtures/output/ocaml/cohttp/multipart-file.ml
Original file line number Diff line number Diff line change
@@ -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 *)
5 changes: 3 additions & 2 deletions test/fixtures/output/ocaml/cohttp/multipart-form-data.ml
Original file line number Diff line number Diff line change
@@ -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 *)
3 changes: 2 additions & 1 deletion test/fixtures/output/ocaml/cohttp/query.ml
Original file line number Diff line number Diff line change
@@ -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 *)
3 changes: 2 additions & 1 deletion test/fixtures/output/ocaml/cohttp/short.ml
Original file line number Diff line number Diff line change
@@ -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 *)