rspx/lib.rs
1//! Pixiv API Rust Client
2//!
3//! This is a Rust client library for interacting with Pixiv API, providing authentication, network requests and other features.
4//!
5//! ## Features
6//!
7//! - OAuth2 password credential authentication
8//! - Automatic token refresh
9//! - Support for multiple runtimes (tokio/async-std/smol)
10//! - Comprehensive error handling
11//! - Detailed logging
12//! - SNI bypass support (for bypassing network restrictions)
13//!
14//! ## Quick Start
15//!
16//! ```rust
17//! use pixiv_rs::auth::AuthClient;
18//!
19//! #[tokio::main]
20//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
21//! // Initialize logging
22//! tracing_subscriber::fmt::init();
23//!
24//! // Create authentication client
25//! let mut auth_client = AuthClient::new()?;
26//!
27//! // Login with username and password
28//! let auth_response = auth_client.login("your_username", "your_password").await?;
29//!
30//! println!("Login successful! User ID: {}", auth_response.user.id);
31//!
32//! Ok(())
33//! }
34//! ```
35
36pub mod auth;
37pub mod client {
38 pub mod app;
39 pub mod public;
40 pub mod bypass_sni;
41}
42pub mod error;
43pub mod models {
44 pub mod app;
45 pub mod public;
46}
47pub mod network;
48pub mod utils;
49
50// Re-export common types and functions
51pub use auth::{AuthClient, AuthResponse, User as AuthUser};
52pub use client::app::AppClient;
53pub use client::public::PublicClient;
54pub use client::bypass_sni::BypassSniAppClient;
55
56/// Example of using SNI bypass feature
57///
58/// ```rust
59/// use pixiv_rs::client::bypass_sni::BypassSniAppClient;
60///
61/// #[tokio::main]
62/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
63/// // Use IP address to bypass SNI restrictions
64/// let client = BypassSniAppClient::with_ip("210.140.131.145")?;
65///
66/// // Set access token
67/// client.http_client.set_access_token("your_access_token".to_string());
68///
69/// // Call API
70/// // let illust = client.illust_detail(12345).await?;
71///
72/// Ok(())
73/// }
74/// ```
75pub use error::{ApiErrorCode, ApiErrorDetails, PixivError, Result};
76pub use models::app::{
77 Comment, CommentAccessControl, CommentsResponse, ContentType, Duration, Filter, FollowRestrict,
78 Illust, IllustBookmarkResponse, IllustDetail, IllustFollowResponse, ImageUrls, MetaPage, MetaSinglePage,
79 ProfileImageUrls, RankingLabel, RankingMode, RankingResponse, RecommendedResponse, RestrictionAttributes,
80 SearchIllustResponse, SearchTarget, Series, Sort, Tag, TrendingTag, TrendingTagsResponse,
81 UgoiraFrame, UgoiraMetadata, UgoiraMetadataResponse, User as AppUser, UserFollowerResponse,
82 UserFollowingResponse, UserMypixivResponse, UserPreview, ZipUrls,
83};
84pub use models::public::{
85 PublicIllust, PublicUser, PublicSearchResponse, PublicUserDetail, PublicUserIllusts, PublicUserBookmarks,
86 PublicSearchResponse as PublicSearchIllustResponse, SearchTarget as PublicSearchTarget, Sort as PublicSort,
87 Restrict as PublicRestrict, ContentType as PublicContentType, Duration as PublicDuration, Filter as PublicFilter
88};
89pub use network::HttpClient;
90pub use utils::{download, extract_extension, format_file_size, parse_qs, safe_filename, set_accept_language};
91
92/// Library version information
93pub const VERSION: &str = env!("CARGO_PKG_VERSION");
94
95/// Library name
96pub const NAME: &str = env!("CARGO_PKG_NAME");
97
98/// Initialize logger
99///
100/// This is a convenience function for initializing the default logger.
101/// In production environments, you may need to configure more complex logging settings.
102pub fn init_logging() {
103 #[cfg(feature = "tracing-subscriber")]
104 {
105 use tracing_subscriber::{fmt, EnvFilter};
106
107 fmt()
108 .with_env_filter(EnvFilter::from_default_env())
109 .init();
110 }
111
112 #[cfg(not(feature = "tracing-subscriber"))]
113 {
114 // If tracing-subscriber is not enabled, do not initialize logger
115 // Users need to configure the logger themselves
116 eprintln!("Warning: tracing-subscriber feature not enabled, logs may not be displayed");
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn test_library_info() {
126 assert!(!VERSION.is_empty());
127 assert_eq!(NAME, "pixiv_rs");
128 }
129}