osu: no longer depends on tower

This commit is contained in:
Natsu Kagami 2020-09-13 22:24:46 -04:00
parent dca732e52e
commit 71cbe5c63e
No known key found for this signature in database
GPG key ID: F17543D4B9424B94
3 changed files with 795 additions and 977 deletions

1736
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -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"

View file

@ -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(