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
pub struct IpcsApi {
    url: url::Url,
    client: reqwest::Client,
}

impl IpcsApi {
    pub fn new(url: &str) -> Result<Self, url::ParseError> {
        Ok(IpcsApi {
            url: url::Url::parse(url)?,
            client: reqwest::Client::new(),
        })
    }

    pub async fn exec(&self, method: &str, args: &[&str]) -> Result<String, reqwest::Error> {
        let mut url = self.url.clone();
        url.set_path("/api/v0/exec");

        let response = self
            .client
            .post(url)
            .json(&apidefs::ExecReq {
                method: method.to_string(),
                args: args.iter().map(|v| v.to_string()).collect(),
            })
            .send()
            .await?;

        if response.status().is_client_error() || response.status().is_server_error() {
            return Ok(response.text().await?);
        }

        let res: apidefs::ExecResp = response.json().await?;
        Ok(res.hash)
    }
}