major feat: auth v1

This commit is contained in:
Matthew Toro 2026-04-03 21:39:59 -04:00
parent 0fa5ae3037
commit 1daae8e2a8
15 changed files with 627 additions and 100 deletions

View file

@ -9,13 +9,15 @@ static size_t writeCallback(char *data, size_t size, size_t nmemb, void *userdat
return size * nmemb;
}
static HttpResponse performRequest(CURL *curl)
static HttpResponse performRequest(CURL *curl, struct curl_slist *extraHeaders = nullptr)
{
std::string responseBody;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if (extraHeaders)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, extraHeaders);
CURLcode res = curl_easy_perform(curl);
@ -24,20 +26,30 @@ static HttpResponse performRequest(CURL *curl)
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode);
curl_easy_cleanup(curl);
if (extraHeaders)
curl_slist_free_all(extraHeaders);
return {statusCode, std::move(responseBody)};
}
HttpResponse HttpClient::get(const std::string &url)
static struct curl_slist *buildHeaders(const std::vector<std::string> &headers)
{
struct curl_slist *list = nullptr;
for (const auto &h : headers)
list = curl_slist_append(list, h.c_str());
return list;
}
HttpResponse HttpClient::get(const std::string &url, const std::vector<std::string> &headers)
{
CURL *curl = curl_easy_init();
if (!curl)
return {0, ""};
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
return performRequest(curl);
return performRequest(curl, headers.empty() ? nullptr : buildHeaders(headers));
}
HttpResponse HttpClient::post(const std::string &url, const std::string &body, const std::string &contentType)
HttpResponse HttpClient::post(const std::string &url, const std::string &body, const std::string &contentType, const std::vector<std::string> &headers)
{
CURL *curl = curl_easy_init();
if (!curl)
@ -47,11 +59,8 @@ HttpResponse HttpClient::post(const std::string &url, const std::string &body, c
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(body.size()));
struct curl_slist *headers = nullptr;
headers = curl_slist_append(headers, ("Content-Type: " + contentType).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
auto *headerList = buildHeaders(headers);
headerList = curl_slist_append(headerList, ("Content-Type: " + contentType).c_str());
HttpResponse resp = performRequest(curl);
curl_slist_free_all(headers);
return resp;
return performRequest(curl, headerList);
}