Skip to main content

golgi/api/
whoami.rs

1//! Return the SSB ID of the local sbot instance.
2//!
3//! Implements the following methods:
4//!
5//! - [`Sbot::whoami`]
6
7use crate::{error::GolgiError, sbot::Sbot, utils};
8
9impl Sbot {
10    /// Get the public key of the local identity.
11    ///
12    /// # Example
13    ///
14    /// ```rust
15    /// use golgi::{Sbot, GolgiError};
16    ///
17    /// async fn fetch_id() -> Result<(), GolgiError> {
18    ///     let mut sbot_client = Sbot::init(None, None).await?;
19    ///
20    ///     let pub_key = sbot_client.whoami().await?;
21    ///
22    ///     println!("local ssb id: {}", pub_key);
23    ///
24    ///     Ok(())
25    /// }
26    /// ```
27    pub async fn whoami(&mut self) -> Result<String, GolgiError> {
28        let mut sbot_connection = self.get_sbot_connection().await?;
29        let req_id = sbot_connection.client.whoami_req_send().await?;
30
31        let result = utils::get_async(
32            &mut sbot_connection.rpc_reader,
33            req_id,
34            utils::json_res_parse,
35        )
36        .await?;
37
38        let id = result
39            .get("id")
40            .ok_or_else(|| GolgiError::Sbot("id key not found on whoami call".to_string()))?
41            .as_str()
42            .ok_or_else(|| GolgiError::Sbot("whoami returned non-string value".to_string()))?;
43        Ok(id.to_string())
44    }
45}