~starkingdoms/starkingdoms

ref: 6fca7a488e3c678ed19e333f01494052b622acfb starkingdoms/server/src/api.rs -rw-r--r-- 2.5 KiB
6fca7a48 — ghostlyzsh attachment partially working, weird stuff though 2 years ago
                                                                                
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
97
98
use log::error;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use starkingdoms_protocol::api::APISavedPlayerData;
use std::error::Error;

#[derive(Serialize, Deserialize)]
pub struct BeaminRequest {
    pub api_token: String,
    pub user_auth_realm_id: String,
    pub user_auth_token: String,
}

#[derive(Serialize, Deserialize)]
pub struct BeaminResponse {
    pub save_id: String,
    pub save: APISavedPlayerData,
}

pub async fn load_player_data_from_api(
    token: &str,
    user_id: &str,
    internal_token: &str,
) -> Result<APISavedPlayerData, Box<dyn Error>> {
    let client = reqwest::Client::new();

    let req_body = BeaminRequest {
        api_token: internal_token.to_owned(),
        user_auth_realm_id: user_id.to_owned(),
        user_auth_token: token.to_owned(),
    };

    let res = client
        .post(format!("{}/beamin", std::env::var("STK_API_URL").unwrap()))
        .header("Content-Type", "application/json")
        .body(serde_json::to_string(&req_body)?)
        .send()
        .await?;

    if res.status() == StatusCode::NO_CONTENT {
        return Ok(APISavedPlayerData::default());
    }

    if res.status() != StatusCode::OK {
        error!(
            "error with API call (status: {}, body: {})",
            res.status(),
            res.text().await?
        );
        return Err("Error with API call".into());
    }

    let resp: BeaminResponse = serde_json::from_str(&res.text().await?)?;

    Ok(resp.save)
}

#[derive(Serialize, Deserialize)]
pub struct BeamoutRequest {
    pub api_token: String,
    pub user_auth_realm_id: String,
    pub user_auth_token: String,
    pub data: APISavedPlayerData,
}

pub async fn save_player_data_to_api(
    data: &APISavedPlayerData,
    token: &str,
    user_id: &str,
    internal_token: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
    let client = reqwest::Client::new();

    let req_body = BeamoutRequest {
        api_token: internal_token.to_owned(),
        user_auth_realm_id: user_id.to_owned(),
        user_auth_token: token.to_owned(),
        data: data.to_owned(),
    };

    let res = client
        .post(format!("{}/beamout", std::env::var("STK_API_URL").unwrap()))
        .header("Content-Type", "application/json")
        .body(serde_json::to_string(&req_body)?)
        .send()
        .await?;

    if res.status() != StatusCode::OK {
        error!(
            "error with API call (status: {}, body: {})",
            res.status(),
            res.text().await?
        );
        return Err("Error with API call".into());
    }

    Ok(())
}