/*
* This file is part of laurelin/shared
* Copyright (C) 2023 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;
pub mod types;
#[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)]
#[serde(untagged)]
pub enum ResponseRegister {
Error(APIErrorWrapper),
Valid(types::UserPub),
}
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()
}