python-requests.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  6. return []
  7. }
  8. export const PythonRequestsCodegen = {
  9. id: "python-requests",
  10. name: "Python Requests",
  11. language: "python",
  12. generator: ({
  13. url,
  14. pathName,
  15. queryString,
  16. auth,
  17. httpUser,
  18. httpPassword,
  19. bearerToken,
  20. method,
  21. rawInput,
  22. rawParams,
  23. rawRequestBody,
  24. contentType,
  25. headers,
  26. }) => {
  27. const requestString = []
  28. const genHeaders = []
  29. requestString.push(`import requests\n\n`)
  30. requestString.push(`url = '${url}${pathName}?${queryString}'\n`)
  31. // auth headers
  32. if (auth === "Basic Auth") {
  33. const basic = `${httpUser}:${httpPassword}`
  34. genHeaders.push(
  35. `'Authorization': 'Basic ${window.btoa(
  36. unescape(encodeURIComponent(basic))
  37. )}'`
  38. )
  39. } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  40. genHeaders.push(`'Authorization': 'Bearer ${bearerToken}'`)
  41. }
  42. // custom headers
  43. if (headers.length) {
  44. headers.forEach(({ key, value }) => {
  45. if (key) genHeaders.push(`'${key}': '${value}'`)
  46. })
  47. }
  48. // initial request setup
  49. let requestBody = rawInput ? rawParams : rawRequestBody
  50. if (method === "GET") {
  51. requestString.push(...printHeaders(genHeaders))
  52. requestString.push(`response = requests.request(\n`)
  53. requestString.push(` '${method}',\n`)
  54. requestString.push(` '${url}${pathName}?${queryString}',\n`)
  55. }
  56. if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
  57. genHeaders.push(`'Content-Type': '${contentType}'`)
  58. requestString.push(...printHeaders(genHeaders))
  59. if (isJSONContentType(contentType)) {
  60. requestBody = JSON.stringify(requestBody)
  61. requestString.push(`data = ${requestBody}\n`)
  62. } else if (contentType.includes("x-www-form-urlencoded")) {
  63. const formData = []
  64. if (requestBody.includes("=")) {
  65. requestBody.split("&").forEach((rq) => {
  66. const [key, val] = rq.split("=")
  67. formData.push(`('${key}', '${val}')`)
  68. })
  69. }
  70. if (formData.length) {
  71. requestString.push(`data = [${formData.join(",\n ")}]\n`)
  72. }
  73. } else {
  74. requestString.push(`data = '''${requestBody}'''\n`)
  75. }
  76. requestString.push(`response = requests.request(\n`)
  77. requestString.push(` '${method}',\n`)
  78. requestString.push(` '${url}${pathName}?${queryString}',\n`)
  79. requestString.push(` data=data,\n`)
  80. }
  81. if (genHeaders.length) {
  82. requestString.push(` headers=headers,\n`)
  83. }
  84. requestString.push(`)\n\n`)
  85. requestString.push(`print(response)`)
  86. return requestString.join("")
  87. },
  88. }