blob: c11acc7d75be1f4c5d709204df3f8ba9f9c3a202 (
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
|
#pragma once
#include "token.hpp"
#include <iostream>
#include <regex>
#include <string>
class HearthstoneApi {
public:
HearthstoneApi(Token &&iToken);
std::uint64_t numberOfCards(const std::string &iApiResponse) const;
private:
std::string authorizationHeader();
std::string makeApiRequest(const std::string &iUrl) const;
static size_t curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb, std::string *iS);
/**
* @brief
* @param[in] iJsonContent The std::string which contains Json (Battle.net API response for instance)
* @param[in] iKeyValueRegex The regex used to retrieve value from (key, value) pair
* An example: "\"cardCount\":(\\d+)"
*/
template <typename T> T valueInJson(const std::string &iJsonContent, const std::string &iKeyValueRegex) const {
const std::regex iRegex{iKeyValueRegex};
std::smatch aMatches;
if (std::regex_search(iJsonContent, aMatches, iRegex)) {
const std::string kValueString{aMatches[1].str()};
T aValue;
const auto kConversionStatus =
std::from_chars(kValueString.data(), kValueString.data() + kValueString.size(), aValue);
if (kConversionStatus.ec == std::errc{}) {
return aValue;
}
}
std::cerr << std::format("Unable to find value with regex: '{}'\n", iKeyValueRegex);
return 0;
}
Token mToken;
std::string mAuthorizationHeader; //<! Bearer authorization string for OAuth 2.0 header
friend class HearthstoneApiMock;
};
|