use std::{error::Error, fmt::Display};
use reqwest::StatusCode;
pub mod client;
pub mod illusts;
pub mod models;
pub mod search;
pub mod ugoira;
pub mod users;
#[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, sync::Arc};
#[tokio::test]
async fn login() {
let token = env::var("PIXIV_REFRESH_TOKEN");
let mut client = client::PixivAppClient::new(
token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"),
);
client.refresh_token().await;
let cloned_access_token = Arc::clone(&client.access_token);
let t = cloned_access_token.lock().await;
assert!(!t.is_empty(), "Expected to receive token!");
}
#[tokio::test]
async fn illust_details() {
let illust_id = 122388293;
let token = env::var("PIXIV_REFRESH_TOKEN");
let mut client = client::PixivAppClient::new(
token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"),
);
client.refresh_token().await;
let illust = client.illust_details(illust_id).await;
assert!(illust.is_ok(), "Expected illustration data: {:#?}", illust);
assert_eq!(illust.unwrap().id, illust_id);
}
}