koios_sdk/api/
ogmios.rs

1use crate::{
2    error::Result,
3    models::{network::OgmiosTip, requests::OgmiosRequest},
4    Client,
5};
6use serde_json::Value;
7
8impl Client {
9    /// Query the Ogmios service with a custom request
10    ///
11    /// # Arguments
12    ///
13    /// * `method` - The Ogmios method to call
14    /// * `params` - Optional parameters for the method
15    ///
16    /// # Examples
17    ///
18    /// ```no_run
19    /// use koios_sdk::Client;
20    /// use serde_json::json;
21    ///
22    /// #[tokio::main]
23    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
24    ///     let client = Client::new()?;
25    ///     
26    ///     // Query network tip
27    ///     let tip = client.query_ogmios("queryNetwork/tip", None).await?;
28    ///     println!("Network tip: {:?}", tip);
29    ///     
30    ///     // Query with parameters
31    ///     let params = json!({
32    ///         "query": "someQuery",
33    ///         "value": 123
34    ///     });
35    ///     let result = client.query_ogmios("someMethod", Some(params)).await?;
36    ///     println!("Query result: {:?}", result);
37    ///     
38    ///     Ok(())
39    /// }
40    /// ```
41    pub async fn query_ogmios(&self, method: &str, params: Option<Value>) -> Result<OgmiosTip> {
42        let request = if let Some(params) = params {
43            OgmiosRequest::with_params(method.to_string(), params)
44        } else {
45            OgmiosRequest::new(method.to_string())
46        };
47
48        self.post("/ogmios", &request).await
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use serde_json::json;
56    use wiremock::matchers::{body_json, header, method, path};
57    use wiremock::{Mock, MockServer, ResponseTemplate};
58
59    #[tokio::test]
60    async fn test_query_ogmios_with_params() {
61        let mock_server = MockServer::start().await;
62        let client = Client::builder()
63            .base_url(mock_server.uri())
64            .build()
65            .unwrap();
66
67        let params = json!({
68            "query": "testQuery",
69            "value": 123
70        });
71
72        let mock_response = json!({
73            "jsonrpc": "2.0",
74            "method": "testMethod",
75            "result": {
76                "slot": 42000000,
77                "id": "6ed09ba58a56c6e946668038ba4d3cef8eb97a20cbf76c5970e1402e8a8d6541"
78            }
79        });
80
81        let expected_request = json!({
82            "jsonrpc": "2.0",
83            "method": "testMethod",
84            "params": {
85                "query": "testQuery",
86                "value": 123
87            }
88        });
89
90        Mock::given(method("POST"))
91            .and(path("/ogmios"))
92            .and(header("Content-Type", "application/json"))
93            .and(body_json(&expected_request))
94            .respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
95            .mount(&mock_server)
96            .await;
97
98        let response = client
99            .query_ogmios("testMethod", Some(params))
100            .await
101            .unwrap();
102        assert_eq!(response.result.slot, 42000000);
103        assert_eq!(
104            response.result.id,
105            "6ed09ba58a56c6e946668038ba4d3cef8eb97a20cbf76c5970e1402e8a8d6541"
106        );
107    }
108}