DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

85f88d2f9bd31a91f928a7fcb77c509b8ff59c87 — Jonni Liljamo 2 years ago d0c1835
APIError struct and login/register API calls
2 files changed, 90 insertions(+), 0 deletions(-)

M sdbclient/src/api/mod.rs
A sdbclient/src/api/user/mod.rs
M sdbclient/src/api/mod.rs => sdbclient/src/api/mod.rs +7 -0
@@ 9,6 9,13 @@
use reqwest;
use serde::{Deserialize, Serialize};

pub mod user;

#[derive(Deserialize)]
pub struct APIError {
    pub error: String,
}

#[derive(Serialize, Deserialize)]
pub struct APIInfo {
    pub info: String,

A sdbclient/src/api/user/mod.rs => sdbclient/src/api/user/mod.rs +83 -0
@@ 0,0 1,83 @@
/*
 * 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};

#[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 { error: String },
    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 { error: String },
    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()
}