Implement Beatmap request + parsing

This commit is contained in:
Natsu Kagami 2019-12-23 17:33:46 -05:00
parent efcf062e8f
commit 0cc9b318ad
7 changed files with 551 additions and 200 deletions

137
youmubot-osu/src/request.rs Normal file
View file

@ -0,0 +1,137 @@
use crate::models::Mode;
use chrono::{DateTime, Utc};
use reqwest::{Client, RequestBuilder};
trait ToQuery {
fn to_query(&self) -> Vec<(&'static str, String)>;
}
impl<T: ToQuery> ToQuery for Option<T> {
fn to_query(&self) -> Vec<(&'static str, String)> {
match self {
Some(ref v) => v.to_query(),
None => vec![],
}
}
}
impl ToQuery for Mode {
fn to_query(&self) -> Vec<(&'static str, String)> {
vec![("m", (*self as u8).to_string())]
}
}
impl ToQuery for (&'static str, String) {
fn to_query(&self) -> Vec<(&'static str, String)> {
vec![(self.0, self.1.clone())]
}
}
impl ToQuery for (&'static str, DateTime<Utc>) {
fn to_query(&self) -> Vec<(&'static str, String)> {
vec![(self.0, format!("{}", self.1.date().format("%Y-%m-%d")))]
}
}
pub enum UserID {
Username(String),
ID(u64),
Auto(String),
}
impl ToQuery for UserID {
fn to_query(&self) -> Vec<(&'static str, String)> {
use UserID::*;
match self {
Username(ref s) => vec![("u", s.clone()), ("type", "string".to_owned())],
ID(u) => vec![("u", u.to_string()), ("type", "id".to_owned())],
Auto(ref s) => vec![("u", s.clone())],
}
}
}
pub enum BeatmapRequestKind {
ByUser(UserID),
Beatmap(u64),
Beatmapset(u64),
BeatmapHash(String),
}
impl ToQuery for BeatmapRequestKind {
fn to_query(&self) -> Vec<(&'static str, String)> {
use BeatmapRequestKind::*;
match self {
ByUser(ref u) => u.to_query(),
Beatmap(b) => vec![("b", b.to_string())],
Beatmapset(s) => vec![("s", s.to_string())],
BeatmapHash(ref h) => vec![("h", h.clone())],
}
}
}
pub mod builders {
use super::*;
/// A builder for a Beatmap request.
pub struct BeatmapRequestBuilder {
kind: BeatmapRequestKind,
since: Option<DateTime<Utc>>,
mode: Mode,
converted: bool,
}
impl BeatmapRequestBuilder {
pub(crate) fn new(kind: BeatmapRequestKind) -> Self {
BeatmapRequestBuilder {
kind,
since: None,
mode: Mode::Std,
converted: false,
}
}
pub fn since(&mut self, since: DateTime<Utc>) -> &Self {
self.since = Some(since);
self
}
pub fn mode(&mut self, mode: Mode, converted: bool) -> &Self {
self.mode = mode;
self.converted = converted;
self
}
pub(crate) fn build(self, client: &Client) -> RequestBuilder {
client
.get("https://osu.ppy.sh/api/get_beatmaps")
.query(&self.kind.to_query())
.query(&self.since.map(|v| ("since", v)).to_query())
.query(&self.mode.to_query())
.query(
&(if self.converted {
Some(("a", "1".to_owned()))
} else {
None
})
.to_query(),
)
}
}
}
pub struct UserRequest {
pub user: UserID,
pub mode: Option<Mode>,
pub event_days: Option<u8>,
}
pub struct ScoreRequest {
pub beatmap_id: u64,
pub user: Option<UserID>,
pub mode: Option<Mode>,
pub mods: u64, // Later
}
pub struct UserBestRequest {
pub user: UserID,
pub mode: Option<Mode>,
}
pub struct UserRecentRequest {
pub user: UserID,
pub mode: Option<Mode>,
}