Python http client codegen (#1252)

* add codegen for python http client

* update print header function
This commit is contained in:
luthrap
2020-10-08 23:29:00 +05:30
committed by GitHub
parent a4c5817b54
commit 92854b9e84
2 changed files with 97 additions and 0 deletions

View File

@@ -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) {

View File

@@ -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("")
},
}