2024-01-11 15:21:44 +00:00
|
|
|
use crate::common::{ArticleView, LoginResponse, LoginUserData, RegisterUserData};
|
2024-01-03 16:06:52 +00:00
|
|
|
use crate::common::GetArticleData;
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use reqwest::{Client, RequestBuilder};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-01-11 15:50:57 +00:00
|
|
|
use crate::frontend::error::MyResult;
|
2024-01-03 16:06:52 +00:00
|
|
|
|
|
|
|
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
|
|
|
|
|
2024-01-11 15:50:57 +00:00
|
|
|
pub async fn get_article(hostname: &str, title: String) -> MyResult<ArticleView> {
|
2024-01-03 16:06:52 +00:00
|
|
|
let get_article = GetArticleData { title };
|
|
|
|
get_query::<ArticleView, _>(hostname, "article", Some(get_article.clone())).await
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:50:57 +00:00
|
|
|
pub async fn get_query<T, R>(hostname: &str, endpoint: &str, query: Option<R>) -> MyResult<T>
|
2024-01-03 16:06:52 +00:00
|
|
|
where
|
|
|
|
T: for<'de> Deserialize<'de>,
|
|
|
|
R: Serialize,
|
|
|
|
{
|
|
|
|
let mut req = CLIENT.get(format!("http://{}/api/v1/{}", hostname, endpoint));
|
|
|
|
if let Some(query) = query {
|
|
|
|
req = req.query(&query);
|
|
|
|
}
|
|
|
|
handle_json_res::<T>(req).await
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:50:57 +00:00
|
|
|
pub async fn handle_json_res<T>(req: RequestBuilder) -> MyResult<T>
|
2024-01-03 16:06:52 +00:00
|
|
|
where
|
|
|
|
T: for<'de> Deserialize<'de>,
|
|
|
|
{
|
2024-01-11 15:50:57 +00:00
|
|
|
let res = req.send().await?;
|
2024-01-03 16:06:52 +00:00
|
|
|
let status = res.status();
|
2024-01-11 15:50:57 +00:00
|
|
|
let text = res.text().await?;
|
2024-01-03 16:06:52 +00:00
|
|
|
if status == reqwest::StatusCode::OK {
|
2024-01-11 15:50:57 +00:00
|
|
|
Ok(serde_json::from_str(&text)
|
2024-01-03 16:06:52 +00:00
|
|
|
.map_err(|e| anyhow!("Json error on {text}: {e}"))
|
2024-01-11 15:50:57 +00:00
|
|
|
?)
|
2024-01-03 16:06:52 +00:00
|
|
|
} else {
|
2024-01-11 15:50:57 +00:00
|
|
|
Err(anyhow!("API error: {text}").into())
|
2024-01-03 16:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-11 15:21:44 +00:00
|
|
|
|
2024-01-11 15:50:57 +00:00
|
|
|
pub async fn register(hostname: &str, username: &str, password: &str) -> MyResult<LoginResponse> {
|
2024-01-11 15:21:44 +00:00
|
|
|
let register_form = RegisterUserData {
|
|
|
|
username: username.to_string(),
|
|
|
|
password: password.to_string(),
|
|
|
|
};
|
|
|
|
let req = CLIENT
|
|
|
|
.post(format!("http://{}/api/v1/user/register", hostname))
|
|
|
|
.form(®ister_form);
|
|
|
|
handle_json_res(req).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn login(
|
|
|
|
hostname: &str,
|
|
|
|
username: &str,
|
|
|
|
password: &str,
|
2024-01-11 15:50:57 +00:00
|
|
|
) -> MyResult<LoginResponse> {
|
2024-01-11 15:21:44 +00:00
|
|
|
let login_form = LoginUserData {
|
|
|
|
username: username.to_string(),
|
|
|
|
password: password.to_string(),
|
|
|
|
};
|
|
|
|
let req = CLIENT
|
|
|
|
.post(format!("http://{}/api/v1/user/login", hostname))
|
|
|
|
.form(&login_form);
|
|
|
|
handle_json_res(req).await
|
|
|
|
}
|