whisky_provider/maestro/
mod.rs

1mod evaluator;
2mod fetcher;
3pub mod models;
4mod submitter;
5pub mod utils;
6use whisky_common::*;
7
8use maestro_rust_sdk::models::transactions::RedeemerEvaluation;
9use reqwest::RequestBuilder;
10use serde::Serialize;
11
12#[derive(Debug, Clone)]
13pub struct Maestro {
14    api_key: String,
15    http_client: reqwest::Client,
16    pub base_url: String,
17}
18
19impl Maestro {
20    pub fn new(api_key: String, network: String) -> Self {
21        let base_url = format!("https://{}.gomaestro-api.org/v1", &network,);
22        let http_client = reqwest::Client::builder()
23            .timeout(std::time::Duration::from_secs(300))
24            .build()
25            .expect("Failed to create HTTP client");
26
27        Maestro {
28            api_key,
29            http_client,
30            base_url,
31        }
32    }
33
34    async fn send_request(
35        &self,
36        req: RequestBuilder,
37        response_body: &mut String,
38    ) -> Result<(), Box<dyn std::error::Error>> {
39        let req = req
40            .header("Accept", "application/json")
41            .header("api-key", &self.api_key)
42            .build()?;
43
44        let response = self.http_client.execute(req).await?;
45
46        println!("response: {:?}", response);
47
48        if response.status().is_success() {
49            *response_body = response.text().await?;
50            Ok(())
51        } else {
52            Err(format!("Error: {}", response.status()).into())
53        }
54    }
55
56    pub async fn get(&self, url: &str) -> Result<String, WError> {
57        let req = self.http_client.get(format!("{}{}", &self.base_url, url));
58        let mut response_body = String::new();
59        self.send_request(req, &mut response_body)
60            .await
61            .map_err(WError::from_err("Maestro - get - send_request"))?;
62        Ok(response_body)
63    }
64
65    pub async fn post<T: Serialize>(&self, url: &str, body: T) -> Result<String, WError> {
66        let json_body =
67            serde_json::to_string(&body).map_err(WError::from_err("Maestro - post - json_body"))?;
68
69        let req = self
70            .http_client
71            .post(format!("{}{}", &self.base_url, url))
72            .header("Content-Type", "application/json")
73            .body(json_body);
74
75        let mut response_body = String::new();
76        self.send_request(req, &mut response_body)
77            .await
78            .map_err(WError::from_err("Maestro - post - send_request"))?;
79        Ok(response_body)
80    }
81}
82
83#[derive(Clone, Debug)]
84pub struct MaestroProvider {
85    pub maestro_client: Maestro,
86}
87
88impl MaestroProvider {
89    pub fn new(api_key: &str, network: &str) -> MaestroProvider {
90        let maestro_client = Maestro::new(api_key.to_string(), network.to_string());
91        MaestroProvider { maestro_client }
92    }
93}