first working fetch
This commit is contained in:
@@ -1,19 +1,75 @@
|
||||
#include "fetch.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <curl/curl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
CURL *curl;
|
||||
CURL *curl;
|
||||
|
||||
bool asana_init() {
|
||||
char asana_auth_header_str[AUTH_HEADER_SIZE];
|
||||
struct curl_slist *asana_auth_header;
|
||||
|
||||
bool asana_init(char *pat) {
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
curl = curl_easy_init();
|
||||
if (!curl) return false;
|
||||
|
||||
snprintf(asana_auth_header_str, AUTH_HEADER_SIZE, "Authorization: Bearer %s", pat);
|
||||
asana_auth_header = curl_slist_append(NULL, asana_auth_header_str);
|
||||
|
||||
if (!asana_auth_header) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void asana_cleanup() {
|
||||
curl_slist_free_all(asana_auth_header);
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
size_t asana_write_callback(void *contents, size_t size, size_t nmemb, void *resp) {
|
||||
Response *response = (Response *)resp;
|
||||
|
||||
size_t realsize = size * nmemb;
|
||||
|
||||
char *body = realloc(response->body, response->len + realsize + 1);
|
||||
response->body = body;
|
||||
memcpy(&(response->body[response->len]), contents, realsize);
|
||||
response->len += realsize;
|
||||
response->body[response->len] = '\0';
|
||||
|
||||
return realsize;
|
||||
}
|
||||
|
||||
Response *asana_fetch(char *path) {
|
||||
char url[ASANA_URL_MAX_LENGTH];
|
||||
snprintf(url, ASANA_URL_MAX_LENGTH, "https://app.asana.com/api/1.0/%s", path);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, asana_auth_header);
|
||||
|
||||
Response *response = calloc(sizeof(Response), 1);
|
||||
response->body = calloc(sizeof(char), 1);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, asana_write_callback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
|
||||
|
||||
CURLcode result;
|
||||
result = curl_easy_perform(curl);
|
||||
if (result == CURLE_OK) {
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &(response->status));
|
||||
} else {
|
||||
response->status = 500;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
void asana_free_response(Response *r) {
|
||||
free(r->body);
|
||||
free(r);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,17 @@
|
||||
#define ASANA_FETCH_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define AUTH_HEADER_SIZE 1024
|
||||
#define ASANA_URL_MAX_LENGTH 4096
|
||||
|
||||
typedef struct Response {
|
||||
int status;
|
||||
size_t len;
|
||||
char *body;
|
||||
} Response;
|
||||
|
||||
|
||||
/**
|
||||
* initialize libcurl
|
||||
@@ -13,4 +24,14 @@ bool asana_init();
|
||||
*/
|
||||
void asana_cleanup();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
Response *asana_fetch(char *path);
|
||||
|
||||
/**
|
||||
* Frees memory allocated for a Response
|
||||
*/
|
||||
void asana_free_response(Response *r);
|
||||
|
||||
#endif // ASANA_FETCH_H_
|
||||
|
||||
Reference in New Issue
Block a user