gosh_remote/
client.rs

1// [[file:../remote.note::8bb618e6][8bb618e6]]
2use super::*;
3use reqwest;
4// 8bb618e6 ends here
5
6// [[file:../remote.note::d2c8de54][d2c8de54]]
7/// Client for remote execution
8#[derive(Debug, Clone)]
9pub struct Client {
10    client: reqwest::Client,
11    service_uri: String,
12}
13
14impl Client {
15    /// Connect to remote service using address like "localhost:12345"
16    pub fn connect(address: impl std::fmt::Display) -> Self {
17        // by the default there is no timeout
18        let client = reqwest::Client::builder().build().expect("reqwest client");
19        let service_uri = format!("http://{}", address);
20        Self { client, service_uri }
21    }
22}
23// d2c8de54 ends here
24
25// [[file:../remote.note::743b32f9][743b32f9]]
26impl Client {
27    /// Apply Post request
28    pub(crate) async fn post(&self, end_point: &str, data: impl serde::Serialize) -> Result<String> {
29        trace!("post to {end_point:?}");
30        let uri = format!("{}/{end_point}", self.service_uri);
31        let resp = self.client.post(&uri).json(&data).send().await?.text().await?;
32        Ok(resp)
33    }
34}
35// 743b32f9 ends here