onemoney_protocol/api/governance.rs
1//! Governance and epoch-related API operations.
2//!
3//! This module provides convenient helpers for retrieving epoch information
4//! from the OneMoney REST API using the shared [`Client`] implementation.
5
6use om_rest_types::responses::EpochResponse;
7
8use crate::{
9 client::{
10 Client,
11 config::{
12 api_path,
13 endpoints::governance::{CURRENT_EPOCH, EPOCH_BY_ID},
14 },
15 },
16 error::Result,
17};
18
19impl Client {
20 /// Fetch the current epoch information from the network.
21 ///
22 /// # Returns
23 ///
24 /// The latest epoch metadata including the governance certificate data.
25 ///
26 /// # Example
27 ///
28 /// ```rust,no_run
29 /// # use onemoney_protocol::Client;
30 /// # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
31 /// let client = Client::testnet()?;
32 /// let epoch = client.get_current_epoch().await?;
33 /// println!("Current epoch id: {}", epoch.epoch_id);
34 /// # Ok(())
35 /// # }
36 /// ```
37 pub async fn get_current_epoch(&self) -> Result<EpochResponse> {
38 let path = api_path(CURRENT_EPOCH);
39 self.get(&path).await
40 }
41
42 /// Fetch epoch information by its identifier.
43 ///
44 /// # Arguments
45 ///
46 /// * `epoch_id` - The epoch identifier to query.
47 pub async fn get_epoch_by_id(&self, epoch_id: u64) -> Result<EpochResponse> {
48 let path = api_path(&format!("{EPOCH_BY_ID}?id={epoch_id}"));
49 self.get(&path).await
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56 use crate::client::config::api_path;
57
58 #[test]
59 fn test_current_epoch_endpoint_path() {
60 let path = api_path(CURRENT_EPOCH);
61 assert_eq!(path, "/v1/governances/epoch");
62 assert!(path.contains("/governances/epoch"));
63 }
64
65 #[test]
66 fn test_epoch_by_id_endpoint_path() {
67 let id = 42;
68 let path = api_path(&format!("{EPOCH_BY_ID}?id={id}"));
69 assert_eq!(path, "/v1/governances/epoch/by_id?id=42");
70 assert!(path.contains("id=42"));
71 }
72}