@readme/httpsnippet 2.5.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -9
- package/package.json +24 -28
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +27 -25
- package/src/helpers/headers.js +10 -19
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +167 -146
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +18 -23
- package/src/targets/clojure/clj_http.js +103 -87
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +90 -90
- package/src/targets/csharp/index.js +3 -5
- package/src/targets/csharp/restsharp.js +20 -22
- package/src/targets/go/index.js +3 -5
- package/src/targets/go/native.js +46 -53
- package/src/targets/http/http1.1.js +26 -36
- package/src/targets/http/index.js +3 -5
- package/src/targets/index.js +2 -4
- package/src/targets/java/asynchttp.js +22 -23
- package/src/targets/java/index.js +3 -5
- package/src/targets/java/nethttp.js +20 -24
- package/src/targets/java/okhttp.js +31 -31
- package/src/targets/java/unirest.js +22 -19
- package/src/targets/javascript/axios.js +43 -38
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +46 -40
- package/src/targets/javascript/xhr.js +42 -38
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +33 -30
- package/src/targets/node/fetch.js +89 -83
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +36 -30
- package/src/targets/node/request.js +64 -64
- package/src/targets/node/unirest.js +56 -55
- package/src/targets/objc/helpers.js +24 -23
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +73 -45
- package/src/targets/ocaml/cohttp.js +29 -27
- package/src/targets/ocaml/index.js +3 -5
- package/src/targets/php/curl.js +86 -82
- package/src/targets/php/guzzle.js +135 -0
- package/src/targets/php/helpers.js +29 -28
- package/src/targets/php/http1.js +33 -44
- package/src/targets/php/http2.js +70 -71
- package/src/targets/php/index.js +5 -5
- package/src/targets/powershell/common.js +35 -30
- package/src/targets/powershell/index.js +3 -5
- package/src/targets/powershell/restmethod.js +3 -5
- package/src/targets/powershell/webrequest.js +3 -5
- package/src/targets/python/helpers.js +37 -34
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -44
- package/src/targets/python/requests.js +47 -72
- package/src/targets/r/httr.js +59 -70
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +39 -33
- package/src/targets/shell/curl.js +78 -51
- package/src/targets/shell/helpers.js +7 -9
- package/src/targets/shell/httpie.js +45 -39
- package/src/targets/shell/index.js +4 -5
- package/src/targets/shell/wget.js +20 -22
- package/src/targets/swift/helpers.js +35 -32
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +53 -39
- package/src/.DS_Store +0 -0
|
@@ -8,84 +8,73 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
module.exports = function (source, options) {
|
|
16
|
-
const code = new CodeBuilder()
|
|
13
|
+
module.exports = function (source) {
|
|
14
|
+
const code = new CodeBuilder();
|
|
17
15
|
// Start Request
|
|
18
|
-
code.push('import http.client')
|
|
19
|
-
.blank()
|
|
16
|
+
code.push('import http.client').blank();
|
|
20
17
|
|
|
21
18
|
// Check which protocol to be used for the client connection
|
|
22
|
-
const protocol = source.uriObj.protocol
|
|
19
|
+
const protocol = source.uriObj.protocol;
|
|
23
20
|
if (protocol === 'https:') {
|
|
24
|
-
code.push('conn = http.client.HTTPSConnection("%s")', source.uriObj.host)
|
|
25
|
-
.blank()
|
|
21
|
+
code.push('conn = http.client.HTTPSConnection("%s")', source.uriObj.host).blank();
|
|
26
22
|
} else {
|
|
27
|
-
code.push('conn = http.client.HTTPConnection("%s")', source.uriObj.host)
|
|
28
|
-
.blank()
|
|
23
|
+
code.push('conn = http.client.HTTPConnection("%s")', source.uriObj.host).blank();
|
|
29
24
|
}
|
|
30
25
|
|
|
31
26
|
// Create payload string if it exists
|
|
32
|
-
const payload = JSON.stringify(source.postData.text)
|
|
27
|
+
const payload = JSON.stringify(source.postData.text);
|
|
33
28
|
if (payload) {
|
|
34
|
-
code.push('payload = %s', payload)
|
|
35
|
-
.blank()
|
|
29
|
+
code.push('payload = %s', payload).blank();
|
|
36
30
|
}
|
|
37
31
|
|
|
38
32
|
// Create Headers
|
|
39
|
-
const headers = source.allHeaders
|
|
40
|
-
const headerCount = Object.keys(headers).length
|
|
33
|
+
const headers = source.allHeaders;
|
|
34
|
+
const headerCount = Object.keys(headers).length;
|
|
41
35
|
if (headerCount === 1) {
|
|
42
|
-
|
|
43
|
-
code.push('headers = { \'%s\': "%s" }', header, headers[header])
|
|
44
|
-
|
|
45
|
-
}
|
|
36
|
+
Object.keys(headers).forEach(header => {
|
|
37
|
+
code.push('headers = { \'%s\': "%s" }', header, headers[header]).blank();
|
|
38
|
+
});
|
|
46
39
|
} else if (headerCount > 1) {
|
|
47
|
-
let count = 1
|
|
40
|
+
let count = 1;
|
|
48
41
|
|
|
49
|
-
code.push('headers = {')
|
|
42
|
+
code.push('headers = {');
|
|
50
43
|
|
|
51
|
-
|
|
44
|
+
Object.keys(headers).forEach(header => {
|
|
45
|
+
// eslint-disable-next-line no-plusplus
|
|
52
46
|
if (count++ !== headerCount) {
|
|
53
|
-
code.push(' \'%s\': "%s",', header, headers[header])
|
|
47
|
+
code.push(' \'%s\': "%s",', header, headers[header]);
|
|
54
48
|
} else {
|
|
55
|
-
code.push(' \'%s\': "%s"', header, headers[header])
|
|
49
|
+
code.push(' \'%s\': "%s"', header, headers[header]);
|
|
56
50
|
}
|
|
57
|
-
}
|
|
51
|
+
});
|
|
58
52
|
|
|
59
|
-
code.push(' }')
|
|
60
|
-
.blank()
|
|
53
|
+
code.push(' }').blank();
|
|
61
54
|
}
|
|
62
55
|
|
|
63
56
|
// Make Request
|
|
64
|
-
const method = source.method
|
|
65
|
-
const path = source.uriObj.path
|
|
57
|
+
const method = source.method;
|
|
58
|
+
const path = source.uriObj.path;
|
|
66
59
|
if (payload && headerCount) {
|
|
67
|
-
code.push('conn.request("%s", "%s", payload, headers)', method, path)
|
|
60
|
+
code.push('conn.request("%s", "%s", payload, headers)', method, path);
|
|
68
61
|
} else if (payload && !headerCount) {
|
|
69
|
-
code.push('conn.request("%s", "%s", payload)', method, path)
|
|
62
|
+
code.push('conn.request("%s", "%s", payload)', method, path);
|
|
70
63
|
} else if (!payload && headerCount) {
|
|
71
|
-
code.push('conn.request("%s", "%s", headers=headers)', method, path)
|
|
64
|
+
code.push('conn.request("%s", "%s", headers=headers)', method, path);
|
|
72
65
|
} else {
|
|
73
|
-
code.push('conn.request("%s", "%s")', method, path)
|
|
66
|
+
code.push('conn.request("%s", "%s")', method, path);
|
|
74
67
|
}
|
|
75
68
|
|
|
76
69
|
// Get Response
|
|
77
|
-
code.blank()
|
|
78
|
-
.push('res = conn.getresponse()')
|
|
79
|
-
.push('data = res.read()')
|
|
80
|
-
.blank()
|
|
81
|
-
.push('print(data.decode("utf-8"))')
|
|
70
|
+
code.blank().push('res = conn.getresponse()').push('data = res.read()').blank().push('print(data.decode("utf-8"))');
|
|
82
71
|
|
|
83
|
-
return code.join()
|
|
84
|
-
}
|
|
72
|
+
return code.join();
|
|
73
|
+
};
|
|
85
74
|
|
|
86
75
|
module.exports.info = {
|
|
87
76
|
key: 'python3',
|
|
88
77
|
title: 'http.client',
|
|
89
78
|
link: 'https://docs.python.org/3/library/http.client.html',
|
|
90
|
-
description: 'Python3 HTTP Client'
|
|
91
|
-
}
|
|
79
|
+
description: 'Python3 HTTP Client',
|
|
80
|
+
};
|
|
@@ -8,119 +8,94 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
15
|
-
const helpers = require('./helpers')
|
|
11
|
+
const { format } = require('util');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
13
|
+
const helpers = require('./helpers');
|
|
16
14
|
|
|
17
15
|
module.exports = function (source, options) {
|
|
18
|
-
const opts =
|
|
16
|
+
const opts = {
|
|
19
17
|
indent: ' ',
|
|
20
|
-
pretty: true
|
|
21
|
-
|
|
18
|
+
pretty: true,
|
|
19
|
+
...options,
|
|
20
|
+
};
|
|
22
21
|
|
|
23
22
|
// Start snippet
|
|
24
|
-
const code = new CodeBuilder(opts.indent)
|
|
23
|
+
const code = new CodeBuilder(opts.indent);
|
|
25
24
|
|
|
26
25
|
// Import requests
|
|
27
|
-
code.push('import requests')
|
|
28
|
-
.blank()
|
|
26
|
+
code.push('import requests').blank();
|
|
29
27
|
|
|
30
28
|
// Set URL
|
|
31
|
-
code.push('url = "%s"', source.
|
|
32
|
-
.blank()
|
|
33
|
-
|
|
34
|
-
// Construct query string
|
|
35
|
-
let qs
|
|
36
|
-
if (Object.keys(source.queryObj).length) {
|
|
37
|
-
qs = 'querystring = ' + JSON.stringify(source.queryObj)
|
|
38
|
-
|
|
39
|
-
code.push(qs)
|
|
40
|
-
.blank()
|
|
41
|
-
}
|
|
29
|
+
code.push('url = "%s"', source.fullUrl).blank();
|
|
42
30
|
|
|
43
31
|
// Construct payload
|
|
44
|
-
let hasPayload = false
|
|
45
|
-
let jsonPayload = false
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (payload) {
|
|
58
|
-
code.push('payload = %s', payload)
|
|
59
|
-
hasPayload = true
|
|
60
|
-
}
|
|
32
|
+
let hasPayload = false;
|
|
33
|
+
let jsonPayload = false;
|
|
34
|
+
if (source.postData.mimeType === 'application/json') {
|
|
35
|
+
if (source.postData.jsonObj) {
|
|
36
|
+
code.push('payload = %s', helpers.literalRepresentation(source.postData.jsonObj, opts));
|
|
37
|
+
jsonPayload = true;
|
|
38
|
+
hasPayload = true;
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const payload = JSON.stringify(source.postData.text);
|
|
42
|
+
if (payload) {
|
|
43
|
+
code.push('payload = %s', payload);
|
|
44
|
+
hasPayload = true;
|
|
61
45
|
}
|
|
62
46
|
}
|
|
63
47
|
|
|
64
48
|
// Construct headers
|
|
65
|
-
const headers = source.allHeaders
|
|
66
|
-
const headerCount = Object.keys(headers).length
|
|
49
|
+
const headers = source.allHeaders;
|
|
50
|
+
const headerCount = Object.keys(headers).length;
|
|
67
51
|
|
|
68
52
|
if (headerCount === 1) {
|
|
69
|
-
|
|
70
|
-
code.push('headers = {"%s": "%s"}', header, headers[header])
|
|
71
|
-
|
|
72
|
-
}
|
|
53
|
+
Object.keys(headers).forEach(header => {
|
|
54
|
+
code.push('headers = {"%s": "%s"}', header, headers[header]).blank();
|
|
55
|
+
});
|
|
73
56
|
} else if (headerCount > 1) {
|
|
74
|
-
let count = 1
|
|
57
|
+
let count = 1;
|
|
75
58
|
|
|
76
|
-
code.push('headers = {')
|
|
59
|
+
code.push('headers = {');
|
|
77
60
|
|
|
78
|
-
|
|
61
|
+
Object.keys(headers).forEach(header => {
|
|
62
|
+
// eslint-disable-next-line no-plusplus
|
|
79
63
|
if (count++ !== headerCount) {
|
|
80
|
-
code.push(1, '"%s": "%s",', header, headers[header])
|
|
64
|
+
code.push(1, '"%s": "%s",', header, headers[header]);
|
|
81
65
|
} else {
|
|
82
|
-
code.push(1, '"%s": "%s"', header, headers[header])
|
|
66
|
+
code.push(1, '"%s": "%s"', header, headers[header]);
|
|
83
67
|
}
|
|
84
|
-
}
|
|
68
|
+
});
|
|
85
69
|
|
|
86
|
-
code.push('}')
|
|
87
|
-
.blank()
|
|
70
|
+
code.push('}').blank();
|
|
88
71
|
}
|
|
89
72
|
|
|
90
73
|
// Construct request
|
|
91
|
-
const method = source.method
|
|
92
|
-
let request =
|
|
74
|
+
const method = source.method;
|
|
75
|
+
let request = format('response = requests.request("%s", url', method);
|
|
93
76
|
|
|
94
77
|
if (hasPayload) {
|
|
95
78
|
if (jsonPayload) {
|
|
96
|
-
request += ', json=payload'
|
|
79
|
+
request += ', json=payload';
|
|
97
80
|
} else {
|
|
98
|
-
request += ', data=payload'
|
|
81
|
+
request += ', data=payload';
|
|
99
82
|
}
|
|
100
83
|
}
|
|
101
84
|
|
|
102
85
|
if (headerCount > 0) {
|
|
103
|
-
request += ', headers=headers'
|
|
86
|
+
request += ', headers=headers';
|
|
104
87
|
}
|
|
105
88
|
|
|
106
|
-
|
|
107
|
-
request += ', params=querystring'
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
request += ')'
|
|
111
|
-
|
|
112
|
-
code.push(request)
|
|
113
|
-
.blank()
|
|
89
|
+
request += ')';
|
|
114
90
|
|
|
115
|
-
|
|
116
|
-
.push('print(response.text)')
|
|
91
|
+
code.push(request).blank().push('print(response.text)');
|
|
117
92
|
|
|
118
|
-
return code.join()
|
|
119
|
-
}
|
|
93
|
+
return code.join();
|
|
94
|
+
};
|
|
120
95
|
|
|
121
96
|
module.exports.info = {
|
|
122
97
|
key: 'requests',
|
|
123
98
|
title: 'Requests',
|
|
124
99
|
link: 'http://docs.python-requests.org/en/latest/api/#requests.request',
|
|
125
|
-
description: 'Requests HTTP library'
|
|
126
|
-
}
|
|
100
|
+
description: 'Requests HTTP library',
|
|
101
|
+
};
|
package/src/targets/r/httr.js
CHANGED
|
@@ -8,145 +8,134 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const { format } = require('util');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
15
|
-
|
|
16
|
-
module.exports = function (source, options) {
|
|
14
|
+
module.exports = function (source) {
|
|
17
15
|
// Start snippet
|
|
18
|
-
const code = new CodeBuilder()
|
|
16
|
+
const code = new CodeBuilder();
|
|
19
17
|
|
|
20
18
|
// Import httr
|
|
21
|
-
code.push('library(httr)')
|
|
22
|
-
.blank()
|
|
19
|
+
code.push('library(httr)').blank();
|
|
23
20
|
|
|
24
21
|
// Set URL
|
|
25
|
-
code.push('url <- "%s"', source.url)
|
|
26
|
-
.blank()
|
|
22
|
+
code.push('url <- "%s"', source.url).blank();
|
|
27
23
|
|
|
28
24
|
// Construct query string
|
|
29
|
-
const qs = source.queryObj
|
|
30
|
-
const queryCount = Object.keys(qs).length
|
|
31
|
-
|
|
25
|
+
const qs = source.queryObj;
|
|
26
|
+
const queryCount = Object.keys(qs).length;
|
|
27
|
+
// eslint-disable-next-line no-param-reassign
|
|
28
|
+
delete source.queryObj.key;
|
|
32
29
|
|
|
33
30
|
if (source.queryString.length === 1) {
|
|
34
|
-
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString())
|
|
35
|
-
.blank()
|
|
31
|
+
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString()).blank();
|
|
36
32
|
} else if (source.queryString.length > 1) {
|
|
37
|
-
let count = 1
|
|
33
|
+
let count = 1;
|
|
38
34
|
|
|
39
|
-
code.push('queryString <- list(')
|
|
35
|
+
code.push('queryString <- list(');
|
|
40
36
|
|
|
41
|
-
|
|
37
|
+
Object.keys(qs).forEach(query => {
|
|
38
|
+
// eslint-disable-next-line no-plusplus
|
|
42
39
|
if (count++ !== queryCount - 1) {
|
|
43
|
-
code.push(' %s = "%s",', query, qs[query].toString())
|
|
40
|
+
code.push(' %s = "%s",', query, qs[query].toString());
|
|
44
41
|
} else {
|
|
45
|
-
code.push(' %s = "%s"', query, qs[query].toString())
|
|
42
|
+
code.push(' %s = "%s"', query, qs[query].toString());
|
|
46
43
|
}
|
|
47
|
-
}
|
|
44
|
+
});
|
|
48
45
|
|
|
49
|
-
code.push(')')
|
|
50
|
-
.blank()
|
|
46
|
+
code.push(')').blank();
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
// Construct payload
|
|
54
|
-
const payload = JSON.stringify(source.postData.text)
|
|
50
|
+
const payload = JSON.stringify(source.postData.text);
|
|
55
51
|
|
|
56
52
|
if (payload) {
|
|
57
|
-
code.push('payload <- %s', payload)
|
|
58
|
-
.blank()
|
|
53
|
+
code.push('payload <- %s', payload).blank();
|
|
59
54
|
}
|
|
60
55
|
|
|
61
56
|
// Define encode
|
|
62
57
|
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
|
|
63
58
|
switch (source.postData.mimeType) {
|
|
64
59
|
case 'application/x-www-form-urlencoded':
|
|
65
|
-
code.push('encode <- "form"')
|
|
66
|
-
|
|
67
|
-
break
|
|
60
|
+
code.push('encode <- "form"').blank();
|
|
61
|
+
break;
|
|
68
62
|
|
|
69
63
|
case 'application/json':
|
|
70
|
-
code.push('encode <- "json"')
|
|
71
|
-
|
|
72
|
-
break
|
|
64
|
+
code.push('encode <- "json"').blank();
|
|
65
|
+
break;
|
|
73
66
|
|
|
74
67
|
case 'multipart/form-data':
|
|
75
|
-
code.push('encode <- "multipart"')
|
|
76
|
-
|
|
77
|
-
break
|
|
68
|
+
code.push('encode <- "multipart"').blank();
|
|
69
|
+
break;
|
|
78
70
|
|
|
79
71
|
default:
|
|
80
|
-
code.push('encode <- "raw"')
|
|
81
|
-
|
|
82
|
-
break
|
|
72
|
+
code.push('encode <- "raw"').blank();
|
|
73
|
+
break;
|
|
83
74
|
}
|
|
84
75
|
}
|
|
85
76
|
|
|
86
77
|
// Construct headers
|
|
87
|
-
const headers = source.allHeaders
|
|
88
|
-
let headerCount =
|
|
89
|
-
let header = ''
|
|
90
|
-
let cookies
|
|
91
|
-
let accept
|
|
78
|
+
const headers = source.allHeaders;
|
|
79
|
+
let headerCount = 0;
|
|
80
|
+
let header = '';
|
|
81
|
+
let cookies;
|
|
82
|
+
let accept;
|
|
92
83
|
|
|
93
|
-
|
|
84
|
+
Object.keys(headers).forEach(head => {
|
|
94
85
|
if (head.toLowerCase() === 'accept') {
|
|
95
|
-
accept =
|
|
96
|
-
headerCount
|
|
86
|
+
accept = `, accept("${headers[head]}")`;
|
|
87
|
+
headerCount += 1;
|
|
97
88
|
} else if (head.toLowerCase() === 'cookie') {
|
|
98
|
-
cookies =
|
|
99
|
-
headerCount
|
|
89
|
+
cookies = `, set_cookies(\`${headers[head].replace(/;/g, '", `').replace(/` /g, '`').replace(/=/g, '` = "')}")`;
|
|
90
|
+
headerCount += 1;
|
|
100
91
|
} else if (head.toLowerCase() !== 'content-type') {
|
|
101
|
-
header = header + head.replace('-', '_')
|
|
102
|
-
if (headerCount > 1) {
|
|
92
|
+
header = `${header + head.replace('-', '_')} = '${headers[head]}`;
|
|
93
|
+
if (headerCount > 1) {
|
|
94
|
+
header += "', ";
|
|
95
|
+
}
|
|
103
96
|
}
|
|
104
|
-
}
|
|
97
|
+
});
|
|
105
98
|
|
|
106
99
|
// Construct request
|
|
107
|
-
const method = source.method
|
|
108
|
-
let request =
|
|
100
|
+
const method = source.method;
|
|
101
|
+
let request = format('response <- VERB("%s", url', method);
|
|
109
102
|
|
|
110
103
|
if (payload) {
|
|
111
|
-
request += ', body = payload'
|
|
104
|
+
request += ', body = payload';
|
|
112
105
|
}
|
|
113
106
|
|
|
114
107
|
if (header !== '') {
|
|
115
|
-
request +=
|
|
108
|
+
request += `, add_headers(${header}')`;
|
|
116
109
|
}
|
|
117
110
|
|
|
118
111
|
if (source.queryString.length) {
|
|
119
|
-
request += ', query = queryString'
|
|
112
|
+
request += ', query = queryString';
|
|
120
113
|
}
|
|
121
114
|
|
|
122
|
-
request +=
|
|
115
|
+
request += `, content_type("${source.postData.mimeType}")`;
|
|
123
116
|
|
|
124
117
|
if (typeof accept !== 'undefined') {
|
|
125
|
-
request += accept
|
|
118
|
+
request += accept;
|
|
126
119
|
}
|
|
127
120
|
|
|
128
121
|
if (typeof cookies !== 'undefined') {
|
|
129
|
-
request += cookies
|
|
122
|
+
request += cookies;
|
|
130
123
|
}
|
|
131
124
|
|
|
132
125
|
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
|
|
133
|
-
request += ', encode = encode'
|
|
126
|
+
request += ', encode = encode';
|
|
134
127
|
}
|
|
135
128
|
|
|
136
|
-
request += ')'
|
|
137
|
-
|
|
138
|
-
code.push(request)
|
|
139
|
-
.blank()
|
|
129
|
+
request += ')';
|
|
140
130
|
|
|
141
|
-
|
|
142
|
-
.push('content(response, "text")')
|
|
131
|
+
code.push(request).blank().push('content(response, "text")');
|
|
143
132
|
|
|
144
|
-
return code.join()
|
|
145
|
-
}
|
|
133
|
+
return code.join();
|
|
134
|
+
};
|
|
146
135
|
|
|
147
136
|
module.exports.info = {
|
|
148
137
|
key: 'httr',
|
|
149
138
|
title: 'httr',
|
|
150
139
|
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
|
|
151
|
-
description: 'httr: Tools for Working with URLs and HTTP'
|
|
152
|
-
}
|
|
140
|
+
description: 'httr: Tools for Working with URLs and HTTP',
|
|
141
|
+
};
|
package/src/targets/r/index.js
CHANGED
|
@@ -1,65 +1,71 @@
|
|
|
1
|
-
|
|
1
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module.exports = function (source) {
|
|
4
|
+
const code = new CodeBuilder();
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
const code = new CodeBuilder()
|
|
7
|
-
|
|
8
|
-
code.push('require \'uri\'')
|
|
9
|
-
.push('require \'net/http\'')
|
|
6
|
+
code.push("require 'uri'").push("require 'net/http'");
|
|
10
7
|
|
|
11
8
|
if (source.uriObj.protocol === 'https:') {
|
|
12
|
-
code.push(
|
|
9
|
+
code.push("require 'openssl'");
|
|
13
10
|
}
|
|
14
11
|
|
|
15
|
-
code.blank()
|
|
12
|
+
code.blank();
|
|
16
13
|
|
|
17
14
|
// To support custom methods we check for the supported methods
|
|
18
15
|
// and if doesn't exist then we build a custom class for it
|
|
19
|
-
const method = source.method.toUpperCase()
|
|
20
|
-
const methods = [
|
|
21
|
-
|
|
16
|
+
const method = source.method.toUpperCase();
|
|
17
|
+
const methods = [
|
|
18
|
+
'GET',
|
|
19
|
+
'POST',
|
|
20
|
+
'HEAD',
|
|
21
|
+
'DELETE',
|
|
22
|
+
'PATCH',
|
|
23
|
+
'PUT',
|
|
24
|
+
'OPTIONS',
|
|
25
|
+
'COPY',
|
|
26
|
+
'LOCK',
|
|
27
|
+
'UNLOCK',
|
|
28
|
+
'MOVE',
|
|
29
|
+
'TRACE',
|
|
30
|
+
];
|
|
31
|
+
const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
|
|
22
32
|
if (methods.indexOf(method) < 0) {
|
|
23
|
-
code
|
|
24
|
-
.push('
|
|
25
|
-
.push(
|
|
33
|
+
code
|
|
34
|
+
.push('class Net::HTTP::%s < Net::HTTPRequest', capMethod)
|
|
35
|
+
.push(" METHOD = '%s'", method.toUpperCase())
|
|
36
|
+
.push(" REQUEST_HAS_BODY = '%s'", source.postData.text ? 'true' : 'false')
|
|
26
37
|
.push(' RESPONSE_HAS_BODY = true')
|
|
27
38
|
.push('end')
|
|
28
|
-
.blank()
|
|
39
|
+
.blank();
|
|
29
40
|
}
|
|
30
41
|
|
|
31
|
-
code.push('url = URI("%s")', source.fullUrl)
|
|
32
|
-
.blank()
|
|
33
|
-
.push('http = Net::HTTP.new(url.host, url.port)')
|
|
42
|
+
code.push('url = URI("%s")', source.fullUrl).blank().push('http = Net::HTTP.new(url.host, url.port)');
|
|
34
43
|
|
|
35
44
|
if (source.uriObj.protocol === 'https:') {
|
|
36
|
-
code.push('http.use_ssl = true')
|
|
45
|
+
code.push('http.use_ssl = true');
|
|
37
46
|
}
|
|
38
47
|
|
|
39
|
-
code.blank()
|
|
40
|
-
.push('request = Net::HTTP::%s.new(url)', capMethod)
|
|
48
|
+
code.blank().push('request = Net::HTTP::%s.new(url)', capMethod);
|
|
41
49
|
|
|
42
|
-
const headers = Object.keys(source.allHeaders)
|
|
50
|
+
const headers = Object.keys(source.allHeaders);
|
|
43
51
|
if (headers.length) {
|
|
44
52
|
headers.forEach(function (key) {
|
|
45
|
-
code.push('request["%s"] = \'%s\'', key, source.allHeaders[key])
|
|
46
|
-
})
|
|
53
|
+
code.push('request["%s"] = \'%s\'', key, source.allHeaders[key]);
|
|
54
|
+
});
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
if (source.postData.text) {
|
|
50
|
-
code.push('request.body = %s', JSON.stringify(source.postData.text))
|
|
58
|
+
code.push('request.body = %s', JSON.stringify(source.postData.text));
|
|
51
59
|
}
|
|
52
60
|
|
|
53
|
-
code.blank()
|
|
54
|
-
.push('response = http.request(request)')
|
|
55
|
-
.push('puts response.read_body')
|
|
61
|
+
code.blank().push('response = http.request(request)').push('puts response.read_body');
|
|
56
62
|
|
|
57
|
-
return code.join()
|
|
58
|
-
}
|
|
63
|
+
return code.join();
|
|
64
|
+
};
|
|
59
65
|
|
|
60
66
|
module.exports.info = {
|
|
61
67
|
key: 'native',
|
|
62
68
|
title: 'net::http',
|
|
63
69
|
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html',
|
|
64
|
-
description: 'Ruby HTTP client'
|
|
65
|
-
}
|
|
70
|
+
description: 'Ruby HTTP client',
|
|
71
|
+
};
|