weechat-chatwork/api.c

38 行
920 B
C

#include <stdlib.h>
#include <curl/curl.h>
#include <cjson/cJSON.h>
#include "commands.h"
#include "api.h"
ApiResponse makeApiRequest (const ApiRequest *request) {
ApiResponse response = {0};
CURL *curl = curl_easy_init();
if (!curl) {
return response;
}
Buffer readBuffer = { NULL, 0 };
struct curl_slist *headers = NULL;
char header[256];
snprintf(header, sizeof(header), "X-ChatWorkToken: %s", request->token);
headers = curl_slist_append(headers, header);
curl_easy_setopt(curl, CURLOPT_URL, request->url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
response.result = curl_easy_perform(curl);
response.data = cJSON_Parse(readBuffer.data);
free(readBuffer.data);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return response;
}