koios_sdk/api/
ogmios.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::{
    error::Result,
    models::{network::OgmiosTip, requests::OgmiosRequest},
    Client,
};
use serde_json::Value;

impl Client {
    /// Query the Ogmios service with a custom request
    ///
    /// # Arguments
    ///
    /// * `method` - The Ogmios method to call
    /// * `params` - Optional parameters for the method
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    /// use serde_json::json;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     
    ///     // Query network tip
    ///     let tip = client.query_ogmios("queryNetwork/tip", None).await?;
    ///     println!("Network tip: {:?}", tip);
    ///     
    ///     // Query with parameters
    ///     let params = json!({
    ///         "query": "someQuery",
    ///         "value": 123
    ///     });
    ///     let result = client.query_ogmios("someMethod", Some(params)).await?;
    ///     println!("Query result: {:?}", result);
    ///     
    ///     Ok(())
    /// }
    /// ```
    pub async fn query_ogmios(&self, method: &str, params: Option<Value>) -> Result<OgmiosTip> {
        let request = if let Some(params) = params {
            OgmiosRequest::with_params(method.to_string(), params)
        } else {
            OgmiosRequest::new(method.to_string())
        };

        self.post("/ogmios", &request).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use wiremock::matchers::{body_json, header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn test_query_ogmios_with_params() {
        let mock_server = MockServer::start().await;
        let client = Client::builder()
            .base_url(mock_server.uri())
            .build()
            .unwrap();

        let params = json!({
            "query": "testQuery",
            "value": 123
        });

        let mock_response = json!({
            "jsonrpc": "2.0",
            "method": "testMethod",
            "result": {
                "slot": 42000000,
                "id": "6ed09ba58a56c6e946668038ba4d3cef8eb97a20cbf76c5970e1402e8a8d6541"
            }
        });

        let expected_request = json!({
            "jsonrpc": "2.0",
            "method": "testMethod",
            "params": {
                "query": "testQuery",
                "value": 123
            }
        });

        Mock::given(method("POST"))
            .and(path("/ogmios"))
            .and(header("Content-Type", "application/json"))
            .and(body_json(&expected_request))
            .respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
            .mount(&mock_server)
            .await;

        let response = client
            .query_ogmios("testMethod", Some(params))
            .await
            .unwrap();
        assert_eq!(response.result.slot, 42000000);
        assert_eq!(
            response.result.id,
            "6ed09ba58a56c6e946668038ba4d3cef8eb97a20cbf76c5970e1402e8a8d6541"
        );
    }
}