use reqwest_middleware::reqwest::StatusCode;
use std::{error::Error, fmt::Display};
pub mod client;
pub mod illusts;
pub mod models;
pub mod search;
pub mod tokens;
pub mod ugoira;
pub mod users;
#[cfg(feature = "sqlx")]
pub use sqlx;
#[cfg(feature = "clap")]
pub use clap;
pub use reqwest_middleware::reqwest;
#[derive(Debug, Clone)]
pub enum PixivAppError {
TargetNotFound,
RequestFailed,
MissingLogin,
RateLimitReached,
UnhandledStatus(StatusCode),
Unknown,
}
impl Error for PixivAppError {}
impl Display for PixivAppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
PixivAppError::TargetNotFound => "Requested endpoint not found",
PixivAppError::RequestFailed => "Request failed, check request parameters!",
PixivAppError::MissingLogin => "Request failed because of missing authorization!",
PixivAppError::RateLimitReached => "Too many requests, try again later",
PixivAppError::UnhandledStatus(s) => s.as_str(),
PixivAppError::Unknown => "An unknown error happened and no data was returned.",
}
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{assert, assert_eq, env};
#[tokio::test]
async fn illust_details() {
let illust_id = 122388293;
let token = env::var("PIXIV_REFRESH_TOKEN");
let client = client::PixivAppClient::new(
token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"),
);
let illust = client.illust_details(illust_id).await;
assert!(illust.is_ok(), "Expected illustration data: {:#?}", illust);
assert_eq!(illust.unwrap().id, illust_id);
}
}