Skip to main content

farcaster_rs/assets/
get_user_collections.rs

1use crate::constants::merkle::API_ROOT;
2use crate::types::assets::user_collections::UserCollectionsRoot;
3use crate::Farcaster;
4use std::error::Error;
5
6impl Farcaster {
7    /// Get all collections an FID owns
8    /// ```no_run
9    /// use farcaster_rs::{Account, Farcaster};
10    ///
11    /// let account: Account = Account::from_mnemonic("mnemonic phrase", None).await?;
12    /// let farcaster: Farcaster = Farcaster::new("eth provider", account).await?;
13    ///
14    /// // Returns all Farcaster collection owners of the ID specified
15    /// let collections = farcaster.get_collections_by_fid(5, None, None).await?;
16    /// ```
17    pub async fn get_collections_by_fid(
18        &self,
19        fid: i64,
20        limit: Option<i64>,
21        cursor: Option<String>,
22    ) -> Result<UserCollectionsRoot, Box<dyn Error>> {
23        let mut url = format!("{}/v2/user-collections?ownerFid={}", API_ROOT, fid);
24
25        if limit.is_some() {
26            url.push_str(format!("&limit={}", limit.unwrap()).as_str())
27        }
28
29        if cursor.is_some() {
30            url.push_str(format!("&cursor={}", cursor.unwrap()).as_str())
31        }
32
33        let collections_reqwest = &self.reqwest_get(&url).await?;
34
35        let collections: UserCollectionsRoot = serde_json::from_str(&collections_reqwest)?;
36
37        Ok(collections)
38    }
39
40    /// Get all collections a username owns
41    /// ```no_run
42    /// use farcaster_rs::{Account, Farcaster};
43    ///
44    /// let account: Account = Account::from_mnemonic("mnemonic phrase", None).await?;
45    /// let farcaster: Farcaster = Farcaster::new("eth provider", account).await?;
46    ///
47    /// // Returns all Farcaster collection owners of the ID specified
48    /// let collections = farcaster.get_collections_by_username("ace", None, None).await?;
49    /// ```
50    pub async fn get_collections_by_username(
51        &self,
52        username: &str,
53        limit: Option<i64>,
54        cursor: Option<String>,
55    ) -> Result<UserCollectionsRoot, Box<dyn Error>> {
56        let fid = &self.get_user_by_username(username).await?;
57
58        let collection = &self.get_collections_by_fid(fid.fid, limit, cursor).await?;
59
60        Ok(collection.clone())
61    }
62
63    /// Get all collections an address owns
64    /// ```no_run
65    /// use farcaster_rs::{Account, Farcaster};
66    ///
67    /// let account: Account = Account::from_mnemonic("mnemonic phrase", None).await?;
68    /// let farcaster: Farcaster = Farcaster::new("eth provider", account).await?;
69    ///
70    /// // Returns all Farcaster collection owners of the ID specified
71    /// let collections = farcaster.get_collections_by_address("0x000.....", None, None).await?;
72    /// ```
73    pub async fn get_collections_by_address(
74        &self,
75        address: &str,
76        limit: Option<i64>,
77        cursor: Option<String>,
78    ) -> Result<UserCollectionsRoot, Box<dyn Error>> {
79        let fid = &self.get_user_by_address(address).await?;
80
81        let collection = &self.get_collections_by_fid(fid.fid, limit, cursor).await?;
82
83        Ok(collection.clone())
84    }
85}