Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
Python http client codegen (#1252)
luthrap92854b9e84ef
diff --git a/helpers/codegen/codegen.js b/helpers/codegen/codegen.jsindex 8f0afcb30..7436936c7 100644--- a/helpers/codegen/codegen.js+++ b/helpers/codegen/codegen.js@@ -9,6 +9,7 @@ import { JSjQueryCodegen } from "./generators/js-jQuery"import { PowerShellRestMethod } from "./generators/powershell"import { PhpCurlCodegen } from "./generators/php-curl"import { PythonRequestsCodegen } from "./generators/python-requests"+import { PythonHttpClientCodegen } from "./generators/python-http-client"/* Register code generators here.* A code generator is defined as an object with the following structure.@@ -30,6 +31,7 @@ export const codegens = [PhpCurlCodegen,PowerShellRestMethod,PythonRequestsCodegen,+ PythonHttpClientCodegen,]export function generateCodeWithGenerator(codegenID, context) {diff --git a/helpers/codegen/generators/python-http-client.js b/helpers/codegen/generators/python-http-client.jsnew file mode 100644index 000000000..941188376--- /dev/null+++ b/helpers/codegen/generators/python-http-client.js@@ -0,0 +1,95 @@+import { isJSONContentType } from "~/helpers/utils/contenttypes"++const printHeaders = (headers) => {+ if (headers.length) {+ return [`headers = {\n`, ` ${headers.join(",\n ")}\n`, `}\n`]+ } else {+ return [`headers = {}\n`]+ }+}++export const PythonHttpClientCodegen = {+ id: "python-http-client",+ name: "Python HTTP Client",+ generator: ({+ url,+ pathName,+ queryString,+ auth,+ httpUser,+ httpPassword,+ bearerToken,+ method,+ rawInput,+ rawParams,+ rawRequestBody,+ contentType,+ headers,+ }) => {+ const requestString = []+ let genHeaders = []++ requestString.push(`import http.client\n`)+ requestString.push(`import mimetypes\n`)++ const currentUrl = new URL(url)+ let hostname = currentUrl["hostname"]+ let port = currentUrl["port"]+ if (!port) {+ requestString.push(`conn = http.client.HTTPSConnection("${hostname}")\n`)+ } else {+ requestString.push(`conn = http.client.HTTPSConnection("${hostname}", ${port})\n`)+ }+ // auth headers+ if (auth === "Basic Auth") {+ const basic = `${httpUser}:${httpPassword}`+ genHeaders.push(+ `'Authorization': 'Basic ${window.btoa(unescape(encodeURIComponent(basic)))}'`+ )+ } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {+ genHeaders.push(`'Authorization': 'Bearer ${bearerToken}'`)+ }++ // custom headers+ if (headers.length) {+ headers.forEach(({ key, value }) => {+ if (key) genHeaders.push(`'${key}': '${value}'`)+ })+ }++ // initial request setup+ let requestBody = rawInput ? rawParams : rawRequestBody+ if (method == "GET") {+ requestString.push(...printHeaders(genHeaders))+ requestString.push(`payload = ''\n`)+ }+ if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {+ genHeaders.push(`'Content-Type': '${contentType}'`)+ requestString.push(...printHeaders(genHeaders))++ if (isJSONContentType(contentType)) {+ requestBody = JSON.stringify(requestBody)+ requestString.push(`payload = ${requestBody}\n`)+ } else if (contentType.includes("x-www-form-urlencoded")) {+ const formData = []+ if (requestBody.indexOf("=") > -1) {+ requestBody.split("&").forEach((rq) => {+ const [key, val] = rq.split("=")+ formData.push(`('${key}', '${val}')`)+ })+ }+ if (formData.length) {+ requestString.push(`payload = [${formData.join(",\n ")}]\n`)+ }+ } else {+ requestString.push(`paylod = '''${requestBody}'''\n`)+ }+ }+ requestString.push(`conn.request("${method}", "${pathName}${queryString}", payload, headers)\n`)+ requestString.push(`res = conn.getresponse()\n`)+ requestString.push(`data = res.read()\n`)+ requestString.push(`print(data.decode("utf-8"))`)++ return requestString.join("")+ },+}