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

use reqwest::Error;
use reqwest::blocking::{Client, Response};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct WebGame {
    pub id: String,
    pub moves: Vec<String>,
    pub board: Vec<Vec<u8>>,
    pub render: String,
}

pub struct WebClient {
    client: Client,
    hostname: String,
}

impl WebClient {
    pub fn from_hostname(hostname: String) -> WebClient {
        WebClient {
            client: Client::new(),
            hostname,
        }
    }

    pub fn get_latest_web_game(&self, game_id: &String) -> Result<WebGame, Box<dyn std::error::Error>> {
        let request_url = format!("https://{}/api/games/{}", self.hostname, *game_id);
        let response = reqwest::blocking::get(&request_url)?;
        let web_game: WebGame = response.json()?;
        Result::Ok(web_game)
    }

    pub fn submit_move(&self, web_game: &WebGame, next_move: &String) -> Result<Response, Error> {
        let move_number = web_game.moves.len();
        let request_url = format!("https://{}/api/games/{}/moves/{}", self.hostname, web_game.id, move_number);
        self.client.post(request_url.as_str()).json(next_move).send()
    }
}