use crate::auth::o_auth::OAuthInfo;
use crate::auth::{AuthInfo, AuthType};
use crate::request::NadeoRequest;
use crate::{Error, Result};
use reqwest::{Client, Response};
use crate::client::client_builder::NadeoClientBuilder;
use crate::request::metadata::MetaData;
use thiserror::Error;
pub mod client_builder;
pub(crate) const NADEO_AUTH_URL: &str =
"https://prod.trackmania.core.nadeo.online/v2/authentication/token/ubiservices";
pub(crate) const NADEO_SERVER_AUTH_URL: &str =
"https://prod.trackmania.core.nadeo.online/v2/authentication/token/basic";
pub(crate) const NADEO_REFRESH_URL: &str =
"https://prod.trackmania.core.nadeo.online/v2/authentication/token/refresh";
pub(crate) const UBISOFT_APP_ID: &str = "86263886-327a-4328-ac69-527f0d20a237";
pub(crate) const EXPIRATION_TIME_BUFFER: i64 = 60;
#[derive(Debug, Clone)]
pub struct NadeoClient {
pub(crate) client: Client,
pub(crate) normal_auth: Option<AuthInfo>,
pub(crate) live_auth: Option<AuthInfo>,
pub(crate) o_auth: Option<OAuthInfo>,
pub(crate) meta_data: MetaData,
}
impl NadeoClient {
pub fn builder() -> NadeoClientBuilder {
NadeoClientBuilder::default()
}
pub async fn execute(&mut self, request: NadeoRequest) -> Result<Response> {
match request.auth_type {
AuthType::NadeoServices => {
if let Some(auth) = &mut self.normal_auth {
auth.execute(request, &self.meta_data, &self.client).await
} else {
Err(Error::from(ClientError::MissingNadeoAuth))
}
}
AuthType::NadeoLiveServices => {
if let Some(auth) = &mut self.live_auth {
auth.execute(request, &self.meta_data, &self.client).await
} else {
Err(Error::from(ClientError::MissingNadeoAuth))
}
}
AuthType::OAuth => {
if let Some(auth) = &mut self.o_auth {
auth.execute(request, &self.meta_data, &self.client).await
} else {
Err(Error::from(ClientError::MissingOAuth))
}
}
}
}
}
#[derive(Error, Debug)]
pub enum ClientError {
#[error("Client does not have credentials for NadeoServices or NadeoLiveServices")]
MissingNadeoAuth,
#[error("Client does not have OAuth credentials")]
MissingOAuth,
}