Simplify api helper and add login route

This commit is contained in:
Bnyro 2023-06-19 08:43:26 +02:00
parent 46a183a52c
commit 5bd3491b00
9 changed files with 45 additions and 34 deletions

10
src/api/auth.rs Normal file
View File

@ -0,0 +1,10 @@
use lemmy_api_common::{person::{LoginResponse, Login}, sensitive::Sensitive};
pub fn login(username_or_email: String, password: String) -> std::result::Result<LoginResponse, reqwest::Error> {
let params = Login {
username_or_email: Sensitive::new(username_or_email),
password: Sensitive::new(password)
};
super::get("/user/login", &params)
}

View File

@ -1,7 +1,5 @@
use lemmy_api_common::{community::{ListCommunities, ListCommunitiesResponse}, lemmy_db_schema::{SortType, SearchType}, lemmy_db_views_actor::structs::CommunityView}; use lemmy_api_common::{community::{ListCommunities, ListCommunitiesResponse}, lemmy_db_schema::{SortType, SearchType}, lemmy_db_views_actor::structs::CommunityView};
use crate::components::CLIENT;
use super::search; use super::search;
pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Result<Vec<CommunityView>, reqwest::Error> { pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Result<Vec<CommunityView>, reqwest::Error> {
@ -12,8 +10,7 @@ pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Resul
..Default::default() ..Default::default()
}; };
let url = format!("{}/community/list", super::get_api_url()); Ok(super::get::<ListCommunitiesResponse, _>("/community/list", &params)?.communities)
Ok(CLIENT.get(&url).query(&params).send()?.json::<ListCommunitiesResponse>()?.communities)
} else { } else {
Ok(search::fetch_search(page, query.unwrap(), Some(SearchType::Communities))?.communities) Ok(search::fetch_search(page, query.unwrap(), Some(SearchType::Communities))?.communities)
} }

View File

