summaryrefslogtreecommitdiff
path: root/battleNetApi/include/hearthstoneTokenBuilder.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'battleNetApi/include/hearthstoneTokenBuilder.hpp')
-rw-r--r--battleNetApi/include/hearthstoneTokenBuilder.hpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/battleNetApi/include/hearthstoneTokenBuilder.hpp b/battleNetApi/include/hearthstoneTokenBuilder.hpp
new file mode 100644
index 0000000..488aff4
--- /dev/null
+++ b/battleNetApi/include/hearthstoneTokenBuilder.hpp
@@ -0,0 +1,43 @@
+#include <chrono>
+#include <cstdint>
+#include <nlohmann/json.hpp>
+#include <string>
+#include <token.hpp>
+#include <tokenBuilder.hpp>
+#include <vector>
+
+using nlohmann::json;
+
+static constexpr std::uint64_t kTOKEN_CONTENT_MIN_LENGTH{10};
+
+class HearthstoneTokenBuilder : TokenBuilder {
+ private:
+ std::shared_ptr<Token> mCurrentToken;
+ bool mUpdateSourceFile{false};
+ std::vector<std::string> mJsonPaths;
+ bool isUpdateSourceFileAllowed() const;
+ const bool hasExpired(json iToken) const;
+ static size_t curlCallbackStringOutput(void *iPtr, size_t iSize,
+ size_t iNmemb,
+ std::string *iUserData);
+ std::chrono::seconds getCurrentTime() const;
+ std::pair<std::string, std::uint64_t> getTokenWithApi() const;
+ std::string getTokenFromLocalFile() const;
+ void writeTokenToLocalFile(
+ const std::pair<std::string, std::uint64_t> &iToken) const;
+
+ public:
+ HearthstoneTokenBuilder(const std::span<std::string> &iJsonPaths);
+ HearthstoneTokenBuilder(const std::string &iJsonPath);
+ HearthstoneTokenBuilder &addJsonPath(const std::string &iJsonPath);
+ HearthstoneTokenBuilder &addJsonPaths(const std::string &iJsonPath);
+ HearthstoneTokenBuilder &allowUpdateSourceFile();
+ /**
+ * @return Create an access token to perform API requests.
+ * First, the cache files are checked.
+ * A new token is computed if the token lifetimes expired,
+ * or if there is no token at all.
+ */
+ HearthstoneTokenBuilder &buildToken() override;
+ std::shared_ptr<Token> getToken() override;
+};