python-http-client.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { isJSONContentType } from "~/helpers/utils/contenttypes"
  2. const printHeaders = (headers) => {
  3. if (headers.length) {
  4. return [`headers = {\n`, ` ${headers.join(",\n ")}\n`, `}\n`]
  5. } else {
  6. return [`headers = {}\n`]
  7. }
  8. }
  9. export const PythonHttpClientCodegen = {
  10. id: "python-http-client",
  11. name: "Python http.client",
  12. language: "python",
  13. generator: ({
  14. url,
  15. pathName,
  16. queryString,
  17. auth,
  18. httpUser,
  19. httpPassword,
  20. bearerToken,
  21. method,
  22. rawInput,
  23. rawParams,
  24. rawRequestBody,
  25. contentType,
  26. headers,
  27. }) => {
  28. const requestString = []
  29. const genHeaders = []
  30. requestString.push(`import http.client\n`)
  31. requestString.push(`import mimetypes\n`)
  32. const currentUrl = new URL(url)
  33. const hostname = currentUrl.hostname
  34. const port = currentUrl.port
  35. if (!port) {
  36. requestString.push(`conn = http.client.HTTPSConnection("${hostname}")\n`)
  37. } else {
  38. requestString.push(
  39. `conn = http.client.HTTPSConnection("${hostname}", ${port})\n`
  40. )
  41. }
  42. // auth headers
  43. if (auth === "Basic Auth") {
  44. const basic = `${httpUser}:${httpPassword}`
  45. genHeaders.push(
  46. `'Authorization': 'Basic ${window.btoa(
  47. unescape(encodeURIComponent(basic))
  48. )}'`
  49. )
  50. } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  51. genHeaders.push(`'Authorization': 'Bearer ${bearerToken}'`)
  52. }
  53. // custom headers
  54. if (headers.length) {
  55. headers.forEach(({ key, value }) => {
  56. if (key) genHeaders.push(`'${key}': '${value}'`)
  57. })
  58. }
  59. // initial request setup
  60. let requestBody = rawInput ? rawParams : rawRequestBody
  61. if (method === "GET") {
  62. requestString.push(...printHeaders(genHeaders))
  63. requestString.push(`payload = ''\n`)
  64. }
  65. if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
  66. requestString.push(...printHeaders(genHeaders))
  67. if (contentType && requestBody) {
  68. if (isJSONContentType(contentType)) {
  69. requestBody = JSON.stringify(requestBody)
  70. requestString.push(`payload = ${requestBody}\n`)
  71. } else if (contentType.includes("x-www-form-urlencoded")) {
  72. const formData = []
  73. if (requestBody.includes("=")) {
  74. requestBody.split("&").forEach((rq) => {
  75. const [key, val] = rq.split("=")
  76. formData.push(`('${key}', '${val}')`)
  77. })
  78. }
  79. if (formData.length) {
  80. requestString.push(`payload = [${formData.join(",\n ")}]\n`)
  81. }
  82. } else {
  83. requestString.push(`paylod = '''${requestBody}'''\n`)
  84. }
  85. } else {
  86. requestString.push(`payload = ''\n`)
  87. }
  88. }
  89. requestString.push(
  90. `conn.request("${method}", "${pathName}?${queryString}", payload, headers)\n`
  91. )
  92. requestString.push(`res = conn.getresponse()\n`)
  93. requestString.push(`data = res.read()\n`)
  94. requestString.push(`print(data.decode("utf-8"))`)
  95. return requestString.join("")
  96. },
  97. }