diff --git a/youmubot-osu/src/discord/oppai_cache.rs b/youmubot-osu/src/discord/oppai_cache.rs index 8277476..a446330 100644 --- a/youmubot-osu/src/discord/oppai_cache.rs +++ b/youmubot-osu/src/discord/oppai_cache.rs @@ -56,15 +56,14 @@ impl BeatmapContent { /// A central cache for the beatmaps. pub struct BeatmapCache { - client: ratelimit::Ratelimit, + client: ratelimit::Ratelimit, cache: dashmap::DashMap>, } impl BeatmapCache { /// Create a new cache. - pub fn new() -> Self { - let client = - ratelimit::Ratelimit::new(surf::Client::new(), 5, std::time::Duration::from_secs(1)); + pub fn new(client: reqwest::Client) -> Self { + let client = ratelimit::Ratelimit::new(client, 5, std::time::Duration::from_secs(1)); BeatmapCache { client, cache: dashmap::DashMap::new(), @@ -78,14 +77,12 @@ impl BeatmapCache { .await? .get(&format!("https://osu.ppy.sh/osu/{}", id)) .send() - .await - .map_err(|e| Error::msg(format!("{}", e)))? - .body_bytes() - .await - .map_err(|e| Error::msg(format!("{}", e)))?; + .await? + .bytes() + .await?; Ok(BeatmapContent { id, - content: CString::new(content)?, + content: CString::new(content.into_iter().collect::>())?, }) } diff --git a/youmubot-osu/src/lib.rs b/youmubot-osu/src/lib.rs index 653b503..b850884 100644 --- a/youmubot-osu/src/lib.rs +++ b/youmubot-osu/src/lib.rs @@ -8,12 +8,12 @@ mod test; use models::*; use request::builders::*; use request::*; -use reqwest::{Client as HTTPClient, RequestBuilder, Response}; -use std::{convert::TryInto, sync::Arc}; -use youmubot_prelude::{self::*, ratelimit::Ratelimit}; +use reqwest::Client as HTTPClient; +use std::convert::TryInto; +use youmubot_prelude::{ratelimit::Ratelimit, *}; /// The number of requests per minute to the osu! server. -const REQUESTS_PER_MINUTE: u64 = 200; +const REQUESTS_PER_MINUTE: usize = 200; /// Client is the client that will perform calls to the osu! api server. pub struct Client { @@ -39,15 +39,16 @@ impl Client { REQUESTS_PER_MINUTE, std::time::Duration::from_secs(60), ); - Client { - key, - client: http_client, - } + Client { key, client } } - async fn build_request(&self, r: RequestBuilder) -> Result { - let v = r.query(&[("k", &*self.key)]).build()?; - Ok(self.client.borrow().await?.execute(v).await?) + pub(crate) async fn build_request(&self, url: &str) -> Result { + Ok(self + .client + .borrow() + .await? + .get(url) + .query(&[("k", &*self.key)])) } pub async fn beatmaps( @@ -57,11 +58,7 @@ impl Client { ) -> Result> { let mut r = BeatmapRequestBuilder::new(kind); f(&mut r); - let res: Vec = self - .build_request(r.build(&self.client)) - .await? - .json() - .await?; + let res: Vec = r.build(&self).await?.json().await?; Ok(vec_try_into(res)?) } @@ -72,11 +69,7 @@ impl Client { ) -> Result, Error> { let mut r = UserRequestBuilder::new(user); f(&mut r); - let res: Vec = self - .build_request(r.build(&self.client)) - .await? - .json() - .await?; + let res: Vec = r.build(&self).await?.json().await?; let res = vec_try_into(res)?; Ok(res.into_iter().next()) } @@ -88,11 +81,7 @@ impl Client { ) -> Result, Error> { let mut r = ScoreRequestBuilder::new(beatmap_id); f(&mut r); - let res: Vec = self - .build_request(r.build(&self.client)) - .await? - .json() - .await?; + let res: Vec = r.build(&self).await?.json().await?; let mut res: Vec = vec_try_into(res)?; // with a scores request you need to fill the beatmap ids yourself @@ -126,11 +115,7 @@ impl Client { ) -> Result, Error> { let mut r = UserScoreRequestBuilder::new(u, user); f(&mut r); - let res: Vec = self - .build_request(r.build(&self.client)) - .await? - .json() - .await?; + let res: Vec = r.build(&self).await?.json().await?; let res = vec_try_into(res)?; Ok(res) } diff --git a/youmubot-osu/src/request.rs b/youmubot-osu/src/request.rs index ad2155a..5f1004c 100644 --- a/youmubot-osu/src/request.rs +++ b/youmubot-osu/src/request.rs @@ -1,6 +1,7 @@ use crate::models::{Mode, Mods}; +use crate::Client; use chrono::{DateTime, Utc}; -use reqwest::{Client, RequestBuilder}; +use youmubot_prelude::*; trait ToQuery { fn to_query(&self) -> Vec<(&'static str, String)>; @@ -84,6 +85,8 @@ impl ToQuery for BeatmapRequestKind { } pub mod builders { + use reqwest::Response; + use super::*; /// A builder for a Beatmap request. pub struct BeatmapRequestBuilder { @@ -110,12 +113,15 @@ pub mod builders { self } - pub(crate) fn build(self, client: &Client) -> RequestBuilder { - client - .get("https://osu.ppy.sh/api/get_beatmaps") + pub(crate) async fn build(self, client: &Client) -> Result { + Ok(client + .build_request("https://osu.ppy.sh/api/get_beatmaps") + .await? .query(&self.kind.to_query()) .query(&self.since.map(|v| ("since", v)).to_query()) .query(&self.mode.to_query()) + .send() + .await?) } } @@ -144,9 +150,10 @@ pub mod builders { self } - pub(crate) fn build(&self, client: &Client) -> RequestBuilder { - client - .get("https://osu.ppy.sh/api/get_user") + pub(crate) async fn build(&self, client: &Client) -> Result { + Ok(client + .build_request("https://osu.ppy.sh/api/get_user") + .await? .query(&self.user.to_query()) .query(&self.mode.to_query()) .query( @@ -155,6 +162,8 @@ pub mod builders { .map(|v| ("event_days", v.to_string())) .to_query(), ) + .send() + .await?) } } @@ -197,14 +206,17 @@ pub mod builders { self } - pub(crate) fn build(&self, client: &Client) -> RequestBuilder { - client - .get("https://osu.ppy.sh/api/get_scores") + pub(crate) async fn build(&self, client: &Client) -> Result { + Ok(client + .build_request("https://osu.ppy.sh/api/get_scores") + .await? .query(&[("b", self.beatmap_id)]) .query(&self.user.to_query()) .query(&self.mode.to_query()) .query(&self.mods.to_query()) .query(&self.limit.map(|v| ("limit", v.to_string())).to_query()) + .send() + .await?) } } @@ -240,15 +252,18 @@ pub mod builders { self } - pub(crate) fn build(&self, client: &Client) -> RequestBuilder { - client - .get(match self.score_type { + pub(crate) async fn build(&self, client: &Client) -> Result { + Ok(client + .build_request(match self.score_type { UserScoreType::Best => "https://osu.ppy.sh/api/get_user_best", UserScoreType::Recent => "https://osu.ppy.sh/api/get_user_recent", }) + .await? .query(&self.user.to_query()) .query(&self.mode.to_query()) .query(&self.limit.map(|v| ("limit", v.to_string())).to_query()) + .send() + .await?) } } }