Skip to main content

farcaster_rs/users/
get_user.rs

1use crate::constants::merkle::API_ROOT;
2use crate::types::user::user::{UserInfo, UserRoot};
3use crate::Farcaster;
4use std::error::Error;
5
6impl Farcaster {
7    /// Get information about a user via their Farcaster ID (more commonly shortened to FID throughout this codebase
8    pub async fn get_user_by_fid(&self, fid: u64) -> Result<UserInfo, Box<dyn Error>> {
9        // make sure fid exists
10        if let Some(_addr) = self.registry.get_address_by_fid(fid) {
11            let response = self
12                .reqwest_get(&format!("{}/v2/user?fid={}", API_ROOT, fid))
13                .await?;
14            let user_root: UserRoot = serde_json::from_str(&response)?;
15
16            return Ok(user_root.result.user);
17        }
18
19        Err(Box::from(format!(
20            "FID '{}' not found in Farcaster ID Registry",
21            fid
22        )))
23    }
24
25    /// Get information about a user via their Farcaster Username
26    pub async fn get_user_by_username(&self, username: &str) -> Result<UserInfo, Box<dyn Error>> {
27        if let Some(fid) = self.registry.get_fid_by_username(username) {
28            return self.get_user_by_fid(fid).await;
29        }
30
31        Err(Box::from(format!(
32            "User '{}' not found in Farcaster Name Registry",
33            username
34        )))
35    }
36
37    /// Get information about a user via their Ethereum address
38    pub async fn get_user_by_address(&self, address: &str) -> Result<UserInfo, Box<dyn Error>> {
39        if let Some(fid) = self.registry.get_fid_by_address(address) {
40            return self.get_user_by_fid(fid).await;
41        }
42
43        Err(Box::from(format!(
44            "Address '{}' not found in Farcaster ID Registry",
45            address
46        )))
47    }
48}