Skip to content
Closed
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
136 changes: 136 additions & 0 deletions src/targets/elixir/httpoison.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* @description
* HTTPoison Elixir HTTP client code snippet generator.
*
* @author
* @danirukun
*
* For any questions or issues regarding the generated code snippet,
* please open an issue mentioning the author.
*/

'use strict'

const CodeBuilder = require('../../helpers/code-builder')

/**
Converts an ES6 list of objects to an Elixir KW list
*/
const jsListToKwList = (jsList) => {
if (jsList.length === 0) return '[]'

const kwList = jsList
.map(obj => `{"${obj.name}", "${obj.value}"}`)
.join(', ')
return `[${kwList}]`
}

/**
Converts a list containing cookies objects into an
HTTPoison `cookie` parameter argument.
*/
const jsCookiesToExBin = (jsCookiesList) => {
if (jsCookiesList.length === 0) return '""'

const exBinary = jsCookiesList
.map(cookie => `${cookie.name}=${cookie.value}`)
.join('; ')
return `"${exBinary}"`
}

/**
Converts a list of params to a Poison multipart form payload.
*/
const jsMultiPartParamsToPoisonPayload = (jsMultiParams) => {
const [params] = jsMultiParams
const isFileUpload = 'fileName' in params
let multiPart

if (isFileUpload) {
const { fileName, name } = params
const formData = [
{ name: 'name', value: name },
{ name: 'filename', value: fileName }]
const formDataKwList = jsListToKwList(formData)
const headers = jsListToKwList([{ name: 'content-type', value: 'multipart/form-data' }])

multiPart = `[{:file, "${fileName}", {"form-data", ${formDataKwList}}, ${headers}}]`
} else {
multiPart = jsListToKwList(jsMultiParams)
}

return `{:multipart, ${multiPart}}`
}

const isJson = (mimeType) => {
return [
'application/json',
'application/x-json',
'text/json'
].includes(mimeType)
}

const isMultiPartForm = (mimeType) => {
return mimeType === 'multipart/form-data'
}

const escapeExString = (text) => {
return `~s(${text})`
}

module.exports = (source, _options) => {
const code = new CodeBuilder()

const { method, headers, cookies, postData: { mimeType, params } } = source
const text = `"${(source.postData.text || '')}"`
const escapedText = isJson(mimeType)
? escapeExString(text)
: text
const payload = isMultiPartForm(mimeType)
? jsMultiPartParamsToPoisonPayload(params)
: escapedText

function appendHeaders (headers) {
const exHeaders = jsListToKwList(headers)

argPlaceholders += ',\n %s'
poisonArgs = poisonArgs.concat([exHeaders])
}

function appendCookies (cookies) {
const exCookies = jsCookiesToExBin(cookies)
const poisonOpts = `hackney: [cookie: ${exCookies}]`

argPlaceholders += ',\n %s'
poisonArgs = poisonArgs.concat([poisonOpts])
}

function appendPayload (payload) {
argPlaceholders += ',\n %s'
poisonArgs = poisonArgs.concat([payload])
}

let argPlaceholders = '\n :%s,\n "%s"'
let poisonArgs = [method.toLowerCase(), source.fullUrl]

appendPayload(payload)

if (headers.length > 0) appendHeaders(headers)
if (cookies.length > 0) {
if (headers.length === 0) appendHeaders([])
appendCookies(cookies)
}

argPlaceholders += '\n'

code.push(`HTTPoison.request(${argPlaceholders})`, ...poisonArgs)

return code.join()
}

module.exports.info = {
key: 'httpoison',
title: 'HTTPoison',
link: 'https://github.com/edgurgel/httpoison',
description: 'HTTP client for Elixir, based on HTTPotion.'
}
11 changes: 11 additions & 0 deletions src/targets/elixir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

module.exports = {
info: {
key: 'elixir',
title: 'Elixir',
extname: '.ex',
default: 'httpoison'
},
httpoison: require('./httpoison')
}
1 change: 1 addition & 0 deletions src/targets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
c: require('./c'),
clojure: require('./clojure'),
csharp: require('./csharp'),
elixir: require('./elixir'),
go: require('./go'),
http: require('./http'),
java: require('./java'),
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/available-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@
"link": "http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html",
"description": "Ruby HTTP client"
}
]
},
{
"key": "elixir",
"title": "Elixir",
"extname": ".ex",
"default": "httpoison",
"clients": [
{
"key": "httpoison",
"title": "HTTPoison",
"link": "https://github.com/edgurgel/httpoison",
"description": "HTTP client for Elixir, based on HTTPotion."
}
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
"foo=bar&hello=world",
[{"content-type", "application/x-www-form-urlencoded"}]
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/application-json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
~s("{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}"),
[{"content-type", "application/json"}]
)
7 changes: 7 additions & 0 deletions test/fixtures/output/elixir/httpoison/cookies.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
"",
[],
hackney: [cookie: "foo=bar; bar=baz"]
)
5 changes: 5 additions & 0 deletions test/fixtures/output/elixir/httpoison/custom-method.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTPoison.request(
:propfind,
"http://mockbin.com/har",
""
)
7 changes: 7 additions & 0 deletions test/fixtures/output/elixir/httpoison/full.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTTPoison.request(
:post,
"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value",
"foo=bar",
[{"accept", "application/json"}, {"content-type", "application/x-www-form-urlencoded"}],
hackney: [cookie: "foo=bar; bar=baz"]
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/headers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:get,
"http://mockbin.com/har",
"",
[{"accept", "application/json"}, {"x-foo", "Bar"}]
)
5 changes: 5 additions & 0 deletions test/fixtures/output/elixir/httpoison/https.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTPoison.request(
:get,
"https://mockbin.com/har",
""
)
8 changes: 8 additions & 0 deletions test/fixtures/output/elixir/httpoison/jsonObj-multiline.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
~s("{
"foo": "bar"
}"),
[{"content-type", "application/json"}]
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/jsonObj-null-value.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
~s("{"foo":null}"),
[{"content-type", "application/json"}]
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/multipart-data.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
{:multipart, [{:file, "hello.txt", {"form-data", [{"name", "foo"}, {"filename", "hello.txt"}]}, [{"content-type", "multipart/form-data"}]}]},
[{"content-type", "multipart/form-data"}]
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/multipart-file.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
{:multipart, [{:file, "test/fixtures/files/hello.txt", {"form-data", [{"name", "foo"}, {"filename", "test/fixtures/files/hello.txt"}]}, [{"content-type", "multipart/form-data"}]}]},
[{"content-type", "multipart/form-data"}]
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/multipart-form-data.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
{:multipart, [{"foo", "bar"}]},
[{"Content-Type", "multipart/form-data"}]
)
5 changes: 5 additions & 0 deletions test/fixtures/output/elixir/httpoison/nested.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTPoison.request(
:get,
"http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value",
""
)
5 changes: 5 additions & 0 deletions test/fixtures/output/elixir/httpoison/query.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTPoison.request(
:get,
"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value",
""
)
5 changes: 5 additions & 0 deletions test/fixtures/output/elixir/httpoison/short.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTPoison.request(
:get,
"http://mockbin.com/har",
""
)
6 changes: 6 additions & 0 deletions test/fixtures/output/elixir/httpoison/text-plain.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTPoison.request(
:post,
"http://mockbin.com/har",
"Hello World",
[{"content-type", "text/plain"}]
)
3 changes: 3 additions & 0 deletions test/targets/elixir/httpoison.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = function (snippet, fixtures) {}