DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: f8676a87d3decf0594df9e8b6dbde7f5a50de1de deck-builder/sdbclient/src/api/user/mod.rs -rw-r--r-- 2.3 KiB
f8676a87Jonni Liljamo fix(sdbapi): simplify returns for game endpoints 1 year, 10 months 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
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * This file is part of sdbclient
 * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

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

use super::APIErrorWrapper;

#[derive(Serialize)]
pub struct PostToken {
    pub email: String,
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ResultToken {
    pub token: String,
    pub id: String,
}

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

pub fn token(api_address: String, email: String, password: String) -> ResponseToken {
    let client = reqwest::blocking::Client::new();

    let resp = client
        .post(&format!("{}/user/token", api_address))
        .json(&PostToken { email, password })
        .send()
        .unwrap();

    resp.json().unwrap()
}

#[derive(Serialize)]
pub struct PostRegister {
    pub username: String,
    pub email: String,
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ResultRegister {
    pub id: String,
    pub username: String,
    pub email: String,
}

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

pub fn register(
    api_address: String,
    username: String,
    email: String,
    password: String,
) -> ResponseRegister {
    let client = reqwest::blocking::Client::new();

    let resp = client
        .post(&format!("{}/user/register", api_address))
        .json(&PostRegister {
            username,
            email,
            password,
        })
        .send()
        .unwrap();

    resp.json().unwrap()
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ResultUserInfoP {
    pub id: String,
    pub username: String,
    pub email: String,
}

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

pub fn userinfop(api_address: String, token: String, user_id: String) -> ResponseUserInfoP {
    let client = reqwest::blocking::Client::new();

    let resp = client
        .get(&format!("{}/user/_/{}", api_address, user_id))
        .header("Authorization", token)
        .send()
        .unwrap();

    resp.json().unwrap()
}