diff options
| author | Aotrix <[email protected]> | 2026-07-29 18:31:43 +0200 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2026-07-29 18:31:43 +0200 |
| commit | 1fbfd13f5d7220122b2a127627cc9105013b4893 (patch) | |
| tree | 0ca51f463996ea96eddc5b8939c07d3ef207a311 /battleNetApi/include | |
| parent | 2c286a812a3bdaa2531e8f87f5503988bb331a78 (diff) | |
Add tests for HearthstoneTokenBuilder
Diffstat (limited to 'battleNetApi/include')
| -rw-r--r-- | battleNetApi/include/hearthstoneTokenBuilder.hpp | 20 | ||||
| -rw-r--r-- | battleNetApi/include/token.hpp | 16 | ||||
| -rw-r--r-- | battleNetApi/include/tokenBuilder.hpp | 2 |
3 files changed, 25 insertions, 13 deletions
diff --git a/battleNetApi/include/hearthstoneTokenBuilder.hpp b/battleNetApi/include/hearthstoneTokenBuilder.hpp index e946fe8..571554f 100644 --- a/battleNetApi/include/hearthstoneTokenBuilder.hpp +++ b/battleNetApi/include/hearthstoneTokenBuilder.hpp @@ -6,29 +6,32 @@ #include <tokenBuilder.hpp> #include <vector> +namespace bnet { 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::shared_ptr<Token> _currentToken; + bool _allowBattleNetApiCall{false}; + bool _allowUpdateSourceFile{false}; + bool _allowExpiredToken{false}; std::vector<std::string> _credentialJsonPaths; [[nodiscard]] bool isUpdateSourceFileAllowed() const; [[nodiscard]] - const bool hasExpired(json iTokenJson) const; + bool isTokenValid(const json &iTokenJson) const; static size_t curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData); [[nodiscard]] std::chrono::seconds getCurrentTime() const; [[nodiscard]] - std::pair<std::string, std::uint64_t> - getTokenWithApi(const std::string &iAgentJsonFilePath) const; + Token getTokenWithApi(const std::string &iCredentialsJsonFilePath) const; [[nodiscard]] - std::string getTokenFromLocalFile() const; - void writeTokenToLocalFile(const std::pair<std::string, std::uint64_t> &iToken) const; + Token getTokenWithApi(const json &iCredentials) const; + void writeTokenToLocalFile(std::shared_ptr<Token> &iToken, + const std::string &iTokenFileOutputPath) const; public: HearthstoneTokenBuilder(const std::span<std::string> &iJsonPaths); @@ -40,7 +43,9 @@ class HearthstoneTokenBuilder : TokenBuilder { HearthstoneTokenBuilder &addCredentialJsonFiles(CredentialJsonPaths... iCredentialJsonPaths) { (_credentialJsonPaths.push_back(iCredentialJsonPaths), ...); } + HearthstoneTokenBuilder &allowBattleNetApiCall(); HearthstoneTokenBuilder &allowUpdateSourceFile(); + HearthstoneTokenBuilder &allowExpiredToken(); /** * @return Create an access token to perform API requests. * First, the cache files are checked. (Which ones?) @@ -51,3 +56,4 @@ class HearthstoneTokenBuilder : TokenBuilder { [[nodiscard]] std::shared_ptr<Token> getToken() override; }; +} // namespace bnet diff --git a/battleNetApi/include/token.hpp b/battleNetApi/include/token.hpp index 4a66c53..f943b81 100644 --- a/battleNetApi/include/token.hpp +++ b/battleNetApi/include/token.hpp @@ -3,16 +3,21 @@ #include <chrono> #include <string> +namespace bnet { +using TokenText = std::string; + class Token { public: - explicit Token(const std::string &iTokenText, const std::chrono::seconds &iLifetime); - std::string text() const; - void setText(const std::string &iTokenText); - void setLifetime(const std::chrono::seconds &iLifetime); + explicit Token(); + std::optional<std::string> text() const; + std::optional<std::chrono::seconds> expirationTime() const; + Token &setText(const TokenText &iTokenText); + Token &setLifetime(const std::chrono::seconds &iLifetime); + Token &setExpirationTime(const std::chrono::seconds &iExpirationTime); bool isExpired(); private: - std::string _tokenText; + std::optional<TokenText> _tokenText; std::chrono::seconds _expirationTime; }; @@ -22,3 +27,4 @@ auto currentTime = [] { std::chrono::steady_clock::now().time_since_epoch()); }; } // namespace +} // namespace bnet diff --git a/battleNetApi/include/tokenBuilder.hpp b/battleNetApi/include/tokenBuilder.hpp index 2893728..5af4791 100644 --- a/battleNetApi/include/tokenBuilder.hpp +++ b/battleNetApi/include/tokenBuilder.hpp @@ -8,5 +8,5 @@ class TokenBuilder { TokenBuilder() = default; virtual ~TokenBuilder() = default; virtual TokenBuilder &buildToken() = 0; - virtual std::shared_ptr<Token> getToken() = 0; + virtual std::shared_ptr<bnet::Token> getToken() = 0; }; |
