summaryrefslogtreecommitdiff
path: root/src/requests/start.rs
blob: d0c4411aaeb689710d5422ef05e931c76776790c (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
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
use std::time::Duration;

use futures_util::{StreamExt, SinkExt};
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::StatusCode;
use serde::Deserialize;
use tokio_tungstenite::{connect_async, tungstenite::Message};

use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protocols};

pub async fn start(id: String, ping: bool) -> Result<(), CliError> {

    let send_start = ProgressBar::new(1);

    // TODO: calculate average start-time on server
    send_start.set_style(
        ProgressStyle::with_template("{spinner:.green} ({elapsed}) {wide_msg}")
        .unwrap()
        .tick_chars("|/-\\\\")
    );

    let url = format_url("start", Protocols::Http)?;

    send_start.set_message(format!("connect to {}", url));
    send_start.enable_steady_tick(Duration::from_millis(125));

    let res = reqwest::Client::new()
        .post(url)
        .headers(default_headers()?)
        .body(
            format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping)
        )
        .send()
        .await
        .map_err(CliError::Reqwest)?;

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

            if body.boot {
                send_start.println("connected, sent start packet");
            }

            if ping {
                send_start.println(status_socket(body.uuid, &send_start).await?.to_string());
            }
        },
        _ => {
            let body = serde_json::from_str::<ErrorResponse>(
                &res.text().await.map_err(CliError::Reqwest)?
            )
            .map_err(CliError::Serde)?;

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

    Ok(())
}

async fn status_socket(uuid: String, pb: &ProgressBar) -> Result<bool, CliError> {
    pb.set_message("setup websocket");

    let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?)
        .await
        .expect("Failed to connect");
    pb.println("connected to websocket");

    pb.set_message("send uuid message");
    ws_stream.send(Message::Text(uuid)).await.unwrap();
    pb.println("sent uuid message");

    pb.set_message("wait for message");
    let msg = ws_stream.next().await.unwrap();

    pb.println(format!("msg: {:?}", msg));
    
    ws_stream.close(None).await.unwrap();
    pb.println("connection closed");
    // TODO: Check for correct UUID and timeout
    pb.set_message("verifying message");
    if msg.is_ok() { return Ok(true) }

    Ok(false)
}

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