gevulot_rs/
pin_client.rs

1use std::sync::Arc;
2use tokio::sync::RwLock;
3
4use crate::{
5    base_client::BaseClient,
6    error::{Error, Result},
7    proto::gevulot::gevulot::{
8        MsgAckPin, MsgAckPinResponse, MsgCreatePin, MsgCreatePinResponse, MsgDeletePin,
9        MsgDeletePinResponse,
10    },
11};
12
13/// Client for managing pins in the Gevulot system.
14#[derive(Debug, Clone)]
15pub struct PinClient {
16    base_client: Arc<RwLock<BaseClient>>,
17}
18
19impl PinClient {
20    /// Creates a new instance of PinClient.
21    ///
22    /// # Arguments
23    ///
24    /// * `base_client` - An Arc-wrapped RwLock of the BaseClient.
25    ///
26    /// # Returns
27    ///
28    /// A new instance of PinClient.
29    pub fn new(base_client: Arc<RwLock<BaseClient>>) -> Self {
30        Self { base_client }
31    }
32
33    /// Lists all pins.
34    ///
35    /// # Returns
36    ///
37    /// A Result containing a vector of pins or an error.
38    ///
39    /// # Errors
40    ///
41    /// This function will return an error if the request to the Gevulot client fails.
42    pub async fn list(&mut self) -> Result<Vec<crate::proto::gevulot::gevulot::Pin>> {
43        let request = crate::proto::gevulot::gevulot::QueryAllPinRequest { pagination: None };
44        let response = self
45            .base_client
46            .write()
47            .await
48            .gevulot_client
49            .pin_all(request)
50            .await?;
51        Ok(response.into_inner().pin)
52    }
53
54    /// Gets a pin by its CID.
55    ///
56    /// # Arguments
57    ///
58    /// * `cid` - The CID of the pin to retrieve.
59    ///
60    /// # Returns
61    ///
62    /// A Result containing the pin or an error.
63    ///
64    /// # Errors
65    ///
66    /// This function will return an error if the pin is not found or if the request to the Gevulot client fails.
67    pub async fn get(&mut self, cid: &str) -> Result<crate::proto::gevulot::gevulot::Pin> {
68        let request = crate::proto::gevulot::gevulot::QueryGetPinRequest {
69            cid: cid.to_owned(),
70        };
71        let response = self
72            .base_client
73            .write()
74            .await
75            .gevulot_client
76            .pin(request)
77            .await?;
78        response.into_inner().pin.ok_or(Error::NotFound)
79    }
80
81    /// Creates a new pin.
82    ///
83    /// # Arguments
84    ///
85    /// * `msg` - The message containing the details of the pin to create.
86    ///
87    /// # Returns
88    ///
89    /// A Result containing the response of the create pin operation or an error.
90    ///
91    /// # Errors
92    ///
93    /// This function will return an error if the request to the Gevulot client fails.
94    pub async fn create(&mut self, msg: MsgCreatePin) -> Result<MsgCreatePinResponse> {
95        let resp: MsgCreatePinResponse = self
96            .base_client
97            .write()
98            .await
99            .send_msg_sync(msg, "")
100            .await?;
101        Ok(resp)
102    }
103
104    /// Deletes a pin.
105    ///
106    /// # Arguments
107    ///
108    /// * `msg` - The message containing the details of the pin to delete.
109    ///
110    /// # Returns
111    ///
112    /// A Result containing the response of the delete pin operation or an error.
113    ///
114    /// # Errors
115    ///
116    /// This function will return an error if the request to the Gevulot client fails.
117    pub async fn delete(&mut self, msg: MsgDeletePin) -> Result<MsgDeletePinResponse> {
118        let resp: MsgDeletePinResponse = self
119            .base_client
120            .write()
121            .await
122            .send_msg_sync(msg, "")
123            .await?;
124        Ok(resp)
125    }
126
127    /// Acknowledges a pin.
128    ///
129    /// # Arguments
130    ///
131    /// * `msg` - The message containing the details of the pin to acknowledge.
132    ///
133    /// # Returns
134    ///
135    /// A Result containing the response of the acknowledge pin operation or an error.
136    ///
137    /// # Errors
138    ///
139    /// This function will return an error if the request to the Gevulot client fails.
140    pub async fn ack(&mut self, msg: MsgAckPin) -> Result<MsgAckPinResponse> {
141        let resp: MsgAckPinResponse = self
142            .base_client
143            .write()
144            .await
145            .send_msg_sync(msg, "")
146            .await?;
147        Ok(resp)
148    }
149}