-
Notifications
You must be signed in to change notification settings - Fork 241
Added OCaml #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added OCaml #1
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 'use strict'; | ||
|
|
||
| var util = require('util'); | ||
|
|
||
| module.exports = function (options) { | ||
| var opts = util._extend({ | ||
| indent: ' ' | ||
| }, options); | ||
|
|
||
| var code = []; | ||
|
|
||
| code.push('open Cohttp_lwt_unix'); | ||
| code.push('open Lwt'); | ||
| code.push(''); | ||
|
|
||
| // Create URI | ||
| code.push(util.format('let uri = Uri.of_string "%s" in', this.source.fullUrl)); | ||
|
|
||
| // Add headers, including the cookies | ||
| var headersPresent = this.source.headers && this.source.headers.length; | ||
| var cookiesPresent = this.source.cookies && this.source.cookies.length; | ||
|
|
||
| if (headersPresent || cookiesPresent) { | ||
| code.push('let headers = Header.init ()'); | ||
|
|
||
| if (headersPresent) { | ||
| for (var header in this.source.headers) { | ||
| code.push(util.format(opts.indent + '|> fun h -> Header.add h "%s" "%s"', this.source.headers[header].name, this.source.headers[header].value)); | ||
| } | ||
| } | ||
|
|
||
| if (cookiesPresent) { | ||
| var cookie = this.source.cookies.map(function(c){return c.name + '=' + c.value;}).join('; '); | ||
| code.push(util.format(opts.indent + '|> fun h -> Header.add h "Cookie" "%s"', cookie)); | ||
| } | ||
| code.push('in'); | ||
| } | ||
|
|
||
| // Add body | ||
| var bodyPresent = this.source.postData && this.source.postData.text; | ||
| if (bodyPresent) { | ||
| //Just text | ||
| code.push(util.format('let body = %s in', JSON.stringify(this.source.postData.text))); | ||
| } else if (this.source.postData && !this.source.postData.text && this.source.postData.params && this.source.postData.params.length) { | ||
| // Post params | ||
| bodyPresent = true; | ||
| var body = this.source.postData.params.map(function(p){return p.name + '=' + p.value;}).join('&'); | ||
| code.push(util.format('let body = "%s" in', body)); | ||
| } | ||
|
|
||
| // Do the request | ||
| code.push(''); | ||
| code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri', | ||
| (headersPresent || cookiesPresent) ? "~headers " : "", | ||
| bodyPresent ? "~body " : "", | ||
| this.source.method | ||
| )); | ||
|
|
||
| // Catch result | ||
| code.push('>>= fun (res, body_stream) ->'); | ||
| code.push(opts.indent + '(* Do stuff with the result *)'); | ||
|
|
||
| return code.join('\n'); | ||
| }; | ||
|
|
||
| module.exports.info = function () { | ||
| return { | ||
| family: 'ocaml', | ||
| key: 'cohttp', | ||
| title: 'OCaml', | ||
| link: 'https://github.com/mirage/ocaml-cohttp', | ||
| description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml', | ||
| extname: '.ml' | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = require('requireindex')(__dirname); | ||
|
|
||
| module.exports._familyInfo = function () { | ||
| return { | ||
| key: 'ocaml', | ||
| title: 'OCaml', | ||
| extname: '.ml', | ||
| default: 'cohttp' | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 'use strict'; | ||
|
|
||
| var fixtures = require('../../fixtures'); | ||
| var HTTPSnippet = require('../../../src'); | ||
| var should = require('should'); | ||
|
|
||
| describe('OCaml', function () { | ||
| it('should convert full request to an OCaml snippet', function (done) { | ||
| var result = new HTTPSnippet(fixtures.full).convert('ocaml', 'cohttp', { | ||
| indent: '\t' | ||
| }); | ||
|
|
||
| result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug?baz=abc&foo=bar&foo=baz" inlet headers = Header.init ()\t|> fun h -> Header.add h "Accept" "text/plain"\t|> fun h -> Header.add h "Content-Type" "application/json"\t|> fun h -> Header.add h "X-Pretty-Print" "2"\t|> fun h -> Header.add h "Cookie" "foo=bar; bar=baz"inlet body = "{\\"foo\\": \\"bar\\"}" inClient.call ~headers ~body (Code.method_of_string "POST") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)'); | ||
|
|
||
| done(); | ||
| }); | ||
|
|
||
| it('should convert query request to an OCaml snippet', function (done) { | ||
| var result = new HTTPSnippet(fixtures.query).convert('ocaml', 'cohttp', { | ||
| indent: '\t' | ||
| }); | ||
|
|
||
| result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug?key=value&baz=abc&foo=bar&foo=baz" inClient.call (Code.method_of_string "POST") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)'); | ||
|
|
||
| done(); | ||
| }); | ||
|
|
||
| it('should convert simple request to an OCaml snippet', function (done) { | ||
| var result = new HTTPSnippet(fixtures.simple).convert('ocaml', 'cohttp', { | ||
| indent: '\t' | ||
| }); | ||
|
|
||
| result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug?foo=bar" inlet headers = Header.init ()\t|> fun h -> Header.add h "Content-Type" "application/json"\t|> fun h -> Header.add h "Cookie" "bar=baz"inlet body = "{\\"foo\\": \\"bar\\"}" inClient.call ~headers ~body (Code.method_of_string "POST") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)'); | ||
|
|
||
| done(); | ||
| }); | ||
|
|
||
| it('should convert short request to an OCaml snippet', function (done) { | ||
| var result = new HTTPSnippet(fixtures.short).convert('ocaml', 'cohttp', { | ||
| indent: '\t' | ||
| }); | ||
|
|
||
| result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/echo" inClient.call (Code.method_of_string "GET") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)'); | ||
|
|
||
| done(); | ||
| }); | ||
|
|
||
| it('should convert http1 request to an OCaml snippet', function (done) { | ||
| var result = new HTTPSnippet(fixtures.http1).convert('ocaml', 'cohttp', { | ||
| indent: '\t' | ||
| }); | ||
|
|
||
| result.replace(/\n/g, '').should.eql('open Cohttp_lwt_unixopen Lwtlet uri = Uri.of_string "http://httpconsole.com/debug" inClient.call (Code.method_of_string "GET") uri>>= fun (res, body_stream) ->\t(* Do stuff with the result *)'); | ||
|
|
||
| done(); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this anymore, checkout the most recent update:
v1.0.0-beta.6