summaryrefslogtreecommitdiff
path: root/battleNetApi/src/hearthstoneApi.cpp
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-02-01 02:55:28 +0100
committerAotrix <[email protected]>2026-02-01 02:55:28 +0100
commit34ba40ca622a7362eb48618deae3c91eac4b6f9a (patch)
tree997ad30acce5c04a3f8bad7af8279193f6783609 /battleNetApi/src/hearthstoneApi.cpp
parenta09398b0734a21c70e5ee3bc80cae49d49a70a22 (diff)
Download all the cards with API
Diffstat (limited to 'battleNetApi/src/hearthstoneApi.cpp')
-rw-r--r--battleNetApi/src/hearthstoneApi.cpp28
1 files changed, 27 insertions, 1 deletions
diff --git a/battleNetApi/src/hearthstoneApi.cpp b/battleNetApi/src/hearthstoneApi.cpp
index 1d2d870..7290a5d 100644
--- a/battleNetApi/src/hearthstoneApi.cpp
+++ b/battleNetApi/src/hearthstoneApi.cpp
@@ -1,14 +1,18 @@
#include "hearthstoneApi.hpp"
+#include <apiUrl.hpp>
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/multi.h>
#include <format>
+#include <fstream>
#include <iostream>
HearthstoneApi::HearthstoneApi(Token &&iToken) : mToken(iToken), mAuthorizationHeader(authorizationHeader()) {}
HearthstoneApi::HearthstoneApi(const Token &iToken) : mToken(iToken), mAuthorizationHeader(authorizationHeader()) {}
-std::string HearthstoneApi::authorizationHeader() { return std::format("Authorization: Bearer {}", mToken.text()); }
+std::string HearthstoneApi::authorizationHeader() const {
+ return std::format("Authorization: Bearer {}", mToken.text());
+}
size_t HearthstoneApi::curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb, std::string *iS) {
const size_t kNewLength = iSize * iNmemb;
@@ -97,3 +101,25 @@ std::vector<json> HearthstoneApi::makeApiRequest(const std::vector<std::string>
}
return aJsonOutputs;
}
+
+void HearthstoneApi::downloadAllCards(ApiUrl &iApiUrl, const std::string &iOutputJsonPath) const {
+ HearthstoneApi aHearthstoneApi(mToken);
+ iApiUrl.setPage("1");
+ std::vector<std::string> aRequests;
+ json aFirstPageJson = aHearthstoneApi.makeApiRequest(iApiUrl());
+
+ int kNumberOfPages{aFirstPageJson["pageCount"]};
+ for (int i{1}; i < kNumberOfPages; ++i) {
+ iApiUrl.setPage(std::to_string(i + 1));
+ aRequests.push_back(iApiUrl());
+ }
+ json::reference &aMergedCards = aFirstPageJson["cards"];
+ std::vector<json> kApiResponses = aHearthstoneApi.makeApiRequest(aRequests);
+ for (json &aPage : kApiResponses) {
+ for (json::reference &aCard : aPage["cards"]) {
+ aMergedCards.push_back(std::move(aCard));
+ }
+ }
+ std::ofstream aOutputJson(iOutputJsonPath);
+ aOutputJson << aMergedCards.dump(4);
+}