summaryrefslogtreecommitdiff
path: root/subprojects
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects')
-rw-r--r--subprojects/battleNetApi/CMakeLists.txt7
-rw-r--r--subprojects/battleNetApi/include/hearthstoneApi.hpp42
-rw-r--r--subprojects/battleNetApi/include/token.hpp21
-rw-r--r--subprojects/battleNetApi/src/hearthstoneApi.cpp68
-rw-r--r--subprojects/battleNetApi/src/token.cpp96
-rw-r--r--subprojects/battleNetApi/tests/tests.cpp28
6 files changed, 201 insertions, 61 deletions
diff --git a/subprojects/battleNetApi/CMakeLists.txt b/subprojects/battleNetApi/CMakeLists.txt
index b2d949a..51c4244 100644
--- a/subprojects/battleNetApi/CMakeLists.txt
+++ b/subprojects/battleNetApi/CMakeLists.txt
@@ -14,10 +14,11 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_definitions(-DSOURCE_DIR_PATH="${CMAKE_SOURCE_DIR}")
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
-set(SRC ${PROJECT_SOURCE_DIR}/src/hearthstoneApi.cpp)
-set(SRC_TESTS ${PROJECT_SOURCE_DIR}/tests/tests.cpp)
+set(SRC
+ ${PROJECT_SOURCE_DIR}/src/hearthstoneApi.cpp
+ ${PROJECT_SOURCE_DIR}/src/token.cpp ${PROJECT_SOURCE_DIR}/tests/tests.cpp)
find_package(Catch2 3 REQUIRED)
-add_executable(tests ${SRC} ${SRC_TESTS})
+add_executable(tests ${SRC})
target_include_directories(tests PRIVATE ${INCLUDE_DIR})
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain curl)
diff --git a/subprojects/battleNetApi/include/hearthstoneApi.hpp b/subprojects/battleNetApi/include/hearthstoneApi.hpp
index f677080..fcd84c0 100644
--- a/subprojects/battleNetApi/include/hearthstoneApi.hpp
+++ b/subprojects/battleNetApi/include/hearthstoneApi.hpp
@@ -1,25 +1,37 @@
#pragma once
+#include "token.hpp"
#include <chrono>
#include <string>
-class HearthstoneAPI {
+class HearthstoneApi {
public:
- HearthstoneAPI();
- std::string get_cards(const std::string &t_card_parameters);
+ HearthstoneApi();
+ std::string getCards(const std::string &t_card_parameters);
+
+ friend class HearthstoneApiTests;
private:
- static size_t curl_callback_string_output(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s);
- std::chrono::seconds get_current_time();
- std::string extract_string_with_regex(const std::string &t_str, const std::string t_regex);
- void write_new_token_info_to_file(const std::string &t_file_path);
- void refresh_token_if_needed();
- std::string get_valid_token();
- std::string generate_request_header();
- const std::string get_url(const std::string &t_parameters);
- const std::string get_set_of_cards_url(const std::string &t_card_parameters);
- std::string make_api_request(const char *t_url, const char *t_header);
- std::string make_api_request(const std::string &t_url, const std::string &t_header);
+ /**
+ * @brief Get a valid token to call Battle.net API
+ * @return A valid token
+ */
+ std::string getValidTokenWithApi() const;
+ static size_t curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s);
+ std::chrono::seconds getCurrentTime() const;
+ std::string extractStringWithRegex(const std::string &t_str, const std::string t_regex) const;
+ void writeTokenToFile(const std::string &iToken) const;
+ void writeNewTokenToFile() const;
+ void refreshToken();
+ std::string getValidToken();
+ std::string generateRequestHeader();
+ const std::string getUrl(const std::string &t_parameters);
+ const std::string getSetOfCardsUrl(const std::string &t_card_parameters);
+ std::string makeApiRequest(const char *t_url, const char *t_header);
+ std::string makeApiRequest(const std::string &t_url, const std::string &t_header);
- std::string m_access_token;
+ // TODO: add Token object
+ // Token mToken;
+ std::string mTokenFilePath;
+ std::string mToken;
std::string m_header;
};
diff --git a/subprojects/battleNetApi/include/token.hpp b/subprojects/battleNetApi/include/token.hpp
new file mode 100644
index 0000000..9c1eb18
--- /dev/null
+++ b/subprojects/battleNetApi/include/token.hpp
@@ -0,0 +1,21 @@
+#include <chrono>
+#include <string>
+
+class Token {
+ public:
+ Token(const std::string &iTokenFilePath);
+ std::string getValidToken() const;
+
+ private:
+ std::string mFilePath; //!< Path to the token file
+
+ void refreshToken() const;
+ void writeNewTokenToFile() const;
+ void writeTokenToFile(const std::string &iToken) const;
+ std::string getValidTokenWithApi() const;
+ std::chrono::seconds getCurrentTime() const;
+ std::string extractFirstMatchRegex(const std::string &t_str, const std::string &t_regex) const;
+ static size_t curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s);
+
+ friend class TokenTests;
+};
diff --git a/subprojects/battleNetApi/src/hearthstoneApi.cpp b/subprojects/battleNetApi/src/hearthstoneApi.cpp
index 2b15179..706906a 100644
--- a/subprojects/battleNetApi/src/hearthstoneApi.cpp
+++ b/subprojects/battleNetApi/src/hearthstoneApi.cpp
@@ -6,12 +6,14 @@
#include <iostream>
#include <regex>
-HearthstoneAPI::HearthstoneAPI() : m_access_token(get_valid_token()), m_header(generate_request_header()) {}
+HearthstoneApi::HearthstoneApi()
+ : mTokenFilePath(std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH)), mToken(getValidToken()),
+ m_header(generateRequestHeader()) {}
-size_t HearthstoneAPI::curl_callback_string_output(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s) {
+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((char *)t_contents, newLength);
+ t_s->append(static_cast<char *>(t_contents), newLength);
} catch (std::bad_alloc &e) {
// handle memory problem
return 0;
@@ -19,26 +21,25 @@ size_t HearthstoneAPI::curl_callback_string_output(void *t_contents, size_t t_si
return newLength;
}
-std::chrono::seconds HearthstoneAPI::get_current_time() {
+std::chrono::seconds HearthstoneApi::getCurrentTime() const {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch());
}
-std::string HearthstoneAPI::extract_string_with_regex(const std::string &t_str, const std::string t_regex) {
+std::string HearthstoneApi::extractStringWithRegex(const std::string &t_str, const std::string t_regex) const {
std::regex regex(t_regex);
auto it = std::sregex_iterator(t_str.begin(), t_str.end(), regex);
std::smatch match = *it;
return match[1].str();
}
-void HearthstoneAPI::write_new_token_info_to_file(const std::string &t_file_path) {
- std::ofstream token_file(t_file_path);
+std::string HearthstoneApi::getValidTokenWithApi() const {
CURL *curl = curl_easy_init();
std::string token_info;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback_string_output);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallbackStringOutput);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info);
curl_easy_setopt(curl, CURLOPT_USERPWD,
"2b50f1123b424d5e9c21971a110fdd04:"
@@ -52,56 +53,63 @@ void HearthstoneAPI::write_new_token_info_to_file(const std::string &t_file_path
std::exit(EXIT_FAILURE);
}
}
- std::string token = extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\"");
- std::chrono::seconds expires_at = get_current_time() + std::chrono::hours(24);
- token_file << token << " " << expires_at.count() << '\n';
+
+ return extractStringWithRegex(token_info, "\"access_token\":\"(\\w+)\"");
+}
+
+void HearthstoneApi::writeNewTokenToFile() const {
+ std::string aToken{getValidTokenWithApi()};
+ writeTokenToFile(aToken);
+}
+
+void HearthstoneApi::writeTokenToFile(const std::string &iToken) const {
+ std::ofstream token_file(mTokenFilePath);
+ std::chrono::seconds expires_at = getCurrentTime() + std::chrono::hours(24);
+ token_file << iToken << " " << expires_at.count() << '\n';
}
-void HearthstoneAPI::refresh_token_if_needed() {
+void HearthstoneApi::refreshToken() {
std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH);
std::ifstream token_file(file_path);
std::string line;
std::getline(token_file, line);
if (line.empty()) {
- write_new_token_info_to_file(file_path);
+ writeNewTokenToFile();
} else {
long expire_at = std::stoull(line.substr(line.find(" ") + 1));
if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch())
.count() >= expire_at) {
- write_new_token_info_to_file(file_path);
+ writeNewTokenToFile();
}
}
}
-std::string HearthstoneAPI::get_valid_token() {
- refresh_token_if_needed();
- std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH);
- std::ifstream token_file(file_path);
+std::string HearthstoneApi::getValidToken() {
+ refreshToken();
+ std::ifstream token_file(mTokenFilePath);
std::string line;
std::getline(token_file, line);
std::string token = line.substr(0, line.find(" "));
return token;
}
-std::string HearthstoneAPI::generate_request_header() {
- return std::format("Authorization: Bearer {}", m_access_token);
-}
+std::string HearthstoneApi::generateRequestHeader() { return std::format("Authorization: Bearer {}", mToken); }
-const std::string HearthstoneAPI::get_url(const std::string &t_parameters) {
+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::get_set_of_cards_url(const std::string &t_card_parameters) {
- return get_url(std::format("cards{}", t_card_parameters));
+const std::string HearthstoneApi::getSetOfCardsUrl(const std::string &t_card_parameters) {
+ return getUrl(std::format("cards{}", t_card_parameters));
}
-std::string HearthstoneAPI::make_api_request(const char *t_url, const char *t_header) {
+std::string HearthstoneApi::makeApiRequest(const char *t_url, const char *t_header) {
CURL *curl = curl_easy_init();
std::string request_output;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, t_url);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback_string_output);
+ 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);
@@ -112,10 +120,10 @@ std::string HearthstoneAPI::make_api_request(const char *t_url, const char *t_he
return request_output;
}
-std::string HearthstoneAPI::make_api_request(const std::string &t_url, const std::string &t_header) {
- return make_api_request(t_url.c_str(), t_header.c_str());
+std::string HearthstoneApi::makeApiRequest(const std::string &t_url, const std::string &t_header) {
+ return makeApiRequest(t_url.c_str(), t_header.c_str());
}
-std::string HearthstoneAPI::get_cards(const std::string &t_card_parameters) {
- return make_api_request(get_set_of_cards_url(t_card_parameters), m_header);
+std::string HearthstoneApi::getCards(const std::string &t_card_parameters) {
+ return makeApiRequest(getSetOfCardsUrl(t_card_parameters), m_header);
}
diff --git a/subprojects/battleNetApi/src/token.cpp b/subprojects/battleNetApi/src/token.cpp
new file mode 100644
index 0000000..7d2e20e
--- /dev/null
+++ b/subprojects/battleNetApi/src/token.cpp
@@ -0,0 +1,96 @@
+#include "token.hpp"
+#include <chrono>
+#include <curl/curl.h>
+#include <format>
+#include <fstream>
+#include <iostream>
+#include <regex>
+
+Token::Token(const std::string &iTokenFilePath) : mFilePath{iTokenFilePath} {}
+
+std::string Token::getValidToken() const {
+ refreshToken();
+ std::ifstream token_file(mFilePath);
+ std::string line;
+ std::getline(token_file, line);
+ std::string token = line.substr(0, line.find(" "));
+ return token;
+}
+
+void Token::refreshToken() const {
+ std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH);
+ std::ifstream token_file(file_path);
+ std::string line;
+ std::getline(token_file, line);
+ if (line.empty()) {
+ writeNewTokenToFile();
+ } else {
+ long expire_at = std::stoull(line.substr(line.find(" ") + 1));
+ if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch())
+ .count() >= expire_at) {
+ writeNewTokenToFile();
+ }
+ }
+}
+
+void Token::writeNewTokenToFile() const {
+ std::string aToken{getValidTokenWithApi()};
+ writeTokenToFile(aToken);
+}
+
+void Token::writeTokenToFile(const std::string &iToken) const {
+ std::ofstream token_file{mFilePath};
+ std::chrono::seconds expires_at = getCurrentTime() + std::chrono::hours(24);
+ token_file << iToken << " " << expires_at.count() << '\n';
+}
+
+std::string Token::getValidTokenWithApi() const {
+ CURL *curl = curl_easy_init();
+ std::string token_info;
+ if (curl) {
+ curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token");
+ 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, &token_info);
+ curl_easy_setopt(curl, CURLOPT_USERPWD,
+ "2b50f1123b424d5e9c21971a110fdd04:"
+ "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6");
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
+ CURLcode result_code;
+ result_code = curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
+ if (result_code != CURLE_OK) {
+ std::cerr << "ERROR: token request failed" << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+ }
+
+ return extractFirstMatchRegex(token_info, "\"access_token\":\"(\\w+)\"");
+}
+
+std::chrono::seconds Token::getCurrentTime() const {
+ return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch());
+}
+
+std::string Token::extractFirstMatchRegex(const std::string &iText, const std::string &iRegex) const {
+ std::regex regex(iRegex);
+ auto aMatchBegin = std::sregex_iterator(iText.begin(), iText.end(), regex);
+ auto aMatchEnd = std::sregex_iterator();
+ if (std::distance(aMatchBegin, aMatchEnd) > 0) {
+ std::smatch match = *aMatchBegin;
+ return match[1].str();
+ }
+ return "";
+}
+
+size_t Token::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;
+}
diff --git a/subprojects/battleNetApi/tests/tests.cpp b/subprojects/battleNetApi/tests/tests.cpp
index d94bbd6..1790f88 100644
--- a/subprojects/battleNetApi/tests/tests.cpp
+++ b/subprojects/battleNetApi/tests/tests.cpp
@@ -1,17 +1,19 @@
-#include "hearthstoneApi.hpp"
-#include <catch2/benchmark/catch_benchmark.hpp>
+#include "token.hpp"
#include <catch2/catch_test_macros.hpp>
+#include <iostream>
-#include <cstdint>
+class TokenTests {
+ public:
+ std::string getValidTokenWithApi() const {
+ const std::string aTokenFilePath{""};
+ const Token aToken(aTokenFilePath);
+ return aToken.getValidTokenWithApi();
+ }
+};
-uint64_t fibonacci(uint64_t number) { return number < 2 ? number : fibonacci(number - 1) + fibonacci(number - 2); }
-
-TEST_CASE("Benchmark Fibonacci", "[fibonacci]") {
- REQUIRE(fibonacci(5) == 5);
-
- REQUIRE(fibonacci(20) == 6'765);
- BENCHMARK("fibonacci 20") { return fibonacci(20); };
-
- REQUIRE(fibonacci(25) == 75'025);
- BENCHMARK("fibonacci 25") { return fibonacci(25); };
+TEST_CASE("API Token retrieval", "[getApiToken]") {
+ TokenTests aToken;
+ const std::string kToken{aToken.getValidTokenWithApi()};
+ std::cout << kToken << '\n';
+ REQUIRE(kToken == "");
}