1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-26 08:31:09 +00:00
ibis/src/frontend/api.rs

69 lines
2.2 KiB
Rust
Raw Normal View History

2024-01-03 16:06:52 +00:00
use crate::common::GetArticleData;
2024-01-16 15:07:01 +00:00
use crate::common::LocalUserView;
use crate::common::{ArticleView, LoginUserData, RegisterUserData};
use crate::frontend::error::MyResult;
2024-01-03 16:06:52 +00:00
use anyhow::anyhow;
use once_cell::sync::Lazy;
use reqwest::{Client, RequestBuilder};
use serde::{Deserialize, Serialize};
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 {
Ok(serde_json::from_str(&text).map_err(|e| anyhow!("Json error on {text}: {e}"))?)
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-16 15:07:01 +00:00
pub async fn register(hostname: &str, register_form: RegisterUserData) -> MyResult<LocalUserView> {
2024-01-11 15:21:44 +00:00
let req = CLIENT
2024-01-16 15:07:01 +00:00
.post(format!("http://{}/api/v1/account/register", hostname))
2024-01-11 15:21:44 +00:00
.form(&register_form);
2024-01-16 15:07:01 +00:00
handle_json_res::<LocalUserView>(req).await
2024-01-11 15:21:44 +00:00
}
2024-01-16 15:07:01 +00:00
pub async fn login(hostname: &str, login_form: LoginUserData) -> MyResult<LocalUserView> {
2024-01-11 15:21:44 +00:00
let req = CLIENT
2024-01-16 15:07:01 +00:00
.post(format!("http://{}/api/v1/account/login", hostname))
2024-01-11 15:21:44 +00:00
.form(&login_form);
2024-01-16 15:07:01 +00:00
handle_json_res::<LocalUserView>(req).await
}
pub async fn my_profile(hostname: &str) -> MyResult<LocalUserView> {
let req = CLIENT.get(format!("http://{}/api/v1/account/my_profile", hostname));
handle_json_res::<LocalUserView>(req).await
}
pub async fn logout(hostname: &str) -> MyResult<()> {
CLIENT
.get(format!("http://{}/api/v1/account/logout", hostname))
.send()
.await?;
Ok(())
2024-01-11 15:21:44 +00:00
}