summaryrefslogtreecommitdiff
path: root/subprojects/battleNetApi/src/hearthstoneApi.cpp
diff options
context:
space:
mode:
authorAotrix <[email protected]>2025-11-09 13:23:05 +0100
committerAotrix <[email protected]>2025-11-09 13:23:05 +0100
commit6110ac9510e8fdf04e7f60625def310687ee6a88 (patch)
treec9cf823bbdc7fbdf6ffced2db3b7cbbaad83282c /subprojects/battleNetApi/src/hearthstoneApi.cpp
parent2df3eeae3a776c989920b8d07893397c2d09b9f0 (diff)
refactor HearthstoneApi methods and rename input parameters
Diffstat (limited to 'subprojects/battleNetApi/src/hearthstoneApi.cpp')
-rw-r--r--subprojects/battleNetApi/src/hearthstoneApi.cpp53
1 files changed, 27 insertions, 26 deletions
diff --git a/subprojects/battleNetApi/src/hearthstoneApi.cpp b/subprojects/battleNetApi/src/hearthstoneApi.cpp
index d531247..903e987 100644
--- a/subprojects/battleNetApi/src/hearthstoneApi.cpp
+++ b/subprojects/battleNetApi/src/hearthstoneApi.cpp
@@ -2,42 +2,30 @@
#include <curl/curl.h>
#include <format>
-HearthstoneApi::HearthstoneApi(Token &&iToken) : mToken(iToken), mHeader(generateRequestHeader()) {}
+HearthstoneApi::HearthstoneApi(Token &&iToken) : mToken(iToken), mAuthorizationHeader(authorizationHeader()) {}
-std::string HearthstoneApi::generateRequestHeader() {
- return std::format("Authorization: Bearer {}", mToken.toString());
+std::uint64_t HearthstoneApi::numberOfCards() {
+ // TODO: implement logic
+ return 123456;
}
-const std::string HearthstoneApi::getUrl(const std::string &t_parameters) {
- return std::format("https://eu.api.blizzard.com/hearthstone/{}", t_parameters);
-}
-
-const std::string HearthstoneApi::getSetOfCardsUrl(const std::string &t_card_parameters) {
- return getUrl(std::format("cards{}", t_card_parameters));
-}
+std::string HearthstoneApi::authorizationHeader() { return std::format("Authorization: Bearer {}", mToken.toString()); }
-size_t HearthstoneApi::curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s) {
- size_t newLength = t_size * t_nmemb;
- try {
- t_s->append(static_cast<char *>(t_contents), newLength);
- } catch (std::bad_alloc &e) {
- // handle memory problem
- return 0;
- }
- return newLength;
+const std::string HearthstoneApi::getUrl(const std::string &iParameters) {
+ return std::format("https://eu.api.blizzard.com/hearthstone/{}", iParameters);
}
-std::string HearthstoneApi::makeApiRequest(const char *t_url, const char *t_header) {
+std::string HearthstoneApi::makeApiRequest(const char *iUrl) {
CURL *curl = curl_easy_init();
std::string request_output;
if (curl) {
- curl_easy_setopt(curl, CURLOPT_URL, t_url);
+ curl_easy_setopt(curl, CURLOPT_URL, iUrl);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallbackStringOutput);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &request_output);
struct curl_slist *chunk = nullptr;
- chunk = curl_slist_append(chunk, t_header);
+ chunk = curl_slist_append(chunk, mAuthorizationHeader.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
@@ -45,10 +33,23 @@ std::string HearthstoneApi::makeApiRequest(const char *t_url, const char *t_head
return request_output;
}
-std::string HearthstoneApi::makeApiRequest(const std::string &t_url, const std::string &t_header) {
- return makeApiRequest(t_url.c_str(), t_header.c_str());
+const std::string HearthstoneApi::getSetOfCardsUrl(const std::string &iCardParameters) {
+ return getUrl(std::format("cards{}", iCardParameters));
}
-std::string HearthstoneApi::getCards(const std::string &t_card_parameters) {
- return makeApiRequest(getSetOfCardsUrl(t_card_parameters), mHeader);
+size_t HearthstoneApi::curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb, std::string *iS) {
+ size_t newLength = iSize * iNmemb;
+ try {
+ iS->append(static_cast<char *>(iContent), newLength);
+ } catch (std::bad_alloc &e) {
+ // handle memory problem
+ return 0;
+ }
+ return newLength;
+}
+
+std::string HearthstoneApi::makeApiRequest(const std::string &iUrl) { return makeApiRequest(iUrl.c_str()); }
+
+std::string HearthstoneApi::getCards(const std::string &iCardParameters) {
+ return makeApiRequest(getSetOfCardsUrl(iCardParameters));
}