-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from tggreene/add-clojure-generator
Add clojure generator
- Loading branch information
Showing
19 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"bin": "bin/httpsnippet", | ||
"keywords": [ | ||
"api", | ||
"clojure", | ||
"csharp", | ||
"curl", | ||
"go", | ||
|
This file contains 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,151 @@ | ||
/** | ||
* @description | ||
* HTTP code snippet generator for Clojure using clj-http. | ||
* | ||
* @author | ||
* @tggreene | ||
* | ||
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. | ||
*/ | ||
|
||
'use strict' | ||
|
||
var CodeBuilder = require('../../helpers/code-builder') | ||
|
||
var Keyword = function (name) { | ||
this.name = name | ||
} | ||
|
||
Keyword.prototype.toString = function () { | ||
return ':' + this.name | ||
} | ||
|
||
var File = function (path) { | ||
this.path = path | ||
} | ||
|
||
File.prototype.toString = function () { | ||
return '(clojure.java.io/file "' + this.path + '")' | ||
} | ||
|
||
var jsType = function (x) { | ||
return (typeof x !== 'undefined') | ||
? x.constructor.name.toLowerCase() | ||
: null | ||
} | ||
|
||
var objEmpty = function (x) { | ||
return (jsType(x) === 'object') | ||
? Object.keys(x).length === 0 | ||
: false | ||
} | ||
|
||
var filterEmpty = function (m) { | ||
Object.keys(m) | ||
.filter(function (x) { return objEmpty(m[x]) }) | ||
.forEach(function (x) { delete m[x] }) | ||
return m | ||
} | ||
|
||
var padBlock = function (x, s) { | ||
var padding = Array.apply(null, Array(x)) | ||
.map(function (_) { | ||
return ' ' | ||
}) | ||
.join('') | ||
return s.replace(/\n/g, '\n' + padding) | ||
} | ||
|
||
var jsToEdn = function (js) { | ||
switch (jsType(js)) { | ||
default: // 'number' 'boolean' | ||
return js.toString() | ||
case 'string': | ||
return '"' + js.replace(/\"/g, '\\"') + '"' | ||
case 'file': | ||
return js.toString() | ||
case 'keyword': | ||
return js.toString() | ||
case 'null': | ||
return 'nil' | ||
case 'regexp': | ||
return '#"' + js.source + '"' | ||
case 'object': // simple vertical format | ||
var obj = Object.keys(js) | ||
.reduce(function (acc, key) { | ||
var val = padBlock(key.length + 2, jsToEdn(js[key])) | ||
return acc + ':' + key + ' ' + val + '\n ' | ||
}, '') | ||
.trim() | ||
return '{' + padBlock(1, obj) + '}' | ||
case 'array': // simple horizontal format | ||
var arr = js.reduce(function (acc, val) { | ||
return acc + ' ' + jsToEdn(val) | ||
}, '').trim() | ||
return '[' + padBlock(1, arr) + ']' | ||
} | ||
} | ||
|
||
module.exports = function (source, options) { | ||
var code = new CodeBuilder(options) | ||
var methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'] | ||
|
||
if (methods.indexOf(source.method.toLowerCase()) === -1) { | ||
return code.push('Method not supported').join() | ||
} | ||
|
||
var params = {headers: source.allHeaders, | ||
'query-params': source.queryObj} | ||
|
||
switch (source.postData.mimeType) { | ||
case 'application/json': | ||
params['content-type'] = new Keyword('json') | ||
params['form-params'] = source.postData.jsonObj | ||
delete params.headers['content-type'] | ||
break | ||
case 'application/x-www-form-urlencoded': | ||
params['form-params'] = source.postData.paramsObj | ||
delete params.headers['content-type'] | ||
break | ||
case 'text/plain': | ||
params.body = source.postData.text | ||
delete params.headers['content-type'] | ||
break | ||
case 'multipart/form-data': | ||
params.multipart = source.postData.params.map(function (x) { | ||
if (x.fileName && !x.value) { | ||
return {name: x.name, | ||
content: new File(x.fileName)} | ||
} else { | ||
return {name: x.name, | ||
content: x.value} | ||
} | ||
}) | ||
delete params.headers['content-type'] | ||
break | ||
} | ||
|
||
switch (params.headers.accept) { | ||
case 'application/json': | ||
params.accept = new Keyword('json') | ||
delete params.headers.accept | ||
break | ||
} | ||
|
||
code.push('(require \'[clj-http.client :as client])\n') | ||
|
||
if (objEmpty(filterEmpty(params))) { | ||
code.push('(client/%s "%s")', source.method.toLowerCase(), source.url) | ||
} else { | ||
code.push('(client/%s "%s" %s)', source.method.toLowerCase(), source.url, padBlock(11 + source.method.length + source.url.length, jsToEdn(filterEmpty(params)))) | ||
} | ||
|
||
return code.join() | ||
} | ||
|
||
module.exports.info = { | ||
key: 'clj_http', | ||
title: 'clj-http', | ||
link: 'https://github.com/dakrone/clj-http', | ||
description: 'An idiomatic clojure http client wrapping the apache client.' | ||
} |
This file contains 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 = { | ||
info: { | ||
key: 'clojure', | ||
title: 'Clojure', | ||
extname: '.clj', | ||
default: 'clj_http' | ||
}, | ||
|
||
clj_http: require('./clj_http') | ||
} |
This file contains 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 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
4 changes: 4 additions & 0 deletions
4
test/fixtures/output/clojure/clj_http/application-form-encoded.clj
This file contains 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,4 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:form-params {:foo "bar" | ||
:hello "world"}}) |
This file contains 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,9 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:content-type :json | ||
:form-params {:number 1 | ||
:string "f\"oo" | ||
:arr [1 2 3] | ||
:nested {:a "b"} | ||
:arr_mix [1 "a" {:arr_mix_nested {}}] | ||
:boolean false}}) |
This file contains 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,3 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:headers {:cookie "foo=bar; bar=baz"}}) |
This file contains 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 @@ | ||
Method not supported |
This file contains 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,8 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:headers {:cookie "foo=bar; bar=baz"} | ||
:query-params {:foo ["bar" "baz"] | ||
:baz "abc" | ||
:key "value"} | ||
:form-params {:foo "bar"} | ||
:accept :json}) |
This file contains 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,4 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/get "http://mockbin.com/har" {:headers {:x-foo "Bar"} | ||
:accept :json}) |
This file contains 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,3 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/get "https://mockbin.com/har") |
This file contains 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,4 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:multipart [{:name "foo" | ||
:content "Hello World"}]}) |
This file contains 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,4 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:multipart [{:name "foo" | ||
:content (clojure.java.io/file "test/fixtures/files/hello.txt")}]}) |
4 changes: 4 additions & 0 deletions
4
test/fixtures/output/clojure/clj_http/multipart-form-data.clj
This file contains 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,4 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:multipart [{:name "foo" | ||
:content "bar"}]}) |
This file contains 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,5 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/get "http://mockbin.com/har" {:query-params {:foo ["bar" "baz"] | ||
:baz "abc" | ||
:key "value"}}) |
This file contains 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,3 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/get "http://mockbin.com/har") |
This file contains 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,3 @@ | ||
(require '[clj-http.client :as client]) | ||
|
||
(client/post "http://mockbin.com/har" {:body "Hello World"}) |
This file contains 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,5 @@ | ||
'use strict' | ||
|
||
require('should') | ||
|
||
module.exports = function (snippet, fixtures) {} |