1
2use reqwest::Error;
3use reqwest::blocking::{Client, Response};
4use serde::{Serialize, Deserialize};
5
6#[derive(Serialize, Deserialize, Debug)]
7pub struct WebGame {
8 pub id: String,
9 pub moves: Vec<String>,
10 pub board: Vec<Vec<u8>>,
11 pub render: String,
12}
13
14pub struct WebClient {
15 client: Client,
16 hostname: String,
17}
18
19impl WebClient {
20 pub fn from_hostname(hostname: String) -> WebClient {
21 WebClient {
22 client: Client::new(),
23 hostname,
24 }
25 }
26
27 pub fn get_latest_web_game(&self, game_id: &String) -> Result<WebGame, Box<dyn std::error::Error>> {
28 let request_url = format!("https://{}/api/games/{}", self.hostname, *game_id);
29 let response = reqwest::blocking::get(&request_url)?;
30 let web_game: WebGame = response.json()?;
31 Result::Ok(web_game)
32 }
33
34 pub fn submit_move(&self, web_game: &WebGame, next_move: &String) -> Result<Response, Error> {
35 let move_number = web_game.moves.len();
36 let request_url = format!("https://{}/api/games/{}/moves/{}", self.hostname, web_game.id, move_number);
37 self.client.post(request_url.as_str()).json(next_move).send()
38 }
39}
40
41