Skip to content

Commit

Permalink
[Jaishree] Add C Libcurl Codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
patidar-jaishree committed Oct 29, 2020
1 parent 621888d commit cd3cec5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
51 changes: 51 additions & 0 deletions helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap
Original file line number Diff line number Diff line change
@@ -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, \\"<?xml version='1.0' encoding='utf-8'?>\\\\n<xml>\\\\n <element foo=\\\\\\"bar\\\\\\"></element>\\\\n</xml>\\");
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\\");
Expand Down
3 changes: 3 additions & 0 deletions helpers/codegen/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -26,6 +28,7 @@ import { JavaUnirestCodegen } from "./generators/java-unirest"
*
*/
export const codegens = [
CLibCurlCodeGen,
CurlCodegen,
CsRestSharpCodegen,
GoNativeCodegen,
Expand Down
60 changes: 60 additions & 0 deletions helpers/codegen/generators/c-libcurl.js
Original file line number Diff line number Diff line change
@@ -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")
},
}

0 comments on commit cd3cec5

Please sign in to comment.