@scalar/snippetz 0.7.4 → 0.7.6
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/dist/clients/index.js +168 -169
- package/dist/httpsnippet-lite/helpers/code-builder.js +86 -74
- package/dist/httpsnippet-lite/helpers/escape.js +69 -44
- package/dist/httpsnippet-lite/helpers/headers.js +17 -14
- package/dist/httpsnippet-lite/helpers/reducer.js +1 -1
- package/dist/httpsnippet-lite/helpers/shell.js +13 -13
- package/dist/httpsnippet-lite/helpers/snippet-request.js +1 -1
- package/dist/httpsnippet-lite/helpers/url.js +4 -8
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js +69 -67
- package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js +165 -154
- package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js +35 -38
- package/dist/httpsnippet-lite/targets/go/native/client.js +151 -147
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js +32 -35
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js +40 -39
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js +83 -73
- package/dist/httpsnippet-lite/targets/java/unirest/client.js +41 -34
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js +81 -76
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js +77 -71
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js +73 -71
- package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js +83 -73
- package/dist/httpsnippet-lite/targets/node/axios/client.js +74 -69
- package/dist/httpsnippet-lite/targets/objc/helpers.js +52 -33
- package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js +123 -121
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js +60 -49
- package/dist/httpsnippet-lite/targets/powershell/common.js +46 -56
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js +9 -13
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js +9 -13
- package/dist/httpsnippet-lite/targets/python/python3/client.js +85 -68
- package/dist/httpsnippet-lite/targets/r/httr/client.js +114 -95
- package/dist/httpsnippet-lite/targets/ruby/native/client.js +66 -70
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js +121 -107
- package/dist/httpsnippet-lite/targets/shell/wget/client.js +48 -43
- package/dist/httpsnippet-lite/targets/swift/helpers.js +62 -44
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +126 -112
- package/dist/httpsnippet-lite/targets/target.js +1 -1
- package/dist/index.js +2 -9
- package/dist/libs/http.js +68 -56
- package/dist/libs/javascript.js +77 -60
- package/dist/libs/php.js +59 -49
- package/dist/libs/rust.js +60 -33
- package/dist/libs/shell.js +14 -5
- package/dist/plugins/c/libcurl/index.js +1 -5
- package/dist/plugins/c/libcurl/libcurl.js +13 -13
- package/dist/plugins/clojure/clj_http/clj_http.js +13 -13
- package/dist/plugins/clojure/clj_http/index.js +1 -5
- package/dist/plugins/csharp/httpclient/httpclient.js +206 -150
- package/dist/plugins/csharp/httpclient/index.js +1 -5
- package/dist/plugins/csharp/restsharp/index.js +1 -5
- package/dist/plugins/csharp/restsharp/restsharp.js +13 -13
- package/dist/plugins/dart/http/http.js +95 -93
- package/dist/plugins/dart/http/index.js +1 -5
- package/dist/plugins/fsharp/httpclient/httpclient.js +169 -122
- package/dist/plugins/fsharp/httpclient/index.js +1 -5
- package/dist/plugins/go/native/index.js +1 -5
- package/dist/plugins/go/native/native.js +13 -13
- package/dist/plugins/http/http11/http11.d.ts.map +1 -1
- package/dist/plugins/http/http11/http11.js +104 -85
- package/dist/plugins/http/http11/index.js +1 -5
- package/dist/plugins/java/asynchttp/asynchttp.js +13 -13
- package/dist/plugins/java/asynchttp/index.js +1 -5
- package/dist/plugins/java/nethttp/index.js +1 -5
- package/dist/plugins/java/nethttp/nethttp.js +13 -13
- package/dist/plugins/java/okhttp/index.js +1 -5
- package/dist/plugins/java/okhttp/okhttp.js +13 -13
- package/dist/plugins/java/unirest/index.js +1 -5
- package/dist/plugins/java/unirest/unirest.js +13 -13
- package/dist/plugins/js/axios/axios.js +13 -13
- package/dist/plugins/js/axios/index.js +1 -5
- package/dist/plugins/js/fetch/fetch.js +82 -68
- package/dist/plugins/js/fetch/index.js +1 -5
- package/dist/plugins/js/jquery/index.js +1 -5
- package/dist/plugins/js/jquery/jquery.js +13 -13
- package/dist/plugins/js/ofetch/index.js +1 -5
- package/dist/plugins/js/ofetch/ofetch.js +61 -48
- package/dist/plugins/js/xhr/index.js +1 -5
- package/dist/plugins/js/xhr/xhr.js +13 -13
- package/dist/plugins/kotlin/okhttp/index.js +1 -5
- package/dist/plugins/kotlin/okhttp/okhttp.js +13 -13
- package/dist/plugins/node/axios/axios.js +13 -13
- package/dist/plugins/node/axios/index.js +1 -5
- package/dist/plugins/node/fetch/fetch.js +86 -74
- package/dist/plugins/node/fetch/index.js +1 -5
- package/dist/plugins/node/ofetch/index.js +1 -5
- package/dist/plugins/node/ofetch/ofetch.js +61 -48
- package/dist/plugins/node/undici/index.js +1 -5
- package/dist/plugins/node/undici/undici.js +60 -47
- package/dist/plugins/objc/nsurlsession/index.js +1 -5
- package/dist/plugins/objc/nsurlsession/nsurlsession.js +13 -13
- package/dist/plugins/ocaml/cohttp/cohttp.js +13 -13
- package/dist/plugins/ocaml/cohttp/index.js +1 -5
- package/dist/plugins/php/curl/curl.js +143 -101
- package/dist/plugins/php/curl/index.js +1 -5
- package/dist/plugins/php/guzzle/guzzle.d.ts.map +1 -1
- package/dist/plugins/php/guzzle/guzzle.js +109 -83
- package/dist/plugins/php/guzzle/index.js +1 -5
- package/dist/plugins/powershell/restmethod/index.js +1 -5
- package/dist/plugins/powershell/restmethod/restmethod.js +13 -13
- package/dist/plugins/powershell/webrequest/index.js +1 -5
- package/dist/plugins/powershell/webrequest/webrequest.js +13 -13
- package/dist/plugins/python/httpx/async.js +17 -15
- package/dist/plugins/python/httpx/index.js +2 -7
- package/dist/plugins/python/httpx/sync.js +11 -12
- package/dist/plugins/python/python3/index.js +1 -5
- package/dist/plugins/python/python3/python3.js +13 -13
- package/dist/plugins/python/requests/index.js +1 -5
- package/dist/plugins/python/requests/requests.js +11 -12
- package/dist/plugins/python/requestsLike.d.ts.map +1 -1
- package/dist/plugins/python/requestsLike.js +145 -112
- package/dist/plugins/r/httr/httr.js +13 -13
- package/dist/plugins/r/httr/index.js +1 -5
- package/dist/plugins/ruby/native/index.js +1 -5
- package/dist/plugins/ruby/native/native.js +13 -13
- package/dist/plugins/rust/reqwest/index.js +1 -5
- package/dist/plugins/rust/reqwest/reqwest.js +126 -83
- package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
- package/dist/plugins/shell/curl/curl.js +129 -97
- package/dist/plugins/shell/curl/index.js +1 -5
- package/dist/plugins/shell/httpie/httpie.js +13 -13
- package/dist/plugins/shell/httpie/index.js +1 -5
- package/dist/plugins/shell/wget/index.js +1 -5
- package/dist/plugins/shell/wget/wget.js +13 -13
- package/dist/plugins/swift/nsurlsession/index.js +1 -5
- package/dist/plugins/swift/nsurlsession/nsurlsession.js +13 -13
- package/dist/snippetz.js +25 -28
- package/dist/utils/convertWithHttpSnippetLite.js +100 -96
- package/dist/utils/index.js +1 -5
- package/package.json +8 -12
- package/dist/clients/index.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/code-builder.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/escape.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/headers.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/reducer.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/shell.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/snippet-request.js.map +0 -7
- package/dist/httpsnippet-lite/helpers/url.js.map +0 -7
- package/dist/httpsnippet-lite/targets/c/libcurl/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/go/native/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/asynchttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/nethttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/okhttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/java/unirest/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/axios/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/jquery/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/javascript/xhr/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/node/axios/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/objc/helpers.js.map +0 -7
- package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/common.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/python/python3/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/r/httr/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/ruby/native/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/shell/httpie/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/shell/wget/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/swift/helpers.js.map +0 -7
- package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js.map +0 -7
- package/dist/httpsnippet-lite/targets/target.js.map +0 -7
- package/dist/index.js.map +0 -7
- package/dist/libs/http.js.map +0 -7
- package/dist/libs/javascript.js.map +0 -7
- package/dist/libs/php.js.map +0 -7
- package/dist/libs/rust.js.map +0 -7
- package/dist/libs/shell.js.map +0 -7
- package/dist/plugins/c/libcurl/index.js.map +0 -7
- package/dist/plugins/c/libcurl/libcurl.js.map +0 -7
- package/dist/plugins/clojure/clj_http/clj_http.js.map +0 -7
- package/dist/plugins/clojure/clj_http/index.js.map +0 -7
- package/dist/plugins/csharp/httpclient/httpclient.js.map +0 -7
- package/dist/plugins/csharp/httpclient/index.js.map +0 -7
- package/dist/plugins/csharp/restsharp/index.js.map +0 -7
- package/dist/plugins/csharp/restsharp/restsharp.js.map +0 -7
- package/dist/plugins/dart/http/http.js.map +0 -7
- package/dist/plugins/dart/http/index.js.map +0 -7
- package/dist/plugins/fsharp/httpclient/httpclient.js.map +0 -7
- package/dist/plugins/fsharp/httpclient/index.js.map +0 -7
- package/dist/plugins/go/native/index.js.map +0 -7
- package/dist/plugins/go/native/native.js.map +0 -7
- package/dist/plugins/http/http11/http11.js.map +0 -7
- package/dist/plugins/http/http11/index.js.map +0 -7
- package/dist/plugins/java/asynchttp/asynchttp.js.map +0 -7
- package/dist/plugins/java/asynchttp/index.js.map +0 -7
- package/dist/plugins/java/nethttp/index.js.map +0 -7
- package/dist/plugins/java/nethttp/nethttp.js.map +0 -7
- package/dist/plugins/java/okhttp/index.js.map +0 -7
- package/dist/plugins/java/okhttp/okhttp.js.map +0 -7
- package/dist/plugins/java/unirest/index.js.map +0 -7
- package/dist/plugins/java/unirest/unirest.js.map +0 -7
- package/dist/plugins/js/axios/axios.js.map +0 -7
- package/dist/plugins/js/axios/index.js.map +0 -7
- package/dist/plugins/js/fetch/fetch.js.map +0 -7
- package/dist/plugins/js/fetch/index.js.map +0 -7
- package/dist/plugins/js/jquery/index.js.map +0 -7
- package/dist/plugins/js/jquery/jquery.js.map +0 -7
- package/dist/plugins/js/ofetch/index.js.map +0 -7
- package/dist/plugins/js/ofetch/ofetch.js.map +0 -7
- package/dist/plugins/js/xhr/index.js.map +0 -7
- package/dist/plugins/js/xhr/xhr.js.map +0 -7
- package/dist/plugins/kotlin/okhttp/index.js.map +0 -7
- package/dist/plugins/kotlin/okhttp/okhttp.js.map +0 -7
- package/dist/plugins/node/axios/axios.js.map +0 -7
- package/dist/plugins/node/axios/index.js.map +0 -7
- package/dist/plugins/node/fetch/fetch.js.map +0 -7
- package/dist/plugins/node/fetch/index.js.map +0 -7
- package/dist/plugins/node/ofetch/index.js.map +0 -7
- package/dist/plugins/node/ofetch/ofetch.js.map +0 -7
- package/dist/plugins/node/undici/index.js.map +0 -7
- package/dist/plugins/node/undici/undici.js.map +0 -7
- package/dist/plugins/objc/nsurlsession/index.js.map +0 -7
- package/dist/plugins/objc/nsurlsession/nsurlsession.js.map +0 -7
- package/dist/plugins/ocaml/cohttp/cohttp.js.map +0 -7
- package/dist/plugins/ocaml/cohttp/index.js.map +0 -7
- package/dist/plugins/php/curl/curl.js.map +0 -7
- package/dist/plugins/php/curl/index.js.map +0 -7
- package/dist/plugins/php/guzzle/guzzle.js.map +0 -7
- package/dist/plugins/php/guzzle/index.js.map +0 -7
- package/dist/plugins/powershell/restmethod/index.js.map +0 -7
- package/dist/plugins/powershell/restmethod/restmethod.js.map +0 -7
- package/dist/plugins/powershell/webrequest/index.js.map +0 -7
- package/dist/plugins/powershell/webrequest/webrequest.js.map +0 -7
- package/dist/plugins/python/httpx/async.js.map +0 -7
- package/dist/plugins/python/httpx/index.js.map +0 -7
- package/dist/plugins/python/httpx/sync.js.map +0 -7
- package/dist/plugins/python/python3/index.js.map +0 -7
- package/dist/plugins/python/python3/python3.js.map +0 -7
- package/dist/plugins/python/requests/index.js.map +0 -7
- package/dist/plugins/python/requests/requests.js.map +0 -7
- package/dist/plugins/python/requestsLike.js.map +0 -7
- package/dist/plugins/r/httr/httr.js.map +0 -7
- package/dist/plugins/r/httr/index.js.map +0 -7
- package/dist/plugins/ruby/native/index.js.map +0 -7
- package/dist/plugins/ruby/native/native.js.map +0 -7
- package/dist/plugins/rust/reqwest/index.js.map +0 -7
- package/dist/plugins/rust/reqwest/reqwest.js.map +0 -7
- package/dist/plugins/shell/curl/curl.js.map +0 -7
- package/dist/plugins/shell/curl/index.js.map +0 -7
- package/dist/plugins/shell/httpie/httpie.js.map +0 -7
- package/dist/plugins/shell/httpie/index.js.map +0 -7
- package/dist/plugins/shell/wget/index.js.map +0 -7
- package/dist/plugins/shell/wget/wget.js.map +0 -7
- package/dist/plugins/swift/nsurlsession/index.js.map +0 -7
- package/dist/plugins/swift/nsurlsession/nsurlsession.js.map +0 -7
- package/dist/snippetz.js.map +0 -7
- package/dist/utils/convertWithHttpSnippetLite.js.map +0 -7
- package/dist/utils/index.js.map +0 -7
|
@@ -1,71 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (payload) {
|
|
28
|
-
push(`payload = ${payload}`);
|
|
29
|
-
blank();
|
|
30
|
-
}
|
|
31
|
-
const headers = allHeaders;
|
|
32
|
-
const headerCount = Object.keys(headers).length;
|
|
33
|
-
if (headerCount === 1) {
|
|
34
|
-
for (const header in headers) {
|
|
35
|
-
push(`headers = { '${header}': "${escapeForDoubleQuotes(headers[header])}" }`);
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* HTTP code snippet generator for native Python3.
|
|
4
|
+
*
|
|
5
|
+
* @author
|
|
6
|
+
* @montanaflynn
|
|
7
|
+
*
|
|
8
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
+
*/
|
|
10
|
+
import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
|
|
11
|
+
import { escapeForDoubleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
|
|
12
|
+
export const python3 = {
|
|
13
|
+
info: {
|
|
14
|
+
key: 'python3',
|
|
15
|
+
title: 'http.client',
|
|
16
|
+
link: 'https://docs.python.org/3/library/http.client.html',
|
|
17
|
+
description: 'Python3 HTTP Client',
|
|
18
|
+
},
|
|
19
|
+
convert: ({ uriObj: { path, protocol, host }, postData, allHeaders, method }, options = {}) => {
|
|
20
|
+
const { insecureSkipVerify = false } = options;
|
|
21
|
+
const { push, blank, join } = new CodeBuilder();
|
|
22
|
+
// Start Request
|
|
23
|
+
push('import http.client');
|
|
24
|
+
if (insecureSkipVerify) {
|
|
25
|
+
push('import ssl');
|
|
26
|
+
}
|
|
36
27
|
blank();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (count++ !== headerCount) {
|
|
43
|
-
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}",`);
|
|
44
|
-
} else {
|
|
45
|
-
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}"`);
|
|
28
|
+
// Check which protocol to be used for the client connection
|
|
29
|
+
if (protocol === 'https:') {
|
|
30
|
+
const sslContext = insecureSkipVerify ? ', context = ssl._create_unverified_context()' : '';
|
|
31
|
+
push(`conn = http.client.HTTPSConnection("${host}"${sslContext})`);
|
|
32
|
+
blank();
|
|
46
33
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
34
|
+
else {
|
|
35
|
+
push(`conn = http.client.HTTPConnection("${host}")`);
|
|
36
|
+
blank();
|
|
37
|
+
}
|
|
38
|
+
// Create payload string if it exists
|
|
39
|
+
const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
|
|
40
|
+
if (payload) {
|
|
41
|
+
push(`payload = ${payload}`);
|
|
42
|
+
blank();
|
|
43
|
+
}
|
|
44
|
+
// Create Headers
|
|
45
|
+
const headers = allHeaders;
|
|
46
|
+
const headerCount = Object.keys(headers).length;
|
|
47
|
+
if (headerCount === 1) {
|
|
48
|
+
for (const header in headers) {
|
|
49
|
+
push(`headers = { '${header}': "${escapeForDoubleQuotes(headers[header])}" }`);
|
|
50
|
+
blank();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (headerCount > 1) {
|
|
54
|
+
let count = 1;
|
|
55
|
+
push('headers = {');
|
|
56
|
+
for (const header in headers) {
|
|
57
|
+
if (count++ !== headerCount) {
|
|
58
|
+
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}",`);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}"`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
push('}');
|
|
65
|
+
blank();
|
|
66
|
+
}
|
|
67
|
+
// Make Request
|
|
68
|
+
if (payload && headerCount) {
|
|
69
|
+
push(`conn.request("${method}", "${path}", payload, headers)`);
|
|
70
|
+
}
|
|
71
|
+
else if (payload && !headerCount) {
|
|
72
|
+
push(`conn.request("${method}", "${path}", payload)`);
|
|
73
|
+
}
|
|
74
|
+
else if (!payload && headerCount) {
|
|
75
|
+
push(`conn.request("${method}", "${path}", headers=headers)`);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
push(`conn.request("${method}", "${path}")`);
|
|
79
|
+
}
|
|
80
|
+
// Get Response
|
|
81
|
+
blank();
|
|
82
|
+
push('res = conn.getresponse()');
|
|
83
|
+
push('data = res.read()');
|
|
84
|
+
blank();
|
|
85
|
+
push('print(data.decode("utf-8"))');
|
|
86
|
+
return join();
|
|
87
|
+
},
|
|
70
88
|
};
|
|
71
|
-
//# sourceMappingURL=client.js.map
|
|
@@ -1,96 +1,115 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* HTTP code snippet generator for R using httr
|
|
4
|
+
*
|
|
5
|
+
* @author
|
|
6
|
+
* @gabrielakoreeda
|
|
7
|
+
*
|
|
8
|
+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
+
*/
|
|
10
|
+
import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
|
|
11
|
+
import { escapeForDoubleQuotes, escapeForSingleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
|
|
12
|
+
import { getHeader } from '../../../../httpsnippet-lite/helpers/headers.js';
|
|
13
|
+
export const httr = {
|
|
14
|
+
info: {
|
|
15
|
+
key: 'httr',
|
|
16
|
+
title: 'httr',
|
|
17
|
+
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
|
|
18
|
+
description: 'httr: Tools for Working with URLs and HTTP',
|
|
19
|
+
},
|
|
20
|
+
convert: ({ url, queryObj, queryString, postData, allHeaders, method }, options = {}) => {
|
|
21
|
+
let _a, _b;
|
|
22
|
+
// Start snippet
|
|
23
|
+
const { push, blank, join } = new CodeBuilder({
|
|
24
|
+
indent: (_a = options.indent) !== null && _a !== void 0 ? _a : ' ',
|
|
25
|
+
});
|
|
26
|
+
// Import httr
|
|
27
|
+
push('library(httr)');
|
|
28
|
+
blank();
|
|
29
|
+
// Set URL
|
|
30
|
+
push(`url <- "${url}"`);
|
|
31
|
+
blank();
|
|
32
|
+
// Construct query string
|
|
33
|
+
const qs = queryObj;
|
|
34
|
+
delete queryObj.key;
|
|
35
|
+
const entries = Object.entries(qs).flatMap(([key, value]) => Array.isArray(value) ? value.map((item) => [key, item]) : [[key, value]]);
|
|
36
|
+
const entriesCount = entries.length;
|
|
37
|
+
if (entriesCount === 1) {
|
|
38
|
+
const entry = entries[0];
|
|
39
|
+
push(`queryString <- list(${entry[0]} = "${entry[1]}")`);
|
|
40
|
+
blank();
|
|
41
|
+
}
|
|
42
|
+
else if (entriesCount > 1) {
|
|
43
|
+
push('queryString <- list(');
|
|
44
|
+
entries.forEach(([key, value], i) => {
|
|
45
|
+
const isLastItem = i !== entriesCount - 1;
|
|
46
|
+
const maybeComma = isLastItem ? ',' : '';
|
|
47
|
+
push(`${key} = "${value}"${maybeComma}`, 1);
|
|
48
|
+
});
|
|
49
|
+
push(')');
|
|
50
|
+
blank();
|
|
51
|
+
}
|
|
52
|
+
// Construct payload
|
|
53
|
+
const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
|
|
54
|
+
if (payload) {
|
|
55
|
+
push(`payload <- ${payload}`);
|
|
56
|
+
blank();
|
|
57
|
+
}
|
|
58
|
+
// Define encode
|
|
59
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
60
|
+
switch (postData.mimeType) {
|
|
61
|
+
case 'application/x-www-form-urlencoded':
|
|
62
|
+
push('encode <- "form"');
|
|
63
|
+
blank();
|
|
64
|
+
break;
|
|
65
|
+
case 'application/json':
|
|
66
|
+
push('encode <- "json"');
|
|
67
|
+
blank();
|
|
68
|
+
break;
|
|
69
|
+
case 'multipart/form-data':
|
|
70
|
+
push('encode <- "multipart"');
|
|
71
|
+
blank();
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
push('encode <- "raw"');
|
|
75
|
+
blank();
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// Construct headers
|
|
80
|
+
const cookieHeader = getHeader(allHeaders, 'cookie');
|
|
81
|
+
const acceptHeader = getHeader(allHeaders, 'accept');
|
|
82
|
+
const setCookies = cookieHeader
|
|
83
|
+
? `set_cookies(\`${String(cookieHeader).replace(/;/g, '", `').replace(/` /g, '`').replace(/[=]/g, '` = "')}")`
|
|
84
|
+
: undefined;
|
|
85
|
+
const setAccept = acceptHeader ? `accept("${escapeForDoubleQuotes(acceptHeader)}")` : undefined;
|
|
86
|
+
const setContentType = `content_type("${escapeForDoubleQuotes((_b = postData === null || postData === void 0 ? void 0 : postData.mimeType) !== null && _b !== void 0 ? _b : 'application/octet-stream')}")`;
|
|
87
|
+
const otherHeaders = Object.entries(allHeaders)
|
|
88
|
+
// These headers are all handled separately:
|
|
89
|
+
.filter(([key]) => !['cookie', 'accept', 'content-type'].includes(key.toLowerCase()))
|
|
90
|
+
.map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`)
|
|
91
|
+
.join(', ');
|
|
92
|
+
const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : undefined;
|
|
93
|
+
// Construct request
|
|
94
|
+
let request = `response <- VERB("${method}", url`;
|
|
95
|
+
if (payload) {
|
|
96
|
+
request += ', body = payload';
|
|
97
|
+
}
|
|
98
|
+
if (queryString.length) {
|
|
99
|
+
request += ', query = queryString';
|
|
100
|
+
}
|
|
101
|
+
const headerAdditions = [setHeaders, setContentType, setAccept, setCookies].filter((x) => !!x).join(', ');
|
|
102
|
+
if (headerAdditions) {
|
|
103
|
+
request += `, ${headerAdditions}`;
|
|
104
|
+
}
|
|
105
|
+
if (postData && (postData.text || postData.jsonObj || postData.params)) {
|
|
106
|
+
request += ', encode = encode';
|
|
107
|
+
}
|
|
108
|
+
request += ')';
|
|
109
|
+
push(request);
|
|
110
|
+
blank();
|
|
111
|
+
// Print response
|
|
112
|
+
push('content(response, "text")');
|
|
113
|
+
return join();
|
|
114
|
+
},
|
|
92
115
|
};
|
|
93
|
-
export {
|
|
94
|
-
httr
|
|
95
|
-
};
|
|
96
|
-
//# sourceMappingURL=client.js.map
|
|
@@ -1,71 +1,67 @@
|
|
|
1
|
-
import { CodeBuilder } from
|
|
2
|
-
import { escapeForSingleQuotes } from
|
|
3
|
-
const native = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
1
|
+
import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
|
|
2
|
+
import { escapeForSingleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
|
|
3
|
+
export const native = {
|
|
4
|
+
info: {
|
|
5
|
+
key: 'native',
|
|
6
|
+
title: 'net::http',
|
|
7
|
+
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html',
|
|
8
|
+
description: 'Ruby HTTP client',
|
|
9
|
+
},
|
|
10
|
+
convert: ({ uriObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => {
|
|
11
|
+
const { insecureSkipVerify = false } = options;
|
|
12
|
+
const { push, blank, join } = new CodeBuilder();
|
|
13
|
+
push("require 'uri'");
|
|
14
|
+
push("require 'net/http'");
|
|
15
|
+
blank();
|
|
16
|
+
// To support custom methods we check for the supported methods
|
|
17
|
+
// and if doesn't exist then we build a custom class for it
|
|
18
|
+
const method = rawMethod.toUpperCase();
|
|
19
|
+
const methods = [
|
|
20
|
+
'GET',
|
|
21
|
+
'POST',
|
|
22
|
+
'HEAD',
|
|
23
|
+
'DELETE',
|
|
24
|
+
'PATCH',
|
|
25
|
+
'PUT',
|
|
26
|
+
'OPTIONS',
|
|
27
|
+
'COPY',
|
|
28
|
+
'LOCK',
|
|
29
|
+
'UNLOCK',
|
|
30
|
+
'MOVE',
|
|
31
|
+
'TRACE',
|
|
32
|
+
];
|
|
33
|
+
const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
|
|
34
|
+
if (!methods.includes(method)) {
|
|
35
|
+
push(`class Net::HTTP::${capMethod} < Net::HTTPRequest`);
|
|
36
|
+
push(` METHOD = '${method.toUpperCase()}'`);
|
|
37
|
+
push(` REQUEST_HAS_BODY = '${(postData === null || postData === void 0 ? void 0 : postData.text) ? 'true' : 'false'}'`);
|
|
38
|
+
push(' RESPONSE_HAS_BODY = true');
|
|
39
|
+
push('end');
|
|
40
|
+
blank();
|
|
41
|
+
}
|
|
42
|
+
push(`url = URI("${fullUrl}")`);
|
|
43
|
+
blank();
|
|
44
|
+
push('http = Net::HTTP.new(url.host, url.port)');
|
|
45
|
+
if (uriObj.protocol === 'https:') {
|
|
46
|
+
push('http.use_ssl = true');
|
|
47
|
+
if (insecureSkipVerify) {
|
|
48
|
+
push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
blank();
|
|
52
|
+
push(`request = Net::HTTP::${capMethod}.new(url)`);
|
|
53
|
+
const headers = Object.keys(allHeaders);
|
|
54
|
+
if (headers.length) {
|
|
55
|
+
headers.forEach((key) => {
|
|
56
|
+
push(`request["${key}"] = '${escapeForSingleQuotes(allHeaders[key])}'`);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (postData === null || postData === void 0 ? void 0 : postData.text) {
|
|
60
|
+
push(`request.body = ${JSON.stringify(postData.text)}`);
|
|
61
|
+
}
|
|
62
|
+
blank();
|
|
63
|
+
push('response = http.request(request)');
|
|
64
|
+
push('puts response.read_body');
|
|
65
|
+
return join();
|
|
66
|
+
},
|
|
67
67
|
};
|
|
68
|
-
export {
|
|
69
|
-
native
|
|
70
|
-
};
|
|
71
|
-
//# sourceMappingURL=client.js.map
|