summaryrefslogtreecommitdiff
path: root/battleNetApi/include/hearthstoneTokenBuilder.hpp
blob: 571554f89c3b98a7efc9986309fdadb2e0b836e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <chrono>
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
#include <token.hpp>
#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> _currentToken;
    bool _allowBattleNetApiCall{false};
    bool _allowUpdateSourceFile{false};
    bool _allowExpiredToken{false};
    std::vector<std::string> _credentialJsonPaths;
    [[nodiscard]]
    bool isUpdateSourceFileAllowed() const;
    [[nodiscard]]
    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]]
    Token getTokenWithApi(const std::string &iCredentialsJsonFilePath) const;
    [[nodiscard]]
    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);
    template <typename... CredentialJsonPaths>
    HearthstoneTokenBuilder(CredentialJsonPaths... iCredentialJsonPaths) {
        (_credentialJsonPaths.push_back(iCredentialJsonPaths), ...);
    }
    template <typename... CredentialJsonPaths>
    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?)
     * A new token is computed if the token lifetimes expired,
     * or if there is no token at all.
     */
    HearthstoneTokenBuilder &buildToken() override;
    [[nodiscard]]
    std::shared_ptr<Token> getToken() override;
};
} // namespace bnet