From cd3cec5d481c4d40fcfa5dc908aedbf9a05d8c85 Mon Sep 17 00:00:00 2001 From: Jaishree Patidar Date: Thu, 29 Oct 2020 17:47:25 +0530 Subject: [PATCH] [Jaishree] Add C Libcurl Codegen --- .../__snapshots__/codegen.spec.js.snap | 51 ++++++++++++++++ helpers/codegen/codegen.js | 3 + helpers/codegen/generators/c-libcurl.js | 60 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 helpers/codegen/generators/c-libcurl.js diff --git a/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap b/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap index 8f5fc4b3c..691356427 100644 --- a/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap +++ b/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap @@ -1,5 +1,56 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`generate request for C LibCurl generate GET request 1`] = ` +"CURL *hnd = curl_easy_init(); +curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, \\"GET\\"); +curl_easy_setopt(hnd, CURLOPT_URL, \\"https://httpbin.org/path/to?a=b\\"); +struct curl_slist *headers = NULL; +headers = curl_slist_append(headers, \\"h1: h1v\\"); +headers = curl_slist_append(headers, \\"h2: h2v\\"); +headers = curl_slist_append(headers, \\"Authorization: Basic bW9ja1VzZXI6bW9ja1Bhc3N3b3Jk\\"); +curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); +CURLcode ret = curl_easy_perform(hnd);" +`; + +exports[`generate request for C LibCurl generate POST request for JSON 1`] = ` +"CURL *hnd = curl_easy_init(); +curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, \\"POST\\"); +curl_easy_setopt(hnd, CURLOPT_URL, \\"https://httpbin.org/path/to?a=b\\"); +struct curl_slist *headers = NULL; +headers = curl_slist_append(headers, \\"h1: h1v\\"); +headers = curl_slist_append(headers, \\"h2: h2v\\"); +headers = curl_slist_append(headers, \\"Authorization: Bearer abcdefghijklmn\\"); +headers = curl_slist_append(headers, \\"Content-Type: application/json\\"); +curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); +curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, \\"{\\\\\\"foo\\\\\\": \\\\\\"bar\\\\\\", \\\\\\"baz\\\\\\": \\\\\\"qux\\\\\\"}\\"); +CURLcode ret = curl_easy_perform(hnd);" +`; + +exports[`generate request for C LibCurl generate POST request for XML 1`] = ` +"CURL *hnd = curl_easy_init(); +curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, \\"POST\\"); +curl_easy_setopt(hnd, CURLOPT_URL, \\"https://httpbin.org/path/to?a=b\\"); +struct curl_slist *headers = NULL; +headers = curl_slist_append(headers, \\"h1: h1v\\"); +headers = curl_slist_append(headers, \\"h2: h2v\\"); +headers = curl_slist_append(headers, \\"Authorization: Bearer abcdefghijklmn\\"); +headers = curl_slist_append(headers, \\"Content-Type: application/xml\\"); +curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); +curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, \\"\\\\n\\\\n \\\\n\\"); +CURLcode ret = curl_easy_perform(hnd);" +`; + +exports[`generate request for C LibCurl generate PUT request for www-form-urlencoded 1`] = ` +"CURL *hnd = curl_easy_init(); +curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, \\"PUT\\"); +curl_easy_setopt(hnd, CURLOPT_URL, \\"https://httpbin.org/path/to?a=b\\"); +struct curl_slist *headers = NULL; +headers = curl_slist_append(headers, \\"Content-Type: application/x-www-form-urlencoded\\"); +curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); +curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, \\"foo=bar&baz=qux\\"); +CURLcode ret = curl_easy_perform(hnd);" +`; + exports[`generate request for C# RestSharp generate GET request 1`] = ` "var client = new RestClient(\\"https://httpbin.org\\"); diff --git a/helpers/codegen/codegen.js b/helpers/codegen/codegen.js index 535cce2f9..68c8f53ee 100644 --- a/helpers/codegen/codegen.js +++ b/helpers/codegen/codegen.js @@ -17,6 +17,8 @@ import { SalesforceApexCodegen } from "./generators/salesforce-apex" import { ShellHTTPie } from "./generators/shell-httpie" import { JavaOkHttpClientCodegen } from "./generators/java-ok-http-client" import { JavaUnirestCodegen } from "./generators/java-unirest" +import { CLibCurlCodeGen } from "./generators/c-libcurl" + /* Register code generators here. * A code generator is defined as an object with the following structure. * @@ -26,6 +28,7 @@ import { JavaUnirestCodegen } from "./generators/java-unirest" * */ export const codegens = [ + CLibCurlCodeGen, CurlCodegen, CsRestSharpCodegen, GoNativeCodegen, diff --git a/helpers/codegen/generators/c-libcurl.js b/helpers/codegen/generators/c-libcurl.js new file mode 100644 index 000000000..c7695f808 --- /dev/null +++ b/helpers/codegen/generators/c-libcurl.js @@ -0,0 +1,60 @@ +export const CLibCurlCodeGen = { + id: "C LibCurl", + name: "C LibCurl", + generator: ({ + auth, + httpUser, + httpPassword, + method, + url, + pathName, + queryString, + bearerToken, + headers, + rawInput, + rawParams, + rawRequestBody, + contentType, + }) => { + const requestString = [] + + requestString.push("CURL *hnd = curl_easy_init();") + requestString.push(`curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "${method}");`) + requestString.push(`curl_easy_setopt(hnd, CURLOPT_URL, "${url}${pathName}${queryString}");`) + requestString.push(`struct curl_slist *headers = NULL;`) + + if (headers) { + headers.forEach(({ key, value }) => { + if (key) requestString.push(`headers = curl_slist_append(headers, "${key}: ${value}");`) + }) + } + + if (auth === "Basic Auth") { + const basic = `${httpUser}:${httpPassword}` + requestString.push( + `headers = curl_slist_append(headers, "Authorization: Basic ${window.btoa( + unescape(encodeURIComponent(basic)) + )}");` + ) + } else if (auth === "Bearer Token" || auth === "OAuth 2.0") { + requestString.push( + `headers = curl_slist_append(headers, "Authorization: Bearer ${bearerToken}");` + ) + } + + if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) { + let requestBody = rawInput ? rawParams : rawRequestBody + + if (contentType.includes("x-www-form-urlencoded")) { + requestBody = `"${requestBody}"` + } else requestBody = JSON.stringify(requestBody) + + requestString.push(`headers = curl_slist_append(headers, "Content-Type: ${contentType}");`) + requestString.push("curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);") + requestString.push(`curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, ${requestBody});`) + } else requestString.push("curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);") + + requestString.push(`CURLcode ret = curl_easy_perform(hnd);`) + return requestString.join("\n") + }, +}