Mercurial > sci
diff libsci/apic.c @ 50:b474f0985e39
scictl: add API key support
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 17 Aug 2022 09:11:58 +0200 |
parents | 1d0ddf9e6efd |
children | 38901547a76c |
line wrap: on
line diff
--- a/libsci/apic.c Wed Aug 17 09:11:44 2022 +0200 +++ b/libsci/apic.c Wed Aug 17 09:11:58 2022 +0200 @@ -31,6 +31,7 @@ CURL *curl; CURLcode code; struct curl_slist *headers; + char keyhdr[128]; }; struct apiconf apiconf = { @@ -77,30 +78,30 @@ return fp; } -static struct curlpack -create_curl(FILE *fp, const char *body, const char *url) +static void +create_curl(struct curlpack *pack, FILE *fp, const char *body, const char *url) { - struct curlpack pack = {0}; + /* Create API key string. */ + snprintf(pack->keyhdr, sizeof (pack->keyhdr), "X-Api-Key: %s", apiconf.key); - pack.headers = curl_slist_append(pack.headers, "Content-Type: application/json"); - pack.curl = curl_easy_init(); - curl_easy_setopt(pack.curl, CURLOPT_HTTPHEADER, pack.headers); - curl_easy_setopt(pack.curl, CURLOPT_URL, url); - curl_easy_setopt(pack.curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(pack.curl, CURLOPT_TIMEOUT, 3L); - curl_easy_setopt(pack.curl, CURLOPT_WRITEFUNCTION, writer); - curl_easy_setopt(pack.curl, CURLOPT_WRITEDATA, fp); - curl_easy_setopt(pack.curl, CURLOPT_NOSIGNAL, 1L); + pack->headers = curl_slist_append(pack->headers, "Content-Type: application/json"); + pack->headers = curl_slist_append(pack->headers, pack->keyhdr); + pack->curl = curl_easy_init(); + curl_easy_setopt(pack->curl, CURLOPT_HTTPHEADER, pack->headers); + curl_easy_setopt(pack->curl, CURLOPT_URL, url); + curl_easy_setopt(pack->curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(pack->curl, CURLOPT_TIMEOUT, 3L); + curl_easy_setopt(pack->curl, CURLOPT_WRITEFUNCTION, writer); + curl_easy_setopt(pack->curl, CURLOPT_WRITEDATA, fp); + curl_easy_setopt(pack->curl, CURLOPT_NOSIGNAL, 1L); /* Assume POST request if there is a body. */ if (body) { - curl_easy_setopt(pack.curl, CURLOPT_POSTFIELDS, body); - curl_easy_setopt(pack.curl, CURLOPT_POSTFIELDSIZE, strlen(body)); + curl_easy_setopt(pack->curl, CURLOPT_POSTFIELDS, body); + curl_easy_setopt(pack->curl, CURLOPT_POSTFIELDSIZE, strlen(body)); } - pack.code = curl_easy_perform(pack.curl); - - return pack; + pack->code = curl_easy_perform(pack->curl); } static json_t * @@ -111,13 +112,13 @@ size_t responsesz; json_t *doc = NULL; json_error_t error; - struct curlpack curl; + struct curlpack curl = {0}; memset(req, 0, sizeof (*req)); url = create_url(fmt, ap); fp = create_file(&response, &responsesz); - curl = create_curl(fp, body, url); + create_curl(&curl, fp, body, url); /* Perform that request now. */ fclose(fp); @@ -151,9 +152,6 @@ ret = perform(req, NULL, fmt, ap); va_end(ap); - if (!ret || (!json_is_object(ret) && !json_is_array(ret))) - snprintf(req->error, sizeof (req->error), "invalid JSON document received"); - return ret; } @@ -177,7 +175,7 @@ free(body); json_decref(ret); - return 0; + return req->error[0] ? -1 : 0; } json_t *