use crate::auth::o_auth::OAuthInfo;
use crate::auth::{AuthInfo, AuthType};
use crate::request::metadata::MetaData;
use crate::Result;
use crate::{auth, Error, NadeoClient};
use futures::future::join;
use reqwest::Client;
use thiserror::Error;
type EMail = String;
type Password = String;
type Identifier = String;
type Secret = String;
#[derive(Debug, Clone, Default)]
pub struct NadeoClientBuilder {
normal_auth: Option<(EMail, Password)>,
o_auth: Option<(Identifier, Secret)>,
user_agent: Option<String>,
}
impl NadeoClientBuilder {
pub fn with_normal_auth(mut self, email: &str, password: &str) -> Self {
self.normal_auth = Some((email.to_string(), password.to_string()));
self
}
pub fn with_oauth_auth(mut self, identifier: &str, secret: &str) -> Self {
self.o_auth = Some((identifier.to_string(), secret.to_string()));
self
}
pub fn user_agent(mut self, user_agent: &str) -> Self {
self.user_agent = Some(user_agent.to_string());
self
}
pub async fn build(self) -> Result<NadeoClient> {
if self.o_auth.is_none() && self.normal_auth.is_none() {
return Err(Error::from(NadeoClientBuilderError::MissingCredentials));
}
if self.user_agent.is_none() {
return Err(Error::from(NadeoClientBuilderError::MissingUserAgent));
}
let meta_data = MetaData {
user_agent: self.user_agent.unwrap(),
};
let client = Client::new();
let mut normal_auth = None;
let mut live_auth = None;
if let Some(auth) = self.normal_auth {
let ticket = auth::get_ubi_auth_ticket(&auth.0, &auth.1, &meta_data, &client).await?;
let normal_auth_fut =
AuthInfo::new(AuthType::NadeoServices, &ticket, &meta_data, &client);
let live_auth_fut =
AuthInfo::new(AuthType::NadeoLiveServices, &ticket, &meta_data, &client);
let (n_auth, l_auth) = join(normal_auth_fut, live_auth_fut).await;
normal_auth = Some(n_auth?);
live_auth = Some(l_auth?);
}
let mut o_auth = None;
if let Some(auth) = self.o_auth {
let auth = OAuthInfo::new(&auth.0, &auth.1, &client).await?;
o_auth = Some(auth)
}
Ok(NadeoClient {
client,
normal_auth,
live_auth,
o_auth,
meta_data,
})
}
}
#[derive(Error, Debug)]
pub enum NadeoClientBuilderError {
#[error("No credentials were provided. At least 1 auth method is required")]
MissingCredentials,
#[error("No UserAgent was provided")]
MissingUserAgent,
}