summaryrefslogtreecommitdiff
path: root/src/requests/start.rs
blob: 30f65b90e125551ec772ef267cc9d6e6058eefe0 (plain) (blame)
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
use reqwest::StatusCode;
use serde::Deserialize;

use crate::{config::SETTINGS, error::CliError, default_headers, ErrorResponse};

pub fn start(id: String) -> Result<(), CliError> {
    let res = reqwest::blocking::Client::new()
        .post(
            format!(
                "{}/start",
                SETTINGS.get_string("server").map_err(CliError::Config)?
            )
        )
        .headers(default_headers()?)
        .body(
            format!(r#"{{"id": "{}"}}"#, id)
        )
        .send()
        .map_err(CliError::Reqwest)?;

    match res.status() {
        StatusCode::OK => {
            let body = serde_json::from_str::<StartResponse>(
                &res.text().map_err(CliError::Reqwest)?
            )
            .map_err(CliError::Serde)?;

            if body.boot {
                println!("successfully started {}", body.id);
            }
        },
        _ => {
            let body = serde_json::from_str::<ErrorResponse>(
                &res.text().map_err(CliError::Reqwest)?
            )
            .map_err(CliError::Serde)?;

            println!("got error: {}", body.error);
        }
    }

    Ok(())
}

#[derive(Debug, Deserialize)]
struct StartResponse {
    boot: bool,
    id: String,
}