osu: Fix compile errors

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

View file

@ -56,15 +56,14 @@ impl BeatmapContent {
/// A central cache for the beatmaps. /// A central cache for the beatmaps.
pub struct BeatmapCache { pub struct BeatmapCache {
client: ratelimit::Ratelimit<surf::Client>, client: ratelimit::Ratelimit<reqwest::Client>,
cache: dashmap::DashMap<u64, Arc<BeatmapContent>>, cache: dashmap::DashMap<u64, Arc<BeatmapContent>>,
} }
impl BeatmapCache { impl BeatmapCache {
/// Create a new cache. /// Create a new cache.
pub fn new() -> Self { pub fn new(client: reqwest::Client) -> Self {
let client = let client = ratelimit::Ratelimit::new(client, 5, std::time::Duration::from_secs(1));
ratelimit::Ratelimit::new(surf::Client::new(), 5, std::time::Duration::from_secs(1));
BeatmapCache { BeatmapCache {
client, client,
cache: dashmap::DashMap::new(), cache: dashmap::DashMap::new(),
@ -78,14 +77,12 @@ impl BeatmapCache {
.await? .await?
.get(&format!("https://osu.ppy.sh/osu/{}", id)) .get(&format!("https://osu.ppy.sh/osu/{}", id))
.send() .send()
.await .await?
.map_err(|e| Error::msg(format!("{}", e)))? .bytes()
.body_bytes() .await?;
.await
.map_err(|e| Error::msg(format!("{}", e)))?;
Ok(BeatmapContent { Ok(BeatmapContent {
id, id,
content: CString::new(content)?, content: CString::new(content.into_iter().collect::<Vec<_>>())?,
}) })
} }

View file

@ -8,12 +8,12 @@ mod test;
use models::*; use models::*;
use request::builders::*; use request::builders::*;
use request::*; use request::*;
use reqwest::{Client as HTTPClient, RequestBuilder, Response}; use reqwest::Client as HTTPClient;
use std::{convert::TryInto, sync::Arc}; use std::convert::TryInto;
use youmubot_prelude::{self::*, ratelimit::Ratelimit}; use youmubot_prelude::{ratelimit::Ratelimit, *};
/// 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: usize = 200;
/// 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 {
@ -39,15 +39,16 @@ impl Client {
REQUESTS_PER_MINUTE, REQUESTS_PER_MINUTE,
std::time::Duration::from_secs(60), std::time::Duration::from_secs(60),
); );
Client { Client { key, client }
key,
client: http_client,
}
} }
async fn build_request(&self, r: RequestBuilder) -> Result<Response> { pub(crate) async fn build_request(&self, url: &str) -> Result<reqwest::RequestBuilder> {
let v = r.query(&[("k", &*self.key)]).build()?; Ok(self
Ok(self.client.borrow().await?.execute(v).await?) .client
.borrow()
.await?
.get(url)
.query(&[("k", &*self.key)]))
} }
pub async fn beatmaps( pub async fn beatmaps(
@ -57,11 +58,7 @@ impl Client {
) -> Result<Vec<Beatmap>> { ) -> Result<Vec<Beatmap>> {
let mut r = BeatmapRequestBuilder::new(kind); let mut r = BeatmapRequestBuilder::new(kind);
f(&mut r); f(&mut r);
let res: Vec<raw::Beatmap> = self let res: Vec<raw::Beatmap> = r.build(&self).await?.json().await?;
.build_request(r.build(&self.client))
.await?
.json()
.await?;
Ok(vec_try_into(res)?) Ok(vec_try_into(res)?)
} }
@ -72,11 +69,7 @@ impl Client {
) -> Result<Option<User>, Error> { ) -> Result<Option<User>, Error> {
let mut r = UserRequestBuilder::new(user); let mut r = UserRequestBuilder::new(user);
f(&mut r); f(&mut r);
let res: Vec<raw::User> = self let res: Vec<raw::User> = r.build(&self).await?.json().await?;
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let res = vec_try_into(res)?; let res = vec_try_into(res)?;
Ok(res.into_iter().next()) Ok(res.into_iter().next())
} }
@ -88,11 +81,7 @@ impl Client {
) -> Result<Vec<Score>, Error> { ) -> Result<Vec<Score>, Error> {
let mut r = ScoreRequestBuilder::new(beatmap_id); let mut r = ScoreRequestBuilder::new(beatmap_id);
f(&mut r); f(&mut r);
let res: Vec<raw::Score> = self let res: Vec<raw::Score> = r.build(&self).await?.json().await?;
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let mut res: Vec<Score> = vec_try_into(res)?; let mut res: Vec<Score> = vec_try_into(res)?;
// with a scores request you need to fill the beatmap ids yourself // with a scores request you need to fill the beatmap ids yourself
@ -126,11 +115,7 @@ impl Client {
) -> Result<Vec<Score>, Error> { ) -> Result<Vec<Score>, Error> {
let mut r = UserScoreRequestBuilder::new(u, user); let mut r = UserScoreRequestBuilder::new(u, user);
f(&mut r); f(&mut r);
let res: Vec<raw::Score> = self let res: Vec<raw::Score> = r.build(&self).await?.json().await?;
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let res = vec_try_into(res)?; let res = vec_try_into(res)?;
Ok(res) Ok(res)
} }

View file

@ -1,6 +1,7 @@
use crate::models::{Mode, Mods}; use crate::models::{Mode, Mods};
use crate::Client;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use reqwest::{Client, RequestBuilder}; use youmubot_prelude::*;
trait ToQuery { trait ToQuery {
fn to_query(&self) -> Vec<(&'static str, String)>; fn to_query(&self) -> Vec<(&'static str, String)>;
@ -84,6 +85,8 @@ impl ToQuery for BeatmapRequestKind {
} }
pub mod builders { pub mod builders {
use reqwest::Response;
use super::*; use super::*;
/// A builder for a Beatmap request. /// A builder for a Beatmap request.
pub struct BeatmapRequestBuilder { pub struct BeatmapRequestBuilder {
@ -110,12 +113,15 @@ pub mod builders {
self self
} }
pub(crate) fn build(self, client: &Client) -> RequestBuilder { pub(crate) async fn build(self, client: &Client) -> Result<Response> {
client Ok(client
.get("https://osu.ppy.sh/api/get_beatmaps") .build_request("https://osu.ppy.sh/api/get_beatmaps")
.await?
.query(&self.kind.to_query()) .query(&self.kind.to_query())
.query(&self.since.map(|v| ("since", v)).to_query()) .query(&self.since.map(|v| ("since", v)).to_query())
.query(&self.mode.to_query()) .query(&self.mode.to_query())
.send()
.await?)
} }
} }
@ -144,9 +150,10 @@ pub mod builders {
self self
} }
pub(crate) fn build(&self, client: &Client) -> RequestBuilder { pub(crate) async fn build(&self, client: &Client) -> Result<Response> {
client Ok(client
.get("https://osu.ppy.sh/api/get_user") .build_request("https://osu.ppy.sh/api/get_user")
.await?
.query(&self.user.to_query()) .query(&self.user.to_query())
.query(&self.mode.to_query()) .query(&self.mode.to_query())
.query( .query(
@ -155,6 +162,8 @@ pub mod builders {
.map(|v| ("event_days", v.to_string())) .map(|v| ("event_days", v.to_string()))
.to_query(), .to_query(),
) )
.send()
.await?)
} }
} }
@ -197,14 +206,17 @@ pub mod builders {
self self
} }
pub(crate) fn build(&self, client: &Client) -> RequestBuilder { pub(crate) async fn build(&self, client: &Client) -> Result<Response> {
client Ok(client
.get("https://osu.ppy.sh/api/get_scores") .build_request("https://osu.ppy.sh/api/get_scores")
.await?
.query(&[("b", self.beatmap_id)]) .query(&[("b", self.beatmap_id)])
.query(&self.user.to_query()) .query(&self.user.to_query())
.query(&self.mode.to_query()) .query(&self.mode.to_query())
.query(&self.mods.to_query()) .query(&self.mods.to_query())
.query(&self.limit.map(|v| ("limit", v.to_string())).to_query()) .query(&self.limit.map(|v| ("limit", v.to_string())).to_query())
.send()
.await?)
} }
} }
@ -240,15 +252,18 @@ pub mod builders {
self self
} }
pub(crate) fn build(&self, client: &Client) -> RequestBuilder { pub(crate) async fn build(&self, client: &Client) -> Result<Response> {
client Ok(client
.get(match self.score_type { .build_request(match self.score_type {
UserScoreType::Best => "https://osu.ppy.sh/api/get_user_best", UserScoreType::Best => "https://osu.ppy.sh/api/get_user_best",
UserScoreType::Recent => "https://osu.ppy.sh/api/get_user_recent", UserScoreType::Recent => "https://osu.ppy.sh/api/get_user_recent",
}) })
.await?
.query(&self.user.to_query()) .query(&self.user.to_query())
.query(&self.mode.to_query()) .query(&self.mode.to_query())
.query(&self.limit.map(|v| ("limit", v.to_string())).to_query()) .query(&self.limit.map(|v| ("limit", v.to_string())).to_query())
.send()
.await?)
} }
} }
} }