python-requests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. let requestDataObj = ""
  51. requestString.push(...printHeaders(genHeaders))
  52. if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
  53. if (contentType && requestBody) {
  54. if (isJSONContentType(contentType)) {
  55. requestBody = JSON.stringify(requestBody)
  56. requestDataObj = `data = ${requestBody}\n`
  57. } else if (contentType.includes("x-www-form-urlencoded")) {
  58. const formData = []
  59. if (requestBody.includes("=")) {
  60. requestBody.split("&").forEach((rq) => {
  61. const [key, val] = rq.split("=")
  62. formData.push(`('${key}', '${val}')`)
  63. })
  64. }
  65. if (formData.length) {
  66. requestDataObj = `data = [${formData.join(",\n ")}]\n`
  67. }
  68. } else {
  69. requestDataObj = `data = '''${requestBody}'''\n`
  70. }
  71. }
  72. }
  73. if (requestDataObj) {
  74. requestString.push(requestDataObj)
  75. }
  76. requestString.push(`response = requests.request(\n`)
  77. requestString.push(` '${method}',\n`)
  78. requestString.push(` '${url}${pathName}?${queryString}',\n`)
  79. if (requestDataObj && requestBody) {
  80. requestString.push(` data=data,\n`)
  81. }
  82. if (genHeaders.length) {
  83. requestString.push(` headers=headers,\n`)
  84. }
  85. requestString.push(`)\n\n`)
  86. requestString.push(`print(response)`)
  87. return requestString.join("")
  88. },
  89. }