diff options
| author | Aotrix <[email protected]> | 2026-04-28 03:49:41 +0200 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2026-04-28 03:49:41 +0200 |
| commit | a0632d299b85dc4ef7c0e03898f38e6deca0edc2 (patch) | |
| tree | cc83712ca644e8b5d1b9e1c0c49bdbdd8475d0a9 /battleNetApi/include | |
| parent | 5582e994d39b16a6d61f05b13e78c30f4bac5d84 (diff) | |
add HearthstoneTokenBuilder class
Diffstat (limited to 'battleNetApi/include')
| -rw-r--r-- | battleNetApi/include/hearthstoneTokenBuilder.hpp | 43 | ||||
| -rw-r--r-- | battleNetApi/include/token.hpp | 25 | ||||
| -rw-r--r-- | battleNetApi/include/tokenBuilder.hpp | 14 | ||||
| -rw-r--r-- | battleNetApi/include/tokenPool.hpp | 20 |
4 files changed, 53 insertions, 49 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; +}; diff --git a/battleNetApi/include/token.hpp b/battleNetApi/include/token.hpp index b913b2b..cc909a8 100644 --- a/battleNetApi/include/token.hpp +++ b/battleNetApi/include/token.hpp @@ -1,32 +1,13 @@ #pragma once -#include <chrono> #include <string> class Token { public: - explicit Token(const std::string &iTokenFilePath); + explicit Token(const std::string &iTokenText); std::string text() const; - const bool hasExpired() const; + void setText(const std::string &iTokenText); private: - const std::string mFilePath; //!< Path to the token file - const std::string mUsername; //!< Username for Battle.net API - const std::string mPassword; //!< Password for Battle.net API - std::string mToken; //!< Token value - - /** - * @return An access token that a user can use to perform API requests. - * First, the cache file is checked. - * A new token is computed if the current token lifetime expired, - * or if there is no token at all. - */ - std::string generateToken(); - void writeTokenToLocalFile(const std::pair<std::string, std::uint64_t> &iToken) const; - std::string getTokenFromLocalFile() const; - std::pair<std::string, std::uint64_t> getTokenWithApi() const; - std::chrono::seconds getCurrentTime() const; - static size_t curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData); - - static const std::uint16_t kTokenContentMinLength{10}; + std::string mTokenText; //!< Token value }; diff --git a/battleNetApi/include/tokenBuilder.hpp b/battleNetApi/include/tokenBuilder.hpp index 71286cd..2893728 100644 --- a/battleNetApi/include/tokenBuilder.hpp +++ b/battleNetApi/include/tokenBuilder.hpp @@ -1,12 +1,12 @@ +#pragma once + +#include <memory> #include <token.hpp> class TokenBuilder { public: - TokenBuilder(); - [[nodiscard]] - Token build() const; - TokenBuilder &allowSourceFileUpdate(); - - private: - bool mAllowSourceFileUpdate{false}; + TokenBuilder() = default; + virtual ~TokenBuilder() = default; + virtual TokenBuilder &buildToken() = 0; + virtual std::shared_ptr<Token> getToken() = 0; }; diff --git a/battleNetApi/include/tokenPool.hpp b/battleNetApi/include/tokenPool.hpp deleted file mode 100644 index 0b1e4cc..0000000 --- a/battleNetApi/include/tokenPool.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include <queue> -#include <token.hpp> - -static std::queue<Token> aTokenContainer; - -class TokenPool { - private: - TokenPool() = delete; - TokenPool(const TokenPool &) = delete; - TokenPool(TokenPool &&) = delete; - TokenPool &operator=(const TokenPool &) = delete; - TokenPool &operator=(TokenPool &&) = delete; - - public: - static void feedTokenSource(const std::span<std::string> &iJsonPaths); - static void feedTokenSource(const std::string &iJsonPath); - static std::optional<Token> availableToken(); -}; |
