api.c

← Back to explorer
src/api.c
#include <curl/curl.h>
#include <cjson/cJSON.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "api.h"

typedef struct {
  char *data;
  size_t size;
} http_response_buffer;

static size_t write_to_buffer(void *content, size_t size, size_t number_of_content_elements, void *userdata) {
  size_t real_chunk_size = size * number_of_content_elements;
  http_response_buffer *memory = (http_response_buffer *) userdata;
  char *pChar = realloc(memory->data, memory->size + real_chunk_size + 1);

  if(!pChar) {
    return 0;
  }
  
  memory->data = pChar;
  memcpy(&(memory->data[memory->size]), content, real_chunk_size);
  memory->size += real_chunk_size;
  memory->data[memory->size] = '\0';
  return real_chunk_size;
}