clients/c/sky-island.c
/* function is used to make the call to the API and returns back either NULL or
 * a pointer to a response_t. This memory will need to be freed by the caller */
function is used to make the call to the API and returns back either NULL or
a pointer to a response_t. This memory will need to be freed by the caller
function is used to make the call to the API and returns back either NULL or a pointer to a response_t . This memory will need to be freed by the caller
response_t* function(client_opt_t* client, const char* url, const char* call) {
  CURL* ch;
  CURLcode ret_code;
  json_object* json;
  enum json_tokener_error jerr = json_tokener_success;
  struct curl_fetch_st curl_fetch;
  struct curl_fetch_st* cf = &curl_fetch;
  struct curl_slist* headers = NULL;
  if ((ch = curl_easy_init()) == NULL) {
    fprintf(stderr, "ERROR: Failed to create curl handle in fetch_session");
    return NULL;
  }
  headers = curl_slist_append(headers, "Accept: application/json");
  headers = curl_slist_append(headers, "Content-Type: application/json");
  json = json_object_new_object();
  json_object_object_add(json, "url", json_object_new_string(url));
  json_object_object_add(json, "call", json_object_new_string(call));
  curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(ch, CURLOPT_POSTFIELDS, json_object_to_json_string(json));
  if (client->skip_peer_verify) {
    curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
  }
  if (client->skip_host_verify) {
    curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
  }
  ret_code = curl_fetch_url(ch, client->endpoint, cf);
  curl_easy_cleanup(ch);
  curl_slist_free_all(headers);
  json_object_put(json);
  if (ret_code != CURLE_OK || cf->size < 1) {
    fprintf(stderr, "ERROR: Failed to fetch url (%s) - curl said: %s", url,
            curl_easy_strerror(ret_code));
    return NULL;
  }
  if (cf->payload == NULL) {
    fprintf(stderr, "ERROR: Failed to populate payload");
    free(cf->payload);
    return NULL;
  }
  json = json_tokener_parse_verbose(cf->payload, &jerr);
  free(cf->payload);
  if (jerr != json_tokener_success) {
    fprintf(stderr, "ERROR: Failed to parse json string");
    json_object_put(json);
    return NULL;
  }
  response_t* res = malloc(sizeof(response_t));
  res->timestamp = (unsigned long)time(NULL);
  res->data = strdup(json_object_to_json_string(json));
  json_object_put(json);
  return res;
}
response_t * function ( client_opt_t * client , const char * url , const char * call ) { CURL * ch ; CURLcode ret_code ; json_object * json ; enum json_tokener_error jerr = json_tokener_success ; struct curl_fetch_st curl_fetch ; struct curl_fetch_st * cf = & curl_fetch ; struct curl_slist * headers = NULL ; if ( ( ch = curl_easy_init ( ) ) == NULL ) { fprintf ( stderr , " " ) ; return NULL ; } headers = curl_slist_append ( headers , " " ) ; headers = curl_slist_append ( headers , " " ) ; json = json_object_new_object ( ) ; json_object_object_add ( json , " " , json_object_new_string ( url ) ) ; json_object_object_add ( json , " " , json_object_new_string ( call ) ) ; curl_easy_setopt ( ch , CURLOPT_CUSTOMREQUEST , " " ) ; curl_easy_setopt ( ch , CURLOPT_HTTPHEADER , headers ) ; curl_easy_setopt ( ch , CURLOPT_POSTFIELDS , json_object_to_json_string ( json ) ) ; if ( client -> skip_peer_verify ) { curl_easy_setopt ( ch , CURLOPT_SSL_VERIFYPEER , 0L ) ; } if ( client -> skip_host_verify ) { curl_easy_setopt ( ch , CURLOPT_SSL_VERIFYHOST , 0L ) ; } ret_code = curl_fetch_url ( ch , client -> endpoint , cf ) ; curl_easy_cleanup ( ch ) ; curl_slist_free_all ( headers ) ; json_object_put ( json ) ; if ( ret_code != CURLE_OK || cf -> size < 1 ) { fprintf ( stderr , " " , url , curl_easy_strerror ( ret_code ) ) ; return NULL ; } if ( cf -> payload == NULL ) { fprintf ( stderr , " " ) ; free ( cf -> payload ) ; return NULL ; } json = json_tokener_parse_verbose ( cf -> payload , & jerr ) ; free ( cf -> payload ) ; if ( jerr != json_tokener_success ) { fprintf ( stderr , " " ) ; json_object_put ( json ) ; return NULL ; } response_t * res = malloc ( sizeof ( response_t ) ) ; res -> timestamp = ( unsigned long ) time ( NULL ) ; res -> data = strdup ( json_object_to_json_string ( json ) ) ; json_object_put ( json ) ; return res ; }
function is used to make the call to the API and returns back either NULL or
 a pointer to a response_t.
function is used to make the call to the API and returns back either NULL or a pointer to a response_t .
// free the memory used by the JSON call