cs-restsharp.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { isJSONContentType } from "~/helpers/utils/contenttypes"
  2. export const CsRestsharpCodegen = {
  3. id: "cs-restsharp",
  4. name: "C# RestSharp",
  5. language: "csharp",
  6. generator: ({
  7. url,
  8. pathName,
  9. queryString,
  10. auth,
  11. httpUser,
  12. httpPassword,
  13. bearerToken,
  14. method,
  15. rawInput,
  16. rawParams,
  17. rawRequestBody,
  18. contentType,
  19. headers,
  20. }) => {
  21. const requestString = []
  22. // initial request setup
  23. let requestBody = rawInput ? rawParams : rawRequestBody
  24. requestBody = requestBody.replace(/"/g, '""') // escape quotes for C# verbatim string compatibility
  25. // prepare data
  26. let requestDataFormat
  27. let requestContentType
  28. if (isJSONContentType(contentType)) {
  29. requestDataFormat = "DataFormat.Json"
  30. requestContentType = "text/json"
  31. } else {
  32. requestDataFormat = "DataFormat.Xml"
  33. requestContentType = "text/xml"
  34. }
  35. const verbs = [
  36. { verb: "GET", csMethod: "Get" },
  37. { verb: "POST", csMethod: "Post" },
  38. { verb: "PUT", csMethod: "Put" },
  39. { verb: "PATCH", csMethod: "Patch" },
  40. { verb: "DELETE", csMethod: "Delete" },
  41. ]
  42. // create client and request
  43. requestString.push(`var client = new RestClient("${url}");\n\n`)
  44. requestString.push(
  45. `var request = new RestRequest("${pathName}?${queryString}", ${requestDataFormat});\n\n`
  46. )
  47. // authentification
  48. if (auth === "Basic Auth") {
  49. requestString.push(
  50. `client.Authenticator = new HttpBasicAuthenticator("${httpUser}", "${httpPassword}");\n`
  51. )
  52. } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  53. requestString.push(
  54. `request.AddHeader("Authorization", "Bearer ${bearerToken}");\n`
  55. )
  56. }
  57. // content type
  58. if (contentType) {
  59. requestString.push(
  60. `request.AddHeader("Content-Type", "${contentType}");\n`
  61. )
  62. }
  63. // custom headers
  64. if (headers) {
  65. headers.forEach(({ key, value }) => {
  66. if (key) {
  67. requestString.push(`request.AddHeader("${key}", "${value}");\n`)
  68. }
  69. })
  70. }
  71. requestString.push(`\n`)
  72. // set body
  73. if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
  74. requestString.push(
  75. `request.AddParameter("${requestContentType}", @"${requestBody}", ParameterType.RequestBody);\n\n`
  76. )
  77. }
  78. // process
  79. const verb = verbs.find((v) => v.verb === method)
  80. requestString.push(`var response = client.${verb.csMethod}(request);\n\n`)
  81. // analyse result
  82. requestString.push(
  83. `if (!response.IsSuccessful)\n{\n Console.WriteLine("An error occurred " + response.ErrorMessage);\n}\n\n`
  84. )
  85. requestString.push(`var result = response.Content;\n`)
  86. return requestString.join("")
  87. },
  88. }