x-rs 0.1.0

x private library
Documentation
//! X/Twitter GraphQL API client.
//!
//! # Example
//!
//! ```no_run
//! use x_rs::{X, CookieData};
//!
//! #[tokio::main]
//! async fn main() -> x_rs::Result<()> {
//!     let cookies = CookieData::new("ct0_value", "auth_token_value");
//!     let client = X::builder().cookies(cookies).build().await?;
//!
//!     // Fetch user with pinned tweet
//!     let user = client.user("Hi_Ohaasa").await?;
//!     println!("{} has {} followers", user.username, user.follower_count);
//!
//!     // Fetch pinned tweet thread (e.g., daily horoscopes)
//!     if let Some(pinned_id) = &user.pinned_tweet_id {
//!         let detail = client.tweet_detail(pinned_id).await?;
//!         println!("Main: {}", detail.tweet.text);
//!         println!("Thread has {} replies", detail.thread.len());
//!     }
//!
//!     Ok(())
//! }
//! ```

mod auth;
mod client;
mod endpoints;
mod error;
mod features;
mod operations;
mod types;

pub use auth::{Auth, CookieData};
pub use client::{X, XBuilder};
pub use error::{Error, Result};
pub use types::*;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn operations_load() {
        let ops = operations::Operations::new();
        assert!(ops.is_ok());
    }

    #[tokio::test]
    async fn build_anonymous_client() {
        let client = X::builder().build().await;
        assert!(client.is_ok());
    }

    #[tokio::test]
    async fn build_with_cookies() {
        let cookies = CookieData::new("test_ct0", "test_auth");
        let client = X::builder().cookies(cookies).build().await;
        assert!(client.is_ok());
    }

    #[test]
    fn cookie_string() {
        let cookies = CookieData::new("abc", "xyz");
        assert_eq!(cookies.to_cookie_string(), "ct0=abc; auth_token=xyz");
    }

    #[test]
    fn media_type_serialization() {
        let photo = MediaType::Photo;
        let json = serde_json::to_string(&photo).unwrap();
        assert_eq!(json, "\"photo\"");
    }
}

#[cfg(test)]
mod api_tests {
    use super::*;

    async fn auth_client() -> Option<X> {
        let ct0 = std::env::var("X_CT0").ok()?;
        let auth_token = std::env::var("X_AUTH_TOKEN").ok()?;
        let cookies = CookieData::new(ct0, auth_token);
        X::builder().cookies(cookies).build().await.ok()
    }

    #[tokio::test]
    #[ignore]
    async fn fetch_user_profile() {
        let x = auth_client().await.expect("set X_CT0 and X_AUTH_TOKEN");
        let user = x.user("Hi_Ohaasa").await.unwrap();
        assert_eq!(user.username, "Hi_Ohaasa");
        assert!(user.follower_count > 0);
        println!("user: {:?}", user);
    }

    #[tokio::test]
    #[ignore]
    async fn fetch_tweet_detail() {
        let x = auth_client().await.expect("set X_CT0 and X_AUTH_TOKEN");
        let detail = x.tweet_detail("2002501924948701342").await.unwrap();
        println!("main: {}", detail.tweet.text);
        println!("thread: {} tweets", detail.thread.len());
        for t in &detail.thread {
            println!("  - {}", t.text.chars().take(50).collect::<String>());
        }
        assert!(!detail.tweet.id.is_empty());
        assert!(!detail.thread.is_empty());
    }

    #[tokio::test]
    #[ignore]
    async fn fetch_pinned_thread() {
        let x = auth_client().await.expect("set X_CT0 and X_AUTH_TOKEN");

        let user = x.user("Hi_Ohaasa").await.unwrap();
        let pinned_id = user.pinned_tweet_id.expect("user has pinned tweet");

        let detail = x.tweet_detail(&pinned_id).await.unwrap();
        println!("Pinned thread from @{}", user.username);
        println!("Main: {}", detail.tweet.text);
        println!("Thread: {} horoscopes", detail.thread.len());

        assert_eq!(detail.thread.len(), 12);
    }
}