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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"har",
"http",
"httpie",
"httr",
"java",
"javascript",
"jquery",
Expand Down
1 change: 1 addition & 0 deletions src/targets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
php: require('./php'),
powershell: require('./powershell'),
python: require('./python'),
r: require('./r'),
ruby: require('./ruby'),
shell: require('./shell'),
swift: require('./swift')
Expand Down
154 changes: 154 additions & 0 deletions src/targets/r/httr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* @description
* HTTP code snippet generator for R using httr
*
* @author
* @gabrielakoreeda
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/

'use strict'

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

module.exports = function (source, options) {
// Start snippet
var code = new CodeBuilder()

// Import httr
code.push('library(httr)')
.blank()

// Set URL
code.push('url <- "%s"', source.url)
.blank()

// Construct query string
var query
var qs = source.queryObj
var queryCount = Object.keys(qs).length
delete source.queryObj['key']

if (source.queryString.length === 1) {
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString())
.blank()
} else if (source.queryString.length > 1) {
var count = 1

code.push('queryString <- list(')

for (query in qs) {
if (count++ !== queryCount - 1) {
code.push(' %s = "%s",', query, qs[query].toString())
} else {
code.push(' %s = "%s"', query, qs[query].toString())
}
}

code.push(')')
.blank()
}

// Construct payload
var payload = JSON.stringify(source.postData.text)

if (payload) {
code.push('payload <- %s', payload)
.blank()
}

// Define encode
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
switch (source.postData.mimeType) {
case 'application/x-www-form-urlencoded':
code.push('encode <- "form"')
.blank()
break

case 'application/json':
code.push('encode <- "json"')
.blank()
break

case 'multipart/form-data':
code.push('encode <- "multipart"')
.blank()
break

default:
code.push('encode <- "raw"')
.blank()
break
}
}

// Construct headers
var head
var headers = source.allHeaders
var headerCount = Object.keys(headers).length
var header = ''
var cookies
var accept

for (head in headers) {
if (head === 'accept') {
accept = ', accept("' + headers[head] + '")'
headerCount = headerCount - 1
} else if (head === 'cookie') {
cookies = ', set_cookies(`' + headers[head].replace(/;/g, '", `').replace(/` /g, '`').replace(/=/g, '` = "') + '")'
headerCount = headerCount - 1
} else if (head !== 'content-type') {
header = header + head.replace('-', '_') + " = '" + headers[head]
if (headerCount > 1) { header = header + "', " }
}
}

// Construct request
var method = source.method
var request = util.format('response <- VERB("%s", url', method)

if (payload) {
request += ', body = payload'
}

if (header !== '') {
request += ', add_headers(' + header + "')"
}

if (source.queryString.length) {
request += ', query = queryString'
}

request += ', content_type("' + source.postData.mimeType + '")'

if (typeof accept !== 'undefined') {
request += accept
}

if (typeof cookies !== 'undefined') {
request += cookies
}

if (source.postData.text || source.postData.jsonObj || source.postData.params) {
request += ', encode = encode'
}

request += ')'

code.push(request)
.blank()

// Print response
.push('content(response, "text")')

return code.join()
}

module.exports.info = {
key: 'httr',
title: 'httr',
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
description: 'httr: Tools for Working with URLs and HTTP'
}
12 changes: 12 additions & 0 deletions src/targets/r/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

module.exports = {
info: {
key: 'r',
title: 'R',
extname: '.r',
default: 'httr'
},

httr: require('./httr')
}
14 changes: 14 additions & 0 deletions test/fixtures/available-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@
}
]
},
{
"key": "r",
"title": "R",
"extname": ".r",
"default": "httr",
"clients": [
{
"key": "httr",
"title": "httr",
"link": "https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html",
"description": "httr: Tools for Working with URLs and HTTP"
}
]
},
{
"default": "webrequest",
"extname": ".ps1",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/application-form-encoded.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "foo=bar&hello=world"

encode <- "form"

response <- VERB("POST", url, body = payload, content_type("application/x-www-form-urlencoded"), encode = encode)

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/application-json.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"

encode <- "json"

response <- VERB("POST", url, body = payload, content_type("application/json"), encode = encode)

content(response, "text")
7 changes: 7 additions & 0 deletions test/fixtures/output/r/httr/cookies.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(httr)

url <- "http://mockbin.com/har"

response <- VERB("POST", url, content_type("application/octet-stream"), set_cookies(`foo` = "bar", `bar` = "baz"))

content(response, "text")
7 changes: 7 additions & 0 deletions test/fixtures/output/r/httr/custom-method.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(httr)

url <- "http://mockbin.com/har"

response <- VERB("PROPFIND", url, content_type("application/octet-stream"))

content(response, "text")
16 changes: 16 additions & 0 deletions test/fixtures/output/r/httr/full.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
library(httr)

url <- "http://mockbin.com/har"

queryString <- list(
foo = "bar,baz",
baz = "abc"
)

payload <- "foo=bar"

encode <- "form"

response <- VERB("POST", url, body = payload, query = queryString, content_type("application/x-www-form-urlencoded"), accept("application/json"), set_cookies(`foo` = "bar", `bar` = "baz"), encode = encode)

content(response, "text")
7 changes: 7 additions & 0 deletions test/fixtures/output/r/httr/headers.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(httr)

url <- "http://mockbin.com/har"

response <- VERB("GET", url, add_headers(x_foo = 'Bar'), content_type("application/octet-stream"), accept("application/json"))

content(response, "text")
7 changes: 7 additions & 0 deletions test/fixtures/output/r/httr/https.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(httr)

url <- "https://mockbin.com/har"

response <- VERB("GET", url, content_type("application/octet-stream"))

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/jsonObj-multiline.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "{\n \"foo\": \"bar\"\n}"

encode <- "json"

response <- VERB("POST", url, body = payload, content_type("application/json"), encode = encode)

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/jsonObj-null-value.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "{\"foo\":null}"

encode <- "json"

response <- VERB("POST", url, body = payload, content_type("application/json"), encode = encode)

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/multipart-data.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--\r\n"

encode <- "multipart"

response <- VERB("POST", url, body = payload, content_type("multipart/form-data"), encode = encode)

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/multipart-file.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n"

encode <- "multipart"

response <- VERB("POST", url, body = payload, content_type("multipart/form-data"), encode = encode)

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/multipart-form-data.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n"

encode <- "multipart"

response <- VERB("POST", url, body = payload, content_type("multipart/form-data"), encode = encode)

content(response, "text")
12 changes: 12 additions & 0 deletions test/fixtures/output/r/httr/query.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
library(httr)

url <- "http://mockbin.com/har"

queryString <- list(
foo = "bar,baz",
baz = "abc"
)

response <- VERB("GET", url, query = queryString, content_type("application/octet-stream"))

content(response, "text")
7 changes: 7 additions & 0 deletions test/fixtures/output/r/httr/short.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(httr)

url <- "http://mockbin.com/har"

response <- VERB("GET", url, content_type("application/octet-stream"))

content(response, "text")
11 changes: 11 additions & 0 deletions test/fixtures/output/r/httr/text-plain.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library(httr)

url <- "http://mockbin.com/har"

payload <- "Hello World"

encode <- "raw"

response <- VERB("POST", url, body = payload, content_type("text/plain"), encode = encode)

content(response, "text")
6 changes: 6 additions & 0 deletions test/targets/r/httr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

require('should')

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