blob: c5031f0dccefb339ddedf899d214fb5c4e09a3bb (
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include "helix_client.hpp"
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
#include "cpr/api.h"
#include "cpr/bearer.h"
#include "cpr/cprtypes.h"
#include "cpr/response.h"
#include "schemas/user.hpp"
namespace bot::api::twitch {
HelixClient::HelixClient(const std::string &token,
const std::string &client_id) {
this->token = token;
this->client_id = client_id;
}
std::vector<schemas::User> HelixClient::get_users(
const std::vector<std::string> &logins) const {
std::string s;
for (auto i = logins.begin(); i != logins.end(); i++) {
std::string start;
if (i == logins.begin()) {
start = "?";
} else {
start = "&";
}
s += start + "login=" + *i;
}
return this->get_users_by_query(s);
}
std::vector<schemas::User> HelixClient::get_users(
const std::vector<int> &ids) const {
std::string s;
for (auto i = ids.begin(); i != ids.end(); i++) {
std::string start;
if (i == ids.begin()) {
start = "?";
} else {
start = "&";
}
s += start + "id=" + std::to_string(*i);
}
return this->get_users_by_query(s);
}
std::vector<schemas::User> HelixClient::get_users_by_query(
const std::string &query) const {
cpr::Response response = cpr::Get(
cpr::Url{this->base_url + "/users" + query}, cpr::Bearer{this->token},
cpr::Header{{"Client-Id", this->client_id.c_str()}});
if (response.status_code != 200) {
return {};
}
std::vector<schemas::User> users;
nlohmann::json j = nlohmann::json::parse(response.text);
for (const auto &d : j["data"]) {
schemas::User u{std::stoi(d["id"].get<std::string>()), d["login"]};
users.push_back(u);
}
return users;
}
}
|