2023-04-07 21:01:48 +02:00
|
|
|
use reqwest::{Client, Request};
|
2023-04-07 21:51:50 +02:00
|
|
|
use std::collections::VecDeque;
|
2023-04-07 21:01:48 +02:00
|
|
|
|
|
|
|
struct Limit {
|
|
|
|
limit: i64,
|
|
|
|
remaining: i64,
|
|
|
|
reset: i64,
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:51:50 +02:00
|
|
|
pub struct LimitedRequester {
|
2023-04-07 21:01:48 +02:00
|
|
|
http: Client,
|
|
|
|
limit: Limit,
|
2023-04-07 21:51:50 +02:00
|
|
|
requests: VecDeque<Request>, // wow, amazing
|
2023-04-07 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 21:51:50 +02:00
|
|
|
impl LimitedRequester {
|
|
|
|
/// Create a new `LimitedRequester`. `LimitedRequester`s use a `VecDeque` to store requests and
|
|
|
|
/// send them to the server using a `Client`. It keeps track of the remaining requests that can
|
|
|
|
/// be send within the `Limit` of an external API Ratelimiter, and looks at the returned request
|
|
|
|
/// headers to see if it can find Ratelimit info to update itself.
|
2023-04-07 21:01:48 +02:00
|
|
|
pub fn new() -> Self {
|
2023-04-07 21:51:50 +02:00
|
|
|
LimitedRequester {
|
2023-04-07 21:01:48 +02:00
|
|
|
limit: Limit {
|
|
|
|
limit: 1,
|
|
|
|
remaining: 1,
|
|
|
|
reset: 0,
|
|
|
|
},
|
|
|
|
http: Client::new(),
|
2023-04-07 21:51:50 +02:00
|
|
|
requests: VecDeque::new(),
|
2023-04-07 21:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|