diff options
-rw-r--r-- | request.c | 25 | ||||
-rw-r--r-- | request.h | 28 |
2 files changed, 27 insertions, 26 deletions
@@ -1,9 +1,6 @@ #include "request.h" #include "shopify.h" -#define GRAPHQL_URL "https://%s/admin/api/2022-07/graphql.json" -#define GRAPHQL_URL_LEN strlen(GRAPHQL_URL) - strlen("%s") - extern inline void request_gettoken(const char *, const char *, const char *, const char *, char **); @@ -11,18 +8,26 @@ void shopify_graphql(const char *query, const struct shopify_session *session, char **json) { CURL *curl = curl_easy_init(); - char url[GRAPHQL_URL_LEN + strlen(session->shop) + 1]; - sprintf(url, GRAPHQL_URL, session->shop); - curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, json); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query); + + static const char *url_tmpl + = "https://%s/admin/api/2022-07/graphql.json"; + char url[strlen(url_tmpl) - strlen("%s") + strlen(session->shop) + 1]; + sprintf(url, url_tmpl, session->shop); + curl_easy_setopt(curl, CURLOPT_URL, url); + + static const char *hdr_tmpl = "X-Shopify-Access-Token: %s"; + char header[strlen(hdr_tmpl) - strlen("%s") + + strlen(session->access_token) + 1]; + sprintf(header, hdr_tmpl, session->access_token); + struct curl_slist *list = NULL; - char header[TOKEN_HEADER_LEN + strlen(session->access_token) + 1]; - sprintf(header, TOKEN_HEADER, session->access_token); list = curl_slist_append(list, header); list = curl_slist_append(list, "Content-Type: application/graphql"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, json); + curl_easy_perform(curl); curl_slist_free_all(list); curl_easy_cleanup(curl); @@ -2,15 +2,6 @@ #include <string.h> #include <curl/curl.h> -#define TOKEN_URL "https://%s/oauth/access_token" -#define TOKEN_URL_LEN strlen(TOKEN_URL) - strlen("%s") - -#define TOKEN_POST "client_id=%s&client_secret=%s&code=%s" -#define TOKEN_POST_LEN strlen(TOKEN_POST) - strlen("%s") * 3 - -#define TOKEN_HEADER "X-Shopify-Access-Token: %s" -#define TOKEN_HEADER_LEN strlen(TOKEN_HEADER) - strlen("%s") - static size_t append(char *data, size_t size, size_t nmemb, char **res) { size_t realsize = size * nmemb; @@ -24,15 +15,20 @@ static inline void request_gettoken(const char *host, const char *key, const char *secret_key, const char *code, char **json) { CURL *curl = curl_easy_init(); - char url[TOKEN_URL_LEN + strlen(host) + 1]; - sprintf(url, TOKEN_URL, host); - curl_easy_setopt(curl, CURLOPT_URL, url); - char post[TOKEN_POST_LEN + strlen(key) + strlen(secret_key) - + strlen(code) + 1]; - sprintf(post, TOKEN_POST, key, secret_key, code); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); curl_easy_setopt(curl, CURLOPT_WRITEDATA, json); + + static const char *url_tmpl = "https://%s/oauth/access_token"; + char url[strlen(url_tmpl) - strlen("%s") + strlen(host) + 1]; + sprintf(url, url_tmpl, host); + curl_easy_setopt(curl, CURLOPT_URL, url); + + static const char *post_tmpl = "client_id=%s&client_secret=%s&code=%s"; + char post[strlen(post_tmpl) - strlen("%s") * 3 + strlen(key) + + strlen(secret_key) + strlen(code) + 1]; + sprintf(post, post_tmpl, key, secret_key, code); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); + curl_easy_perform(curl); curl_easy_cleanup(curl); } |