summaryrefslogtreecommitdiff
path: root/battleNetApi/src/apiUrl.cpp
blob: 5435fd9af5db2ebe7bddfb0e5e21fcdc9cebe1de (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
#include "apiUrl.hpp"
#include <format>

ApiUrl::ApiUrl(const std::string &iContentType, const std::string &iRegion)
    : mContentType{iContentType}, mRegion{iRegion} {}
void ApiUrl::setRegion(const std::string &iRegion) {
    if (not iRegion.empty()) {
        mRegion = iRegion; //!< The region of the data to receive, e.g. "eu"
    }
}
void ApiUrl::setLocale(const std::string &iLocale) {
    if (not iLocale.empty()) {
        //!< The locale to reflect in localized data, e.g. "fr_FR" or "ko_KR"
        //!< If you do not supply a value, all translations are returned.
        const std::string kKey{"locale"};
        setValue(kKey, iLocale);
    }
}
void ApiUrl::setGameMode(const std::string &iGameMode) {
    if (not iGameMode.empty()) {
        //!< A recognized game mode, e.g. "constructed"
        //!< @see https://community.developer.battle.net/documentation/hearthstone/guides/game-modes
        const std::string kKey{"gameMode"};
        setValue(kKey, iGameMode);
    }
}
void ApiUrl::setPage(const std::string &iPage) {
    if (not iPage.empty()) {
        //!< The page to retrieve, e.g. "9"
        const std::string kKey{"page"};
        setValue(kKey, iPage);
    }
}
void ApiUrl::setPageSize(const std::string &iPageSize) {
    if (not iPageSize.empty()) {
        //!< The number of results to choose per page, e.g. "40"
        const std::string kKey{"pageSize"};
        setValue(kKey, iPageSize);
    }
}

void ApiUrl::setValue(const std::string &iKey, const std::string &iValue) { aParameters[iKey] = iValue; }

std::string ApiUrl::operator()() const {
    std::string aFormattedParameters;
    for (const auto &[key, value] : aParameters) {
        aFormattedParameters += std::format("&{}={}", key, value);
    }
    if (not aFormattedParameters.empty()) {
        aFormattedParameters[0] = '?';
    }
    aFormattedParameters =
        std::format("https://{}.api.blizzard.com/hearthstone/{}/{}", mRegion, mContentType, aFormattedParameters);
    return aFormattedParameters;
}