pub mod beatmap;
pub mod data;
use reqwest::Client;
const TOKEN_URL: &str = "https://osu.ppy.sh/oauth/token";
pub struct Apiv2 {
client_id: u32,
client_secret: String,
client: Client,
}
#[derive(Debug, Error)]
pub enum ApiError {
#[error("reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
}
impl Apiv2 {
pub async fn client_credentials_grant(
&self,
) -> Result<ClientCredentialsToken, ApiError> {
#[derive(Serialize)]
struct TokenRequest<'a> {
client_id: u32,
client_secret: &'a str,
grant_type: &'static str,
scope: &'static str,
}
let req = self
.client
.post(TOKEN_URL)
.json(&TokenRequest {
client_id: self.client_id,
client_secret: &self.client_secret,
grant_type: "client_credentials",
scope: "public",
})
.build()?;
Ok(self.client.execute(req).await?.json().await?)
}
pub async fn authorization_code_grant(
&self,
code: impl AsRef<str>,
redirect_uri: impl AsRef<str>,
) -> Result<AuthorizationCodeToken, ApiError> {
let code = code.as_ref();
let redirect_uri = redirect_uri.as_ref();
#[derive(Serialize)]
struct TokenRequest<'a, 'b, 'c> {
client_id: u32,
client_secret: &'a str,
code: &'b str,
grant_type: &'static str,
redirect_uri: &'c str,
}
let req = self
.client
.post(TOKEN_URL)
.json(&TokenRequest {
client_id: self.client_id,
client_secret: &self.client_secret,
code,
grant_type: "authorization_code",
redirect_uri,
})
.build()?;
Ok(self.client.execute(req).await?.json().await?)
}
}
#[derive(Deserialize)]
pub struct ClientCredentialsToken {
pub token_type: String,
pub expires_in: u64,
pub access_token: String,
}
#[derive(Deserialize)]
pub struct AuthorizationCodeToken {
pub token_type: String,
pub expires_in: u64,
pub access_token: String,
pub refresh_token: String,
}