pub mod api_client;
pub mod at_home;
pub mod auth;
pub mod author;
pub mod captcha;
pub mod chapter;
pub mod cover;
pub mod custom_list;
pub mod feed;
pub mod forums;
pub mod legacy;
pub mod manga;
cfg_oauth! {
pub mod oauth;
}
pub mod ping;
pub mod rating;
pub mod report;
pub mod scanlation_group;
pub mod search;
pub mod settings;
pub mod statistics;
pub mod upload;
pub mod user;
#[cfg(all(feature = "multi-thread", not(feature = "tokio-multi-thread")))]
use futures::lock::Mutex;
pub use mangadex_api_schema::v5 as schema;
use mangadex_api_schema::v5::oauth::ClientInfo;
pub(crate) use mangadex_api_schema::v5::AuthTokens;
use mangadex_api_types::error::Result;
use reqwest::Client;
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
use std::cell::RefCell;
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
use std::rc::Rc;
#[cfg(any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
))]
use std::sync::Arc;
#[cfg(feature = "tokio-multi-thread")]
use tokio::sync::Mutex;
#[cfg(feature = "rw-multi-thread")]
use tokio::sync::RwLock;
#[cfg(feature = "utils")]
use crate::utils::download::DownloadBuilder;
use crate::v5::api_client::ApiClientEndpoint;
use crate::v5::at_home::AtHomeBuilder;
use crate::v5::auth::AuthBuilder;
use crate::v5::author::AuthorBuilder;
use crate::v5::captcha::CaptchaBuilder;
use crate::v5::chapter::ChapterBuilder;
use crate::v5::cover::CoverBuilder;
use crate::v5::custom_list::CustomListBuilder;
use crate::v5::feed::FeedBuilder;
use crate::v5::forums::ForumsEndpoint;
use crate::v5::legacy::LegacyBuilder;
use crate::v5::manga::MangaBuilder;
#[cfg(feature = "oauth")]
use crate::v5::oauth::OAuthBuider;
use crate::v5::ping::PingEndpointBuilder;
use crate::v5::rating::RatingBuilder;
use crate::v5::report::ReportBuilder;
use crate::v5::scanlation_group::ScanlationGroupBuilder;
use crate::v5::search::SearchBuilder;
use crate::v5::settings::SettingsBuilder;
use crate::v5::statistics::StatisticsBuilder;
use crate::v5::upload::UploadBuilder;
use crate::v5::user::UserBuilder;
use crate::HttpClient;
use crate::HttpClientRef;
#[derive(Clone, Debug)]
pub struct MangaDexClient {
pub http_client: HttpClientRef,
}
impl Default for MangaDexClient {
fn default() -> Self {
Self::new_with_http_client(HttpClient::default())
}
}
impl MangaDexClient {
pub fn new(client: Client) -> Self {
Self::new_with_http_client_ref(create_ref_counted_http_client(HttpClient::new(client)))
}
pub fn new_with_http_client_ref(http_client: HttpClientRef) -> Self {
Self { http_client }
}
pub fn new_with_http_client(http_client: HttpClient) -> Self {
Self::new_with_http_client_ref(create_ref_counted_http_client(http_client))
}
pub fn get_http_client(&self) -> HttpClientRef {
self.http_client.clone()
}
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
pub async fn set_auth_tokens(&mut self, auth_tokens: &AuthTokens) -> Result<()> {
let client = &mut self.http_client.try_borrow_mut()?;
client.set_auth_tokens(auth_tokens);
Ok(())
}
#[cfg(any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
))]
pub async fn set_auth_tokens(&self, auth_tokens: &AuthTokens) -> Result<()> {
let mut client = {
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
self.http_client.write().await
}
};
client.set_auth_tokens(auth_tokens);
Ok(())
}
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
pub async fn clear_auth_tokens(&mut self) -> Result<()> {
let client = {
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
{
&mut self.http_client.try_borrow_mut()?
}
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
&mut self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
&mut self.http_client.write().await
}
};
client.clear_auth_tokens();
Ok(())
}
#[cfg(any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
))]
pub async fn clear_auth_tokens(&self) -> Result<()> {
let mut client = {
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
self.http_client.write().await
}
};
client.clear_auth_tokens();
Ok(())
}
pub async fn get_auth_tokens(&self) -> Result<AuthTokens> {
let client = {
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
{
&self.http_client.try_borrow()?
}
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
&self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
&self.http_client.read().await
}
};
client
.get_tokens()
.cloned()
.ok_or(mangadex_api_types::error::Error::MissingTokens)
}
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
pub async fn set_captcha<A: Into<String>>(&mut self, captcha: A) -> Result<()> {
let client = &mut self.http_client.try_borrow_mut()?;
client.set_captcha(captcha);
Ok(())
}
#[cfg(any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
))]
pub async fn set_captcha<A: Into<String>>(&self, captcha: A) -> Result<()> {
let mut client = {
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
self.http_client.write().await
}
};
client.set_captcha(captcha);
Ok(())
}
pub async fn get_captcha(&self) -> Result<String> {
let client = {
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
{
&self.http_client.try_borrow()?
}
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
&self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
&self.http_client.read().await
}
};
client
.get_captcha()
.cloned()
.ok_or(mangadex_api_types::error::Error::MissingCaptcha)
}
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
pub async fn clear_captcha(&mut self) -> Result<()> {
let client = &mut self.http_client.try_borrow_mut()?;
client.clear_captcha();
Ok(())
}
#[cfg(any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
))]
pub async fn clear_captcha(&self) -> Result<()> {
let mut client = {
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
self.http_client.write().await
}
};
client.clear_captcha();
Ok(())
}
cfg_oauth! {
pub async fn get_client_info(&self) -> Result<ClientInfo> {
let client = {
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
{
&self.http_client.try_borrow()?
}
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
&self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
&self.http_client.read().await
}
};
client
.get_client_info()
.cloned()
.ok_or(mangadex_api_types::error::Error::MissingClientInfo)
}
}
pub fn at_home(&self) -> AtHomeBuilder {
AtHomeBuilder::new(self.http_client.clone())
}
pub fn auth(&self) -> AuthBuilder {
AuthBuilder::new(self.http_client.clone())
}
pub fn author(&self) -> AuthorBuilder {
AuthorBuilder::new(self.http_client.clone())
}
pub fn captcha(&self) -> CaptchaBuilder {
CaptchaBuilder::new(self.http_client.clone())
}
pub fn chapter(&self) -> ChapterBuilder {
ChapterBuilder::new(self.http_client.clone())
}
pub fn client(&self) -> ApiClientEndpoint {
ApiClientEndpoint::new(self.http_client.clone())
}
pub fn cover(&self) -> CoverBuilder {
CoverBuilder::new(self.http_client.clone())
}
pub fn custom_list(&self) -> CustomListBuilder {
CustomListBuilder::new(self.http_client.clone())
}
pub fn feed(&self) -> FeedBuilder {
FeedBuilder::new(self.http_client.clone())
}
pub fn ping(&self) -> PingEndpointBuilder {
PingEndpointBuilder::new(self.http_client.clone())
}
pub fn legacy(&self) -> LegacyBuilder {
LegacyBuilder::new(self.http_client.clone())
}
pub fn manga(&self) -> MangaBuilder {
MangaBuilder::new(self.http_client.clone())
}
pub fn rating(&self) -> RatingBuilder {
RatingBuilder::new(self.http_client.clone())
}
pub fn report(&self) -> ReportBuilder {
ReportBuilder::new(self.http_client.clone())
}
pub fn scanlation_group(&self) -> ScanlationGroupBuilder {
ScanlationGroupBuilder::new(self.http_client.clone())
}
pub fn search(&self) -> SearchBuilder {
SearchBuilder::new(self.http_client.clone())
}
pub fn settings(&self) -> SettingsBuilder {
SettingsBuilder::new(self.http_client.clone())
}
pub fn statistics(&self) -> StatisticsBuilder {
StatisticsBuilder::new(self.http_client.clone())
}
pub fn upload(&self) -> UploadBuilder {
UploadBuilder::new(self.http_client.clone())
}
pub fn user(&self) -> UserBuilder {
UserBuilder::new(self.http_client.clone())
}
pub fn api_dev_client() -> Self {
Self::new_with_http_client(HttpClient::api_dev_client())
}
cfg_utils! {
pub fn download(&self) -> DownloadBuilder {
DownloadBuilder::new(self.http_client.clone())
}
}
pub fn forums(&self) -> ForumsEndpoint {
ForumsEndpoint::new(self.http_client.clone())
}
cfg_oauth! {
pub fn oauth(&self) -> OAuthBuider {
OAuthBuider::new(self.http_client.clone())
}
}
}
fn create_ref_counted_http_client(http_client: HttpClient) -> HttpClientRef {
#[cfg(all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
))]
{
Rc::new(RefCell::new(http_client))
}
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
Arc::new(Mutex::new(http_client))
}
#[cfg(feature = "rw-multi-thread")]
{
Arc::new(RwLock::new(http_client))
}
}
#[cfg(all(
feature = "oauth",
all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
)
))]
#[cfg_attr(
docsrs,
doc(cfg(all(
feature = "oauth",
all(
not(feature = "multi-thread"),
not(feature = "tokio-multi-thread"),
not(feature = "rw-multi-thread")
)
)))
)]
impl MangaDexClient {
pub async fn set_client_info(&mut self, client_info: &ClientInfo) -> Result<()> {
let client = &mut self.http_client.try_borrow_mut()?;
client.set_client_info(client_info);
Ok(())
}
pub async fn clear_client_info(&mut self) -> Result<()> {
let client = &mut self.http_client.try_borrow_mut()?;
client.clear_client_info();
Ok(())
}
}
#[cfg(all(
feature = "oauth",
any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
)
))]
#[cfg_attr(
docsrs,
doc(all(
feature = "oauth",
any(
feature = "multi-thread",
feature = "tokio-multi-thread",
feature = "rw-multi-thread"
)
))
)]
impl MangaDexClient {
pub async fn set_client_info(&self, client_info: &ClientInfo) -> Result<()> {
let mut client = {
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
self.http_client.write().await
}
};
client.set_client_info(client_info);
Ok(())
}
pub async fn clear_client_info(&self) -> Result<()> {
let mut client = {
#[cfg(any(feature = "multi-thread", feature = "tokio-multi-thread"))]
{
self.http_client.lock().await
}
#[cfg(feature = "rw-multi-thread")]
{
self.http_client.write().await
}
};
client.clear_client_info();
Ok(())
}
}