cs-restsharp.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (requestBody) {
  25. requestBody = requestBody.replace(/"/g, '""') // escape quotes for C# verbatim string compatibility
  26. }
  27. // prepare data
  28. let requestDataFormat
  29. let requestContentType
  30. if (isJSONContentType(contentType)) {
  31. requestDataFormat = "DataFormat.Json"
  32. requestContentType = "text/json"
  33. } else {
  34. requestDataFormat = "DataFormat.Xml"
  35. requestContentType = "text/xml"
  36. }
  37. const verbs = [
  38. { verb: "GET", csMethod: "Get" },
  39. { verb: "POST", csMethod: "Post" },
  40. { verb: "PUT", csMethod: "Put" },
  41. { verb: "PATCH", csMethod: "Patch" },
  42. { verb: "DELETE", csMethod: "Delete" },
  43. ]
  44. // create client and request
  45. requestString.push(`var client = new RestClient("${url}");\n\n`)
  46. requestString.push(
  47. `var request = new RestRequest("${pathName}?${queryString}", ${requestDataFormat});\n\n`
  48. )
  49. // authentification
  50. if (auth === "Basic Auth") {
  51. requestString.push(
  52. `client.Authenticator = new HttpBasicAuthenticator("${httpUser}", "${httpPassword}");\n`
  53. )
  54. } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {
  55. requestString.push(
  56. `request.AddHeader("Authorization", "Bearer ${bearerToken}");\n`
  57. )
  58. }
  59. // custom headers
  60. if (headers) {
  61. headers.forEach(({ key, value }) => {
  62. if (key) {
  63. requestString.push(`request.AddHeader("${key}", "${value}");\n`)
  64. }
  65. })
  66. }
  67. requestString.push(`\n`)
  68. // set body
  69. if (["POST", "PUT", "PATCH", "DELETE"].includes(method) && requestBody) {
  70. requestString.push(
  71. `request.AddParameter("${requestContentType}", @"${requestBody}", ParameterType.RequestBody);\n\n`
  72. )
  73. }
  74. // process
  75. const verb = verbs.find((v) => v.verb === method)
  76. if (verb) {
  77. requestString.push(`var response = client.${verb.csMethod}(request);\n\n`)
  78. } else {
  79. return ""
  80. }
  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. }