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
|
|
|
|
2023-04-08 14:51:36 +02:00
|
|
|
// Note: There seem to be some overlapping request limiters. We need to make sure that sending a
|
|
|
|
// request checks for all the request limiters that apply, and blocks if any of the limiters are 0
|
|
|
|
|
2023-04-07 23:22:44 +02:00
|
|
|
pub struct Limit {
|
2023-04-07 21:01:48 +02:00
|
|
|
limit: i64,
|
|
|
|
remaining: i64,
|
|
|
|
reset: i64,
|
2023-04-07 23:22:44 +02:00
|
|
|
bucket: String,
|
2023-04-07 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 21:51:50 +02:00
|
|
|
pub struct LimitedRequester {
|
2023-04-07 21:01:48 +02:00
|
|
|
http: Client,
|
2023-04-08 14:37:20 +02:00
|
|
|
limit: Vec<Limit>,
|
2023-04-07 23:22:44 +02:00
|
|
|
requests: VecDeque<Request>,
|
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-08 14:51:36 +02:00
|
|
|
pub async fn new(api_url: String) -> Self {
|
2023-04-07 21:51:50 +02:00
|
|
|
LimitedRequester {
|
2023-04-08 14:51:36 +02:00
|
|
|
limit: LimitedRequester::check_limits(api_url).await,
|
2023-04-07 21:01:48 +02:00
|
|
|
http: Client::new(),
|
2023-04-07 21:51:50 +02:00
|
|
|
requests: VecDeque::new(),
|
2023-04-07 21:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-07 23:22:44 +02:00
|
|
|
|
2023-04-08 14:51:36 +02:00
|
|
|
pub async fn check_limits(url: String) -> Vec<Limit> {
|
2023-04-07 23:22:44 +02:00
|
|
|
let client = Client::new();
|
2023-04-08 14:37:20 +02:00
|
|
|
let url_parsed = crate::URLBundle::parse_url(url) + "/api/policies/instance/limits";
|
2023-04-08 23:56:53 +02:00
|
|
|
let result = client
|
|
|
|
.get(url_parsed)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|e| panic!("An error occured while performing the request: {}", e))
|
|
|
|
.text()
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
panic!(
|
|
|
|
"An error occured while parsing the request body string: {}",
|
|
|
|
e
|
|
|
|
)
|
|
|
|
});
|
2023-04-10 14:18:48 +02:00
|
|
|
println!("{}", result);
|
|
|
|
|
2023-04-08 14:51:36 +02:00
|
|
|
/*
|
|
|
|
2. extract rate and absolute rate limits from response result
|
|
|
|
3. put each different rate limit as a new object in the limit vector
|
|
|
|
4. yeah
|
|
|
|
*/
|
2023-04-08 14:37:20 +02:00
|
|
|
let mut limit_vector = Vec::new();
|
|
|
|
limit_vector.push(Limit {
|
2023-04-07 23:22:44 +02:00
|
|
|
limit: -1,
|
|
|
|
remaining: -1,
|
|
|
|
reset: -1,
|
|
|
|
bucket: String::new(),
|
2023-04-08 14:37:20 +02:00
|
|
|
}); // TODO: Implement
|
|
|
|
limit_vector
|
2023-04-07 23:22:44 +02:00
|
|
|
}
|
2023-04-07 21:01:48 +02:00
|
|
|
}
|
2023-04-08 14:37:20 +02:00
|
|
|
|
2023-04-10 14:18:48 +02:00
|
|
|
#[cfg(test)]
|
2023-04-08 14:37:20 +02:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2023-04-10 14:18:48 +02:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_parse_url() {
|
|
|
|
let test_vec = LimitedRequester::check_limits(String::from("http://localhost:3001/")).await;
|
|
|
|
let first_from_vec = test_vec.get(0).unwrap();
|
|
|
|
println!("{}", first_from_vec.bucket);
|
2023-04-08 14:37:20 +02:00
|
|
|
}
|
2023-04-10 14:18:48 +02:00
|
|
|
}
|