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.
pub struct BeatmapCache {
client: ratelimit::Ratelimit<surf::Client>,
client: ratelimit::Ratelimit<reqwest::Client>,
cache: dashmap::DashMap<u64, Arc<BeatmapContent>>,
}
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::<Vec<_>>())?,
})
}

View file

@ -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<Response> {
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<reqwest::RequestBuilder> {
Ok(self
.client
.borrow()
.await?
.get(url)
.query(&[("k", &*self.key)]))
}
pub async fn beatmaps(
@ -57,11 +58,7 @@ impl Client {
) -> Result<Vec<Beatmap>> {
let mut r = BeatmapRequestBuilder::new(kind);
f(&mut r);
let res: Vec<raw::Beatmap> = self
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let res: Vec<raw::Beatmap> = r.build(&self).await?.json().await?;
Ok(vec_try_into(res)?)
}
@ -72,11 +69,7 @@ impl Client {
) -> Result<Option<User>, Error> {
let mut r = UserRequestBuilder::new(user);
f(&mut r);
let res: Vec<raw::User> = self
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let res: Vec<raw::User> = r.build(&self).await?.json().await?;
let res = vec_try_into(res)?;
Ok(res.into_iter().next())
}
@ -88,11 +81,7 @@ impl Client {
) -> Result<Vec<Score>, Error> {
let mut r = ScoreRequestBuilder::new(beatmap_id);
f(&mut r);
let res: Vec<raw::Score> = self
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let res: Vec<raw::Score> = r.build(&self).await?.json().await?;
let mut res: Vec<Score> = vec_try_into(res)?;
// with a scores request you need to fill the beatmap ids yourself
@ -126,11 +115,7 @@ impl Client {
) -> Result<Vec<Score>, Error> {
let mut r = UserScoreRequestBuilder::new(u, user);
f(&mut r);
let res: Vec<raw::Score> = self
.build_request(r.build(&self.client))
.await?
.json()
.await?;
let res: Vec<raw::Score> = r.build(&self).await?.json().await?;
let res = vec_try_into(res)?;
Ok(res)
}

View file

@ -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<Response> {
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<Response> {
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<Response> {
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<Response> {
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?)
}
}
}