Browse Source

Add: Shell wget codegen (#1256)

* Add: Shell wget codegen

* Add missing snapshot for tests
Raul Piraces Alastuey 5 years ago
parent
commit
82c874afb5

+ 38 - 0
helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap

@@ -861,3 +861,41 @@ exports[`generate request for cURL generate PUT request for www-form-urlencoded
   -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \\\\
   -d 'foo=bar&baz=qux'"
 `;
+
+exports[`generate request for wget generate GET request 1`] = `
+"wget -O - --method=GET \\\\
+  'https://httpbin.org/path/to?a=b' \\\\
+  --header='Authorization: Basic bW9ja1VzZXI6bW9ja1Bhc3N3b3Jk' \\\\
+  --header='h1: h1v' \\\\
+  --header='h2: h2v'"
+`;
+
+exports[`generate request for wget generate POST request for JSON 1`] = `
+"wget -O - --method=POST \\\\
+  'https://httpbin.org/path/to?a=b' \\\\
+  --header='Authorization: Bearer abcdefghijklmn' \\\\
+  --header='h1: h1v' \\\\
+  --header='h2: h2v' \\\\
+  --header='Content-Type: application/json; charset=utf-8' \\\\
+  --body-data='{\\"foo\\": \\"bar\\", \\"baz\\": \\"qux\\"}'"
+`;
+
+exports[`generate request for wget generate POST request for XML 1`] = `
+"wget -O - --method=POST \\\\
+  'https://httpbin.org/path/to?a=b' \\\\
+  --header='Authorization: Bearer abcdefghijklmn' \\\\
+  --header='h1: h1v' \\\\
+  --header='h2: h2v' \\\\
+  --header='Content-Type: application/xml; charset=utf-8' \\\\
+  --body-data='<?xml version='1.0' encoding='utf-8'?>
+<xml>
+  <element foo=\\"bar\\"></element>
+</xml>'"
+`;
+
+exports[`generate request for wget generate PUT request for www-form-urlencoded 1`] = `
+"wget -O - --method=PUT \\\\
+  'https://httpbin.org/path/to?a=b' \\\\
+  --header='Content-Type: application/x-www-form-urlencoded; charset=utf-8' \\\\
+  --body-data='foo=bar&baz=qux'"
+`;

+ 2 - 0
helpers/codegen/codegen.js

@@ -10,6 +10,7 @@ import { PowerShellRestMethod } from "./generators/powershell"
 import { PhpCurlCodegen } from "./generators/php-curl"
 import { PythonRequestsCodegen } from "./generators/python-requests"
 import { PythonHttpClientCodegen } from "./generators/python-http-client"
+import { WgetCodegen } from "./generators/wget"
 
 /* Register code generators here.
  * A code generator is defined as an object with the following structure.
@@ -32,6 +33,7 @@ export const codegens = [
   PowerShellRestMethod,
   PythonRequestsCodegen,
   PythonHttpClientCodegen,
+  WgetCodegen,
 ]
 
 export function generateCodeWithGenerator(codegenID, context) {

+ 42 - 0
helpers/codegen/generators/wget.js

@@ -0,0 +1,42 @@
+export const WgetCodegen = {
+  id: "wget",
+  name: "wget",
+  generator: ({
+    url,
+    pathName,
+    queryString,
+    auth,
+    httpUser,
+    httpPassword,
+    bearerToken,
+    method,
+    rawInput,
+    rawParams,
+    rawRequestBody,
+    contentType,
+    headers,
+  }) => {
+    const requestString = []
+    requestString.push(`wget -O - --method=${method}`)
+    requestString.push(`  '${url}${pathName}${queryString}'`)
+    if (auth === "Basic Auth") {
+      const basic = `${httpUser}:${httpPassword}`
+      requestString.push(
+        `  --header='Authorization: Basic ${window.btoa(unescape(encodeURIComponent(basic)))}'`
+      )
+    } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
+      requestString.push(`  --header='Authorization: Bearer ${bearerToken}'`)
+    }
+    if (headers) {
+      headers.forEach(({ key, value }) => {
+        if (key) requestString.push(`  --header='${key}: ${value}'`)
+      })
+    }
+    if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
+      const requestBody = rawInput ? rawParams : rawRequestBody
+      requestString.push(`  --header='Content-Type: ${contentType}; charset=utf-8'`)
+      requestString.push(`  --body-data='${requestBody}'`)
+    }
+    return requestString.join(" \\\n")
+  },
+}