diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | common.h | 11 | ||||
-rw-r--r-- | graphql.c | 32 | ||||
-rw-r--r-- | shopify.c | 39 |
4 files changed, 39 insertions, 45 deletions
diff --git a/Makefile.am b/Makefile.am index 235bf7a..a185925 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ lib_LTLIBRARIES = libshopify.la -libshopify_la_SOURCES = shopify.c graphql.c +libshopify_la_SOURCES = shopify.c libshopify_la_CPPFLAGS = $(DEPS_CFLAGS) include_HEADERS = shopify.h pkgconfigdir = $(libdir)/pkgconfig diff --git a/common.h b/common.h deleted file mode 100644 index 44601fd..0000000 --- a/common.h +++ /dev/null @@ -1,11 +0,0 @@ -#include <stdlib.h> -#include <string.h> - -static size_t append(char *data, size_t size, size_t nmemb, char **res) -{ - size_t realsize = size * nmemb; - size_t res_len = *res ? strlen(*res) : 0; - *res = realloc(*res, res_len + realsize + 1); - strlcpy(&(*res)[res_len], data, realsize + 1); - return realsize; -} diff --git a/graphql.c b/graphql.c deleted file mode 100644 index d0e1ca8..0000000 --- a/graphql.c +++ /dev/null @@ -1,32 +0,0 @@ -#include <curl/curl.h> -#include "shopify.h" -#include "common.h" - -void shopify_graphql(const char *query, const struct shopify_session *session, - char **json) -{ - CURL *curl = curl_easy_init(); - 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; - list = curl_slist_append(list, header); - list = curl_slist_append(list, "Content-Type: application/graphql"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - - curl_easy_perform(curl); - curl_slist_free_all(list); - curl_easy_cleanup(curl); -} @@ -10,7 +10,6 @@ #include "shopify.h" #include "regex.h" #include "sessiontoken.h" -#include "common.h" #define AUTH_URL \ "https://%s/oauth/authorize?client_id=%s&scope=%s&redirect_uri=%s%s"\ @@ -120,6 +119,15 @@ static int keycmp(const void *struct1, const void *struct2) return strcmp(*(char **)struct1, *(char **)struct2); } +static size_t append(char *data, size_t size, size_t nmemb, char **res) +{ + size_t realsize = size * nmemb; + size_t res_len = *res ? strlen(*res) : 0; + *res = realloc(*res, res_len + realsize + 1); + strlcpy(&(*res)[res_len], data, realsize + 1); + return realsize; +} + static inline int redirect(const char *host, const char *id, struct MHD_Connection *con, struct MHD_Response **res) { @@ -534,3 +542,32 @@ void shopify_app(const char *api_key, const char *api_secret_key, free(sessions); curl_global_cleanup(); } + +void shopify_graphql(const char *query, const struct shopify_session *session, + char **json) +{ + CURL *curl = curl_easy_init(); + 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; + list = curl_slist_append(list, header); + list = curl_slist_append(list, "Content-Type: application/graphql"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + + curl_easy_perform(curl); + curl_slist_free_all(list); + curl_easy_cleanup(curl); +} |