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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#![warn(clippy::all, clippy::pedantic, clippy::cargo)]
pub mod response;
pub mod types;
pub mod util;
use response::{
boosters_response::BoostersResponse,
counts_response::CountsResponse,
friends_response::FriendsResponse,
guild_response::GuildResponse,
key_response::KeyResponse,
leaderboards_response::LeaderboardsResponse,
player_response::PlayerResponse,
punishment_stats_response::PunishmentStatsResponse,
recent_games_response::RecentGamesResponse,
skyblock::{
skyblock_auctions_response::SkyblockAuctionsResponse,
skyblock_bazaar_response::SkyblockBazaarResponse,
skyblock_bingo_response::SkyblockBingoResponse,
skyblock_ended_auctions_response::SkyblockEndedAuctionsResponse,
skyblock_fire_sales_response::SkyblockFireSalesResponse,
skyblock_news_response::SkyblockNewsResponse,
skyblock_profile_response::SkyblockProfileResponse,
skyblock_profiles_response::SkyblockProfilesResponse,
},
status_response::StatusResponse,
};
use serde_json::{json, Value};
use std::{cmp::max, time::Duration};
use surf::Client;
use util::{
error::Error,
minecraft::{self, ApiType, Response},
};
struct Key {
pub key: String,
remaining_limit: i64,
time_till_reset: i64,
time: i64,
}
impl Key {
pub fn new(key: &str) -> Key {
Key {
key: key.to_string(),
remaining_limit: 0,
time_till_reset: 0,
time: 0,
}
}
pub fn update_remaining_limit(&mut self, remaining_limit: i64) {
self.remaining_limit = remaining_limit;
self.time = chrono::Utc::now().timestamp_millis();
}
pub fn update_time_till_reset(&mut self, time_till_reset: i64) {
self.time_till_reset = time_till_reset;
self.time = chrono::Utc::now().timestamp_millis();
}
pub fn is_rate_limited(&self) -> bool {
self.remaining_limit <= 1
&& self.time_till_reset > 0
&& self.time + self.time_till_reset * 1000 > chrono::Utc::now().timestamp_millis()
}
pub fn get_time_till_reset(&self) -> i64 {
max(
0,
((self.time + self.time_till_reset * 1000) - chrono::Utc::now().timestamp_millis())
/ 1000,
)
}
}
#[derive(PartialEq)]
pub enum RateLimitStrategy {
Delay,
Error,
}
pub struct Config {
pub client: Client,
pub minecraft_api_type: ApiType,
pub rate_limit_strategy: RateLimitStrategy,
}
#[derive(Default)]
pub struct ConfigBuilder {
client: Option<Client>,
minecraft_api_type: Option<ApiType>,
rate_limit_strategy: Option<RateLimitStrategy>,
}
impl ConfigBuilder {
pub fn client(mut self, client: Client) -> ConfigBuilder {
self.client = Some(client);
self
}
pub fn minecraft_api_type(mut self, minecraft_api_type: ApiType) -> ConfigBuilder {
self.minecraft_api_type = Some(minecraft_api_type);
self
}
pub fn rate_limit_strategy(mut self, rate_limit_strategy: RateLimitStrategy) -> ConfigBuilder {
self.rate_limit_strategy = Some(rate_limit_strategy);
self
}
}
impl From<ConfigBuilder> for Config {
fn from(v: ConfigBuilder) -> Self {
Config {
client: v.client.unwrap_or_else(|| {
surf::Config::new()
.set_timeout(Some(Duration::from_secs(15)))
.try_into()
.unwrap()
}),
minecraft_api_type: v.minecraft_api_type.unwrap_or(ApiType::Ashcon),
rate_limit_strategy: v.rate_limit_strategy.unwrap_or(RateLimitStrategy::Delay),
}
}
}
pub struct RsPixel {
pub config: Config,
key: Key,
}
impl RsPixel {
pub async fn new(key: &str) -> Result<RsPixel, Error> {
let mut rs_pixel = RsPixel {
config: ConfigBuilder::default().into(),
key: Key::new(key),
};
rs_pixel.simple_get("key").await.map(|_| rs_pixel)
}
pub async fn from_config(key: &str, config: Config) -> Result<RsPixel, Error> {
let mut rs_pixel = RsPixel {
config,
key: Key::new(key),
};
rs_pixel.simple_get("key").await.map(|_| rs_pixel)
}
pub async fn username_to_uuid(&self, username: &str) -> Result<Response, Error> {
minecraft::username_to_uuid(self, username).await
}
pub async fn uuid_to_username(&self, uuid: &str) -> Result<Response, Error> {
minecraft::uuid_to_username(self, uuid).await
}
pub async fn simple_get(&mut self, path: &str) -> Result<Value, Error> {
self.get(path, json!({})).await
}
pub async fn get(&mut self, path: &str, params: Value) -> Result<Value, Error> {
if self.key.is_rate_limited() {
let time_till_reset = self.key.get_time_till_reset();
match self.config.rate_limit_strategy {
RateLimitStrategy::Delay => {
println!("Sleeping for {} seconds", time_till_reset);
std::thread::sleep(Duration::from_secs(time_till_reset.try_into().unwrap()));
}
RateLimitStrategy::Error => {
return Err(Error::RateLimit(self.key.time_till_reset));
}
}
}
match self
.config
.client
.get(format!("https://api.hypixel.net/{}", path))
.query(¶ms)
.unwrap()
.header("API-Key", self.key.key.to_string())
.send()
.await
{
Ok(mut res_unwrap) => {
if let Some(remaining_limit) = res_unwrap
.header("RateLimit-Remaining")
.and_then(|header| header.as_str().parse::<i64>().ok())
{
self.key.update_remaining_limit(remaining_limit);
}
if let Some(time_till_reset) = res_unwrap
.header("RateLimit-Reset")
.and_then(|header| header.as_str().parse::<i64>().ok())
{
self.key.update_time_till_reset(time_till_reset);
}
let json = res_unwrap.body_json::<Value>().await.map_err(Error::from);
if res_unwrap.status() == 200 {
return json;
}
Err(Error::from((
res_unwrap.status(),
json.ok()
.as_ref()
.and_then(|json_unwrap| json_unwrap.get("cause"))
.and_then(serde_json::Value::as_str)
.unwrap_or("Unknown fail cause")
.to_string(),
)))
}
Err(err) => Err(Error::from(err)),
}
}
pub async fn get_key(&mut self) -> Result<KeyResponse, Error> {
self.simple_get("key")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_boosters(&mut self) -> Result<BoostersResponse, Error> {
self.simple_get("boosters")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_leaderboards(&mut self) -> Result<LeaderboardsResponse, Error> {
self.simple_get("leaderboards")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_punishment_stats(&mut self) -> Result<PunishmentStatsResponse, Error> {
self.simple_get("punishmentstats")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_player_by_uuid(&mut self, uuid: &str) -> Result<PlayerResponse, Error> {
self.get("player", json!({ "uuid": uuid }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_player_by_username(
&mut self,
username: &str,
) -> Result<PlayerResponse, Error> {
let minecraft_response = self.username_to_uuid(username).await?;
self.get_player_by_uuid(minecraft_response.uuid.as_str())
.await
}
pub async fn get_friends(&mut self, uuid: &str) -> Result<FriendsResponse, Error> {
self.get("friends", json!({ "uuid": uuid }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
async fn get_guild(&mut self, key: &str, value: &str) -> Result<GuildResponse, Error> {
self.get("guild", json!({ key: value }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_guild_by_player(&mut self, uuid: &str) -> Result<GuildResponse, Error> {
self.get_guild("player", uuid).await
}
pub async fn get_guild_by_name(&mut self, name: &str) -> Result<GuildResponse, Error> {
self.get_guild("name", name).await
}
pub async fn get_guild_by_id(&mut self, id: &str) -> Result<GuildResponse, Error> {
self.get_guild("id", id).await
}
pub async fn get_counts(&mut self) -> Result<CountsResponse, Error> {
self.simple_get("counts")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_status(&mut self, uuid: &str) -> Result<StatusResponse, Error> {
self.get("status", json!({ "uuid": uuid }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_recent_games(&mut self, uuid: &str) -> Result<RecentGamesResponse, Error> {
self.get("recentGames", json!({ "uuid": uuid }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_profiles_by_uuid(
&mut self,
uuid: &str,
) -> Result<SkyblockProfilesResponse, Error> {
self.get("skyblock/profiles", json!({ "uuid": uuid }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
.map(|mut response: SkyblockProfilesResponse| {
response.set_uuid(uuid);
response
})
}
pub async fn get_skyblock_profiles_by_name(
&mut self,
username: &str,
) -> Result<SkyblockProfilesResponse, Error> {
let minecraft_response = self.username_to_uuid(username).await?;
self.get_skyblock_profiles_by_uuid(minecraft_response.uuid.as_str())
.await
}
pub async fn get_skyblock_profile(
&mut self,
profile: &str,
) -> Result<SkyblockProfileResponse, Error> {
self.get("skyblock/profile", json!({ "profile": profile }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_bingo(&mut self, uuid: &str) -> Result<SkyblockBingoResponse, Error> {
self.get("skyblock/bingo", json!({ "uuid": uuid }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_news(&mut self) -> Result<SkyblockNewsResponse, Error> {
self.simple_get("skyblock/news")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_auctions(
&mut self,
page: i64,
) -> Result<SkyblockAuctionsResponse, Error> {
self.get("skyblock/auctions", json!({ "page": page }))
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_ended_auctions(
&mut self,
) -> Result<SkyblockEndedAuctionsResponse, Error> {
self.simple_get("skyblock/auctions_ended")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_bazaar(&mut self) -> Result<SkyblockBazaarResponse, Error> {
self.simple_get("skyblock/bazaar")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_skyblock_fire_sales(&mut self) -> Result<SkyblockFireSalesResponse, Error> {
self.simple_get("skyblock/firesales")
.await
.and_then(|response| serde_json::from_value(response).map_err(Error::from))
}
pub async fn get_resource(&mut self, resource: &str) -> Result<Value, Error> {
self.simple_get(format!("resources/{}", resource).as_str())
.await
}
}