@ -1,15 +1,12 @@
use lemmy_api_common::community::{GetCommunity, GetCommunityResponse}; use lemmy_api_common::community::{GetCommunity, GetCommunityResponse};
use crate::components::CLIENT;
pub fn get_community(name: String) -> std::result::Result<GetCommunityResponse, reqwest::Error> { pub fn get_community(name: String) -> std::result::Result<GetCommunityResponse, reqwest::Error> {
let params = GetCommunity { let params = GetCommunity {
name: Some(name), name: Some(name),
..Default::default() ..Default::default()
}; };
let url = format!("{}/community", super::get_api_url()); super::get("/community", &params)
CLIENT.get(&url).query(&params).send()?.json()
} }
pub fn default_community() -> GetCommunityResponse { pub fn default_community() -> GetCommunityResponse {

View File

@ -1,3 +1,5 @@
use serde::{de::DeserializeOwned, Serialize};
use crate::settings::get_prefs; use crate::settings::get_prefs;
pub mod communities; pub mod communities;
@ -6,9 +8,33 @@ pub mod post;
pub mod posts; pub mod posts;
pub mod search; pub mod search;
pub mod user; pub mod user;
pub mod auth;
static API_VERSION: &str = "v3"; static API_VERSION: &str = "v3";
pub fn get_api_url() -> String { use reqwest::blocking::Client;
use relm4::once_cell::sync::Lazy;
pub static CLIENT: Lazy<Client> = Lazy::new(|| {
Client::new()
});
fn get_api_url() -> String {
format!("{}/api/{}", get_prefs().instance_url, API_VERSION).to_string() format!("{}/api/{}", get_prefs().instance_url, API_VERSION).to_string()
} }
fn get_url(path: &str) -> String {
format!("{}{}", get_api_url(), path)
}
fn get<T, Params>(path: &str, params: &Params) -> Result<T, reqwest::Error>
where
T: DeserializeOwned,
Params: Serialize + std::fmt::Debug,
{
CLIENT
.get(&get_url(path))
.query(&params)
.send()?
.json()
}

View File

@ -1,15 +1,12 @@
use lemmy_api_common::{post::{GetPost, GetPostResponse}, lemmy_db_schema::{newtypes::PostId, CommentSortType, ListingType}, comment::{GetComments, GetCommentsResponse}, lemmy_db_views::structs::CommentView}; use lemmy_api_common::{post::{GetPost, GetPostResponse}, lemmy_db_schema::{newtypes::PostId, CommentSortType, ListingType}, comment::{GetComments, GetCommentsResponse}, lemmy_db_views::structs::CommentView};
use crate::components::CLIENT;
pub fn get_post(id: PostId) -> std::result::Result<GetPostResponse, reqwest::Error> { pub fn get_post(id: PostId) -> std::result::Result<GetPostResponse, reqwest::Error> {
let params = GetPost { let params = GetPost {
id: Some(id), id: Some(id),
..Default::default() ..Default::default()
}; };
let url = format!("{}/post", super::get_api_url()); super::get("/post", &params)
CLIENT.get(&url).query(&params).send()?.json()
} }
pub fn get_comments(post_id: PostId) -> std::result::Result<Vec<CommentView>, reqwest::Error> { pub fn get_comments(post_id: PostId) -> std::result::Result<Vec<CommentView>, reqwest::Error> {
@ -20,10 +17,9 @@ pub fn get_comments(post_id: PostId) -> std::result::Result<Vec<CommentView>, re
..Default::default() ..Default::default()
}; };
let url = format!("{}/comment/list", super::get_api_url()); let mut comments = super::get::<GetCommentsResponse, _>("/comment/list", &params)?.comments;
let mut comments = CLIENT.get(&url).query(&params).send()?.json::<GetCommentsResponse>()?.comments;
// hide removed comments // hide removed and deleted comments
comments.retain(|c| !c.comment.deleted && !c.comment.removed); comments.retain(|c| !c.comment.deleted && !c.comment.removed);
Ok(comments) Ok(comments)

View File

@ -1,7 +1,5 @@
use lemmy_api_common::{post::{GetPostsResponse, GetPosts}, lemmy_db_views::structs::PostView}; use lemmy_api_common::{post::{GetPostsResponse, GetPosts}, lemmy_db_views::structs::PostView};
use crate::components::CLIENT;
pub fn list_posts(page: i64, community_name: Option<String>) -> std::result::Result<Vec<PostView>, reqwest::Error> { pub fn list_posts(page: i64, community_name: Option<String>) -> std::result::Result<Vec<PostView>, reqwest::Error> {
let params = GetPosts { let params = GetPosts {
page: Some(page), page: Some(page),
@ -9,6 +7,5 @@ pub fn list_posts(page: i64, community_name: Option<String>) -> std::result::Res
..Default::default() ..Default::default()
}; };
let url = format!("{}/post/list", super::get_api_url()); Ok(super::get::<GetPostsResponse, _>("/post/list", &params)?.posts)
Ok(CLIENT.get(&url).query(&params).send()?.json::<GetPostsResponse>()?.posts)
} }

View File

@ -1,7 +1,5 @@
use lemmy_api_common::{site::{SearchResponse, Search}, lemmy_db_schema::{SortType, SearchType}}; use lemmy_api_common::{site::{SearchResponse, Search}, lemmy_db_schema::{SortType, SearchType}};
use crate::components::CLIENT;
pub fn fetch_search(page: i64, query: String, search_type: Option<SearchType>) -> std::result::Result<SearchResponse, reqwest::Error> { pub fn fetch_search(page: i64, query: String, search_type: Option<SearchType>) -> std::result::Result<SearchResponse, reqwest::Error> {
let params = Search { let params = Search {
q: query, q: query,
@ -11,6 +9,5 @@ pub fn fetch_search(page: i64, query: String, search_type: Option<SearchType>) -
..Default::default() ..Default::default()
}; };
let url = format!("{}/search", super::get_api_url()); super::get("/search", &params)
CLIENT.get(&url).query(&params).send()?.json()
} }

View File

@ -1,5 +1,4 @@
use lemmy_api_common::{person::{GetPersonDetailsResponse, GetPersonDetails}}; use lemmy_api_common::{person::{GetPersonDetailsResponse, GetPersonDetails}};
use crate::components::CLIENT;
pub fn get_user(username: String, page: i64) -> std::result::Result<GetPersonDetailsResponse, reqwest::Error> { pub fn get_user(username: String, page: i64) -> std::result::Result<GetPersonDetailsResponse, reqwest::Error> {
let params = GetPersonDetails { let params = GetPersonDetails {
@ -8,8 +7,7 @@ pub fn get_user(username: String, page: i64) -> std::result::Result<GetPersonDet
..Default::default() ..Default::default()
}; };
let url = format!("{}/user", super::get_api_url()); super::get("/user", &params)
CLIENT.get(&url).query(&params).send()?.json()
} }
pub fn default_person() -> GetPersonDetailsResponse { pub fn default_person() -> GetPersonDetailsResponse {

View File

@ -4,10 +4,3 @@ pub mod profile_page;
pub mod community_page; pub mod community_page;
pub mod post_page; pub mod post_page;
pub mod comment_row; pub mod comment_row;
use reqwest::blocking::Client;
use relm4::once_cell::sync::Lazy;
pub static CLIENT: Lazy<Client> = Lazy::new(|| {
Client::new()
});