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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#![doc(html_root_url = "https://docs.rs/dbl-rs/0.3.0")]
#![deny(rust_2018_idioms)]
use reqwest::header::AUTHORIZATION;
use reqwest::{Client as ReqwestClient, Response};
use reqwest::{Method, StatusCode};
use url::Url;
macro_rules! api {
($e:expr) => {
concat!("https://top.gg/api", $e)
};
($e:expr, $($rest:tt)*) => {
format!(api!($e), $($rest)*)
};
}
mod error;
pub mod types;
pub mod widget;
pub use error::Error;
use types::*;
#[derive(Clone)]
pub struct Client {
client: ReqwestClient,
token: String,
}
impl Client {
pub fn new(token: String) -> Result<Self, Error> {
let client = ReqwestClient::builder().build().map_err(error::from)?;
Ok(Client { client, token })
}
pub fn new_with_client(client: ReqwestClient, token: String) -> Self {
Client { client, token }
}
pub async fn get<T>(&self, bot: T) -> Result<Bot, Error>
where
T: Into<BotId>,
{
let url = api!("/bots/{}", bot.into());
get(self, url).await
}
pub async fn search(&self, filter: &Filter) -> Result<Listing, Error> {
let url = Url::parse_with_params(&api!("/bots"), &filter.0).map_err(Error::Url)?;
get(self, url.to_string()).await
}
pub async fn stats<T>(&self, bot: T) -> Result<Stats, Error>
where
T: Into<BotId>,
{
let url = api!("/bots/{}/stats", bot.into());
get(self, url).await
}
pub async fn update_stats<T>(&self, bot: T, stats: ShardStats) -> Result<(), Error>
where
T: Into<BotId>,
{
let url = api!("/bots/{}/stats", bot.into());
post(self, url, Some(stats)).await
}
pub async fn votes<T>(&self, bot: T) -> Result<Vec<User>, Error>
where
T: Into<BotId>,
{
let url = api!("/bots/{}/votes", bot.into());
get(self, url).await
}
pub async fn has_voted<T, U>(&self, bot: T, user: U) -> Result<bool, Error>
where
T: Into<BotId>,
U: Into<UserId>,
{
let bot = bot.into();
let user = user.into();
let url = api!("/bots/{}/check?userId={}", bot, user);
let v: UserVoted = get(self, url).await?;
Ok(v.voted > 0)
}
pub async fn user<T>(&self, user: T) -> Result<DetailedUser, Error>
where
T: Into<UserId>,
{
let url = api!("/users/{}", user.into());
get(self, url).await
}
}
async fn request<T>(
client: &Client,
method: Method,
url: String,
data: Option<T>,
) -> Result<Response, Error>
where
T: serde::Serialize + Sized,
{
let mut req = client
.client
.request(method, &url)
.header(AUTHORIZATION, &*client.token);
if let Some(data) = data {
req = req.json(&data);
}
let resp = match req.send().await {
Ok(resp) => resp,
Err(e) => return Err(error::from(e)),
};
match resp.status() {
StatusCode::TOO_MANY_REQUESTS => {
let rl = match resp.json::<Ratelimit>().await {
Ok(rl) => rl,
Err(e) => return Err(error::from(e)),
};
Err(error::ratelimit(rl.retry_after))
}
_ => resp.error_for_status().map_err(error::from),
}
}
async fn get<T>(client: &Client, url: String) -> Result<T, Error>
where
T: serde::de::DeserializeOwned + Sized,
{
let resp = request(client, Method::GET, url, None::<()>).await?;
match resp.json().await {
Ok(data) => Ok(data),
Err(e) => Err(error::from(e)),
}
}
async fn post<T>(client: &Client, url: String, data: Option<T>) -> Result<(), Error>
where
T: serde::Serialize + Sized,
{
request(client, Method::POST, url, data).await?;
Ok(())
}