use std::sync::Arc;
use reqwest::Client;
use crate::auth::{Auth, Credentials, Token};
use crate::download::{DownloadAction, Downloader};
use crate::error::Result;
use crate::games::{GameRef, Games};
use crate::mods::ModRef;
use crate::reports::Reports;
use crate::request::RequestBuilder;
use crate::routing::Route;
use crate::types::id::{GameId, ModId};
use crate::user::Me;
mod builder;
pub use builder::Builder;
const DEFAULT_HOST: &str = "https://api.mod.io/v1";
const TEST_HOST: &str = "https://api.test.mod.io/v1";
const DEFAULT_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), '/', env!("CARGO_PKG_VERSION"));
#[derive(Clone, Debug)]
pub struct Modio {
pub(crate) inner: Arc<ClientRef>,
}
#[derive(Debug)]
pub(crate) struct ClientRef {
pub(crate) host: String,
pub(crate) client: Client,
pub(crate) credentials: Credentials,
}
impl Modio {
pub fn builder<C: Into<Credentials>>(credentials: C) -> Builder {
Builder::new(credentials)
}
pub fn new<C>(credentials: C) -> Result<Self>
where
C: Into<Credentials>,
{
Builder::new(credentials).build()
}
pub fn host<H, C>(host: H, credentials: C) -> Result<Self>
where
H: Into<String>,
C: Into<Credentials>,
{
Builder::new(credentials).host(host).build()
}
#[must_use]
pub fn with_credentials<CR>(&self, credentials: CR) -> Self
where
CR: Into<Credentials>,
{
Self {
inner: Arc::new(ClientRef {
host: self.inner.host.clone(),
client: self.inner.client.clone(),
credentials: credentials.into(),
}),
}
}
#[must_use]
pub fn with_token<T>(&self, token: T) -> Self
where
T: Into<Token>,
{
Self {
inner: Arc::new(ClientRef {
host: self.inner.host.clone(),
client: self.inner.client.clone(),
credentials: Credentials {
api_key: self.inner.credentials.api_key.clone(),
token: Some(token.into()),
},
}),
}
}
pub fn auth(&self) -> Auth {
Auth::new(self.clone())
}
pub fn games(&self) -> Games {
Games::new(self.clone())
}
pub fn game(&self, game_id: GameId) -> GameRef {
GameRef::new(self.clone(), game_id)
}
pub fn mod_(&self, game_id: GameId, mod_id: ModId) -> ModRef {
ModRef::new(self.clone(), game_id, mod_id)
}
pub async fn download<A>(&self, action: A) -> Result<Downloader>
where
DownloadAction: From<A>,
{
Downloader::new(self.clone(), action.into()).await
}
pub fn user(&self) -> Me {
Me::new(self.clone())
}
pub fn reports(&self) -> Reports {
Reports::new(self.clone())
}
pub(crate) fn request(&self, route: Route) -> RequestBuilder {
RequestBuilder::new(self.clone(), route)
}
}