mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-20 01:08:55 +00:00
osu: no longer depends on tower
This commit is contained in:
parent
dca732e52e
commit
71cbe5c63e
3 changed files with 795 additions and 977 deletions
1736
Cargo.lock
generated
1736
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -10,7 +10,6 @@ edition = "2018"
|
||||||
serenity = "0.9.0-rc.0"
|
serenity = "0.9.0-rc.0"
|
||||||
chrono = "0.4.10"
|
chrono = "0.4.10"
|
||||||
reqwest = "0.10.1"
|
reqwest = "0.10.1"
|
||||||
tower = "0.3"
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
bitflags = "1"
|
bitflags = "1"
|
||||||
lazy_static = "1"
|
lazy_static = "1"
|
||||||
|
|
|
@ -10,28 +10,14 @@ use request::builders::*;
|
||||||
use request::*;
|
use request::*;
|
||||||
use reqwest::{Client as HTTPClient, RequestBuilder, Response};
|
use reqwest::{Client as HTTPClient, RequestBuilder, Response};
|
||||||
use std::{convert::TryInto, sync::Arc};
|
use std::{convert::TryInto, sync::Arc};
|
||||||
use tower;
|
use youmubot_prelude::{self::*, ratelimit::Ratelimit};
|
||||||
use youmubot_prelude::*;
|
|
||||||
|
|
||||||
/// The number of requests per minute to the osu! server.
|
/// The number of requests per minute to the osu! server.
|
||||||
const REQUESTS_PER_MINUTE: u64 = 200;
|
const REQUESTS_PER_MINUTE: u64 = 200;
|
||||||
|
|
||||||
type BoxedResp =
|
|
||||||
std::pin::Pin<Box<dyn future::Future<Output = Result<Response, reqwest::Error>> + Send>>;
|
|
||||||
/// Client is the client that will perform calls to the osu! api server.
|
/// Client is the client that will perform calls to the osu! api server.
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
http_client: RwLock<
|
client: Ratelimit<HTTPClient>,
|
||||||
Box<
|
|
||||||
dyn tower::Service<
|
|
||||||
reqwest::Request,
|
|
||||||
Response = Response,
|
|
||||||
Error = reqwest::Error,
|
|
||||||
Future = BoxedResp,
|
|
||||||
> + Send
|
|
||||||
+ Sync,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
client: Arc<HTTPClient>,
|
|
||||||
key: String,
|
key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,25 +34,20 @@ fn vec_try_into<U, T: std::convert::TryFrom<U>>(v: Vec<U>) -> Result<Vec<T>, T::
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Create a new client from the given API key.
|
/// Create a new client from the given API key.
|
||||||
pub fn new(key: String) -> Client {
|
pub fn new(key: String) -> Client {
|
||||||
let http_client = Arc::new(HTTPClient::new());
|
let client = Ratelimit::new(
|
||||||
let _http = http_client.clone();
|
HTTPClient::new(),
|
||||||
let srv = tower::ServiceBuilder::new()
|
REQUESTS_PER_MINUTE,
|
||||||
.rate_limit(REQUESTS_PER_MINUTE, std::time::Duration::from_secs(60))
|
std::time::Duration::from_secs(60),
|
||||||
.service(tower::service_fn(move |req| -> BoxedResp {
|
);
|
||||||
Box::pin(_http.execute(req))
|
|
||||||
}));
|
|
||||||
Client {
|
Client {
|
||||||
key,
|
key,
|
||||||
http_client: RwLock::new(Box::new(srv)),
|
|
||||||
client: http_client,
|
client: http_client,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn build_request(&self, r: RequestBuilder) -> Result<Response> {
|
async fn build_request(&self, r: RequestBuilder) -> Result<Response> {
|
||||||
let v = r.query(&[("k", &*self.key)]).build()?;
|
let v = r.query(&[("k", &*self.key)]).build()?;
|
||||||
let mut client = self.http_client.write().await;
|
Ok(self.client.borrow().await?.execute(v).await?)
|
||||||
future::poll_fn(|ctx| client.poll_ready(ctx)).await?;
|
|
||||||
Ok(client.call(v).await?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn beatmaps(
|
pub async fn beatmaps(
|
||||||
|
|
Loading…
Add table
Reference in a new issue