From 77f8f4a9c3f6a83f965fad7cc6010476f784584d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Thu, 22 Sep 2022 15:20:09 +0800 Subject: Use static const char * instead of macro --- request.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'request.c') diff --git a/request.c b/request.c index 8b97c7d..099491b 100644 --- a/request.c +++ b/request.c @@ -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); -- cgit v1.2.3