/*
* 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()
}