DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

0344717b58e35c3f188abbf4648c6601245e566c — Jonni Liljamo 1 year, 8 months ago 6cb4680
feat(sdbclient): patch game state API endpoin
2 files changed, 48 insertions(+), 0 deletions(-)

M sdbclient/src/api/game/mod.rs
A sdbclient/src/api/game/patchstate.rs
M sdbclient/src/api/game/mod.rs => sdbclient/src/api/game/mod.rs +3 -0
@@ 24,3 24,6 @@ pub use join::*;

mod mygames;
pub use mygames::*;

mod patchstate;
pub use patchstate::*;

A sdbclient/src/api/game/patchstate.rs => sdbclient/src/api/game/patchstate.rs +45 -0
@@ 0,0 1,45 @@
/*
 * This file is part of sdbclient
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use reqwest;
use serde::{Deserialize, Serialize};

use super::{
    types::{Game, GameState},
    APIErrorWrapper,
};

#[derive(Serialize)]
pub struct PostState {
    pub state: GameState,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ResponsePatchGameState {
    Error(APIErrorWrapper),
    Valid(Game),
}

pub fn patch_state(
    api_address: String,
    token: String,
    game_id: String,
    state: GameState,
) -> ResponsePatchGameState {
    let client = reqwest::blocking::Client::new();

    let resp = client
        .patch(&format!("{}/game/{}/state", api_address, game_id))
        .header("Authorization", token)
        .json(&PostState { state })
        .send()
        .unwrap();

    resp.json().unwrap()
}