Skip to main content

farcaster_rs/casts/
get_casts.rs

1use crate::constants::merkle::API_ROOT;
2use crate::types::casts::casts::CastRoot;
3use crate::types::user::user::UserInfo;
4use crate::Farcaster;
5use std::error::Error;
6
7impl Farcaster {
8    /// Get a users casts by their FID
9    ///
10    /// ## Params
11    /// fid: i64
12    /// limit: Option<i32>
13    /// cursor: Option<&str>
14    ///
15    /// ## Example
16    /// ```no_run
17    /// let casts = farcaster.get_casts_by_fid(0, None, None).await?;
18    /// ```
19    pub async fn get_casts_by_fid(
20        &self,
21        fid: i64,
22        limit: Option<i32>,
23        cursor: Option<&str>,
24    ) -> Result<CastRoot, Box<dyn Error>> {
25        let mut url = format!("{}/v2/casts?fid={}", API_ROOT, fid);
26        if limit.is_some() {
27            url.push_str(format!("&limit={}", limit.unwrap()).as_str())
28        }
29
30        if cursor.is_some() {
31            url.push_str(format!("&cursor={}", cursor.unwrap()).as_str())
32        }
33
34        println!("{}", url);
35
36        let casts_reqwest = &self.reqwest_get(url.as_str()).await?;
37
38        let casts: CastRoot = serde_json::from_str(casts_reqwest)?;
39
40        Ok(casts)
41    }
42
43    /// Get a users casts by their Username
44    ///
45    /// ## Params
46    /// username: &str
47    /// limit: Option<i32>
48    /// cursor: Option<&str>
49    ///
50    /// ## Example
51    /// ```no_run
52    /// let casts = farcaster.get_casts_by_username("cassie", None, None).await?;
53    /// ```
54    pub async fn get_casts_by_username(
55        &self,
56        username: &str,
57        limit: Option<i32>,
58        cursor: Option<&str>,
59    ) -> Result<CastRoot, Box<dyn Error>> {
60        let fid = &self.get_user_by_username(username).await?;
61
62        let casts = &self.get_casts_by_fid(fid.fid, limit, cursor).await?;
63
64        Ok(casts.clone())
65    }
66
67    /// Get a users casts by their Address
68    ///
69    /// ## Params
70    /// address: &str
71    /// limit: Option<i32>
72    /// cursor: Option<&str>
73    ///
74    /// ## Example
75    /// ```no_run
76    /// let casts = farcaster.get_casts_by_address("0x0000....", None, None).await?;
77    /// ```
78    pub async fn get_casts_by_address(
79        &self,
80        address: &str,
81        limit: Option<i32>,
82        cursor: Option<&str>,
83    ) -> Result<CastRoot, Box<dyn Error>> {
84        let address: &UserInfo = &self.get_user_by_address(address).await?;
85
86        let casts = &self.get_casts_by_fid(address.fid, limit, cursor).await?;
87
88        Ok(casts.clone())
89    }
90}