summaryrefslogtreecommitdiff
path: root/webapp/src/services.rs
blob: 61f9c69d601ec9289dfcb0944ea6bb26d19f89d4 (plain)
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
use anyhow::Context;
use gloo_net::http::Request;
use protocol::{
    bridge_engine::{TableStatePlayerView},
    card::Card,
    Table, actions::Bid,
};

use crate::utils::ok_json;

pub async fn get_table_player_view(
    table: Table,
) -> Result<TableStatePlayerView, anyhow::Error> {
    let response = Request::get(&format!("/api/table/{}", table.id))
        .send()
        .await
        .context("fetching table data")?;
    let table = ok_json(response).await?;
    Ok(table)
}

pub async fn bid(table: Table, bid: Bid) -> Result<(), anyhow::Error> {
    let response = Request::post(&format!("/api/table/{}/bid", table.id))
        .json(&bid)?
        .send()
        .await
        .context("submitting bid")?;
    ok_json(response).await
}

pub async fn play(table: Table, card: Card) -> Result<(), anyhow::Error> {
    let response = Request::post(&format!("/api/table/{}/play", table.id))
        .json(&card)?
        .send()
        .await
        .context("submitting play")?;
    ok_json(response).await
}

pub async fn new_deal(table: Table) -> Result<(), anyhow::Error> {
    let response =
        Request::post(&format!("/api/table/{}/admin/deal", table.id))
            .send()
            .await
            .context("requesting new deal")?;
    ok_json(response).await
}