ahmadnassri/httpsnippet

View on GitHub
src/targets/c/libcurl/client.ts

Summary

Maintainability
A
30 mins
Test Coverage
import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../helpers/escape';
import { Client } from '../../targets';

export const libcurl: Client = {
  info: {
    key: 'libcurl',
    title: 'Libcurl',
    link: 'http://curl.haxx.se/libcurl',
    description: 'Simple REST and HTTP API Client for C',
  },
  convert: ({ method, fullUrl, headersObj, allHeaders, postData }) => {
    const { push, blank, join } = new CodeBuilder();

    push('CURL *hnd = curl_easy_init();');
    blank();
    push(`curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "${method.toUpperCase()}");`);
    push(`curl_easy_setopt(hnd, CURLOPT_URL, "${fullUrl}");`);

    // Add headers, including the cookies
    const headers = Object.keys(headersObj);

    // construct headers
    if (headers.length) {
      blank();
      push('struct curl_slist *headers = NULL;');

      headers.forEach(header => {
        push(`headers = curl_slist_append(headers, "${header}: ${escapeForDoubleQuotes(headersObj[header])}");`);
      });

      push('curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);');
    }

    // construct cookies
    if (allHeaders.cookie) {
      blank();
      push(`curl_easy_setopt(hnd, CURLOPT_COOKIE, "${allHeaders.cookie}");`);
    }

    if (postData.text) {
      blank();
      push(`curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, ${JSON.stringify(postData.text)});`);
    }

    blank();
    push('CURLcode ret = curl_easy_perform(hnd);');

    return join();
  },
};