diff options
Diffstat (limited to 'src/curl.c')
-rw-r--r-- | src/curl.c | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/src/curl.c b/src/curl.c new file mode 100644 index 0000000..4046d3d --- /dev/null +++ b/src/curl.c @@ -0,0 +1,177 @@ +#include <string.h> +#include "util.h" +#include "curl.h" + +const char* +get_apikey() +{ + return check_get_env("MINIFLUX_APIKEY"); +} + +const char * +get_url() +{ + return check_get_env("MINIFLUX_URL"); +} + +char * +get_endpoint(char* endpoint) +{ + char *url = calloc(sizeof(char), PATH_MAX); + snprintf(url, PATH_MAX, "%s/v1/%s", get_url(), endpoint); + return url; +} + +bool +curl_init() +{ + curl = curl_easy_init(); + + if (!curl) { + fprintf(stderr, "Error in curl init\n"); + return false; + } + + api_header = calloc(sizeof(char), MAX_URL); + snprintf(api_header, sizeof(char) * MAX_URL, "X-Auth-Token: %s", get_apikey()); + +#ifdef DEBUG + fprintf(stderr, "api: %s\n", api_header); +#endif + + return true; +} + +size_t +parse_header_reply( + char* ptr, size_t size, size_t nmemb, struct reply *userdata) +{ + size_t s = 0; + if (sscanf(ptr, "Content-Length: %ld\n", &s) != 0) { + userdata->size = s + 1; +#ifdef DEBUG + printf("Len: %ld\n", s); +#endif + } + return size * nmemb; +} + +size_t +parse_body_reply( + char *ptr, size_t size, size_t nmemb, struct reply *userdata) +{ + if (userdata->size == 0) { + return 0; + } + size_t realsize = size * nmemb; + char *tmp = realloc(userdata->data, userdata->size); + if (!tmp) { + fprintf(stderr, "Not enough memory\n"); + return 0; + } else { + userdata->data = tmp; + } + + strncat(userdata->data, ptr, realsize); + return realsize; +} + +struct feed * +get_remote_feeds() +{ + CURLcode res; + struct curl_slist *header = NULL; + + char *url = get_endpoint("feeds"); + curl_easy_setopt(curl, CURLOPT_URL, url); + free(url); + header = curl_slist_append(header, api_header); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); + + struct reply data = {0}; + data.size = 0; + data.data = malloc(1); + data.data[0] = '\0'; + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &parse_body_reply); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); + curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &parse_header_reply); + curl_easy_setopt(curl, CURLOPT_HEADERDATA, &data); + + + res = curl_easy_perform(curl); + if (res != CURLE_OK) { + fprintf(stderr, "Curl failed: %s", curl_easy_strerror(res)); + return NULL; + } + +#ifdef DEBUG + printf("%s\n", data.data); +#endif + + struct feed *feeds = calloc(sizeof(struct feed), 1); + // TODO: populate feed; + free(data.data); + curl_slist_free_all(header); + return feeds; +} + +void +curl_clean() +{ + curl_easy_cleanup(curl); + free(api_header); +} + +size_t +print_reply( + char *ptr, size_t size, size_t nmemb, void *null) +{ + printf("%s\n", ptr); + return size * nmemb; +} + +void +remote_add_feed(struct feed feed) +{ + char *json = calloc(sizeof(char), 4096); + snprintf(json, 4096, "{ \"feed_url\": \"%s\", \"category_id\": 1 }", feed.url); + char *url = get_endpoint("feeds"); + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json); + + struct curl_slist *header = NULL; + header = curl_slist_append(header, api_header); + header = curl_slist_append(header, "Content-Type: application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &print_reply); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL); + + curl_easy_perform(curl); + free(url); + free(json); + curl_slist_free_all(header); +} + +bool +create_category(char *title) +{ + char *json = calloc(sizeof(char), 4096); + snprintf(json, 4096, "{ \"title\": \"%s\" }", title); + char *url = get_endpoint("categories"); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json); + struct curl_slist *header = NULL; + header = curl_slist_append(header, api_header); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &print_reply); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL); + + curl_easy_perform(curl); + free(url); + free(json); + curl_slist_free_all(header); + + return true; +} |