From 5bd3491b00000c1b9a6bd25ae3eca456b5cf6a54 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Jun 2023 08:43:26 +0200 Subject: [PATCH] Simplify api helper and add login route --- src/api/auth.rs | 10 ++++++++++ src/api/communities.rs | 5 +---- src/api/community.rs | 5 +---- src/api/mod.rs | 28 +++++++++++++++++++++++++++- src/api/post.rs | 10 +++------- src/api/posts.rs | 5 +---- src/api/search.rs | 5 +---- src/api/user.rs | 4 +--- src/components/mod.rs | 7 ------- 9 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 src/api/auth.rs diff --git a/src/api/auth.rs b/src/api/auth.rs new file mode 100644 index 0000000..b15c047 --- /dev/null +++ b/src/api/auth.rs @@ -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 { + let params = Login { + username_or_email: Sensitive::new(username_or_email), + password: Sensitive::new(password) + }; + + super::get("/user/login", ¶ms) +} diff --git a/src/api/communities.rs b/src/api/communities.rs index 6560f32..835e79e 100644 --- a/src/api/communities.rs +++ b/src/api/communities.rs @@ -1,7 +1,5 @@ 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; pub fn fetch_communities(page: i64, query: Option) -> std::result::Result, reqwest::Error> { @@ -12,8 +10,7 @@ pub fn fetch_communities(page: i64, query: Option) -> std::result::Resul ..Default::default() }; - let url = format!("{}/community/list", super::get_api_url()); - Ok(CLIENT.get(&url).query(¶ms).send()?.json::()?.communities) + Ok(super::get::("/community/list", ¶ms)?.communities) } else { Ok(search::fetch_search(page, query.unwrap(), Some(SearchType::Communities))?.communities) } diff --git a/src/api/community.rs b/src/api/community.rs index 5d3ef7c..b58936d 100644 --- a/src/api/community.rs +++ b/src/api/community.rs @@ -1,15 +1,12 @@ use lemmy_api_common::community::{GetCommunity, GetCommunityResponse}; -use crate::components::CLIENT; - pub fn get_community(name: String) -> std::result::Result { let params = GetCommunity { name: Some(name), ..Default::default() }; - let url = format!("{}/community", super::get_api_url()); - CLIENT.get(&url).query(¶ms).send()?.json() + super::get("/community", ¶ms) } pub fn default_community() -> GetCommunityResponse { diff --git a/src/api/mod.rs b/src/api/mod.rs index 48b6632..b1afa19 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,3 +1,5 @@ +use serde::{de::DeserializeOwned, Serialize}; + use crate::settings::get_prefs; pub mod communities; @@ -6,9 +8,33 @@ pub mod post; pub mod posts; pub mod search; pub mod user; +pub mod auth; 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 = Lazy::new(|| { + Client::new() +}); + +fn get_api_url() -> String { format!("{}/api/{}", get_prefs().instance_url, API_VERSION).to_string() } + +fn get_url(path: &str) -> String { + format!("{}{}", get_api_url(), path) +} + +fn get(path: &str, params: &Params) -> Result +where + T: DeserializeOwned, + Params: Serialize + std::fmt::Debug, +{ + CLIENT + .get(&get_url(path)) + .query(¶ms) + .send()? + .json() +} diff --git a/src/api/post.rs b/src/api/post.rs index c5043e9..aa98faa 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -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 crate::components::CLIENT; - pub fn get_post(id: PostId) -> std::result::Result { let params = GetPost { id: Some(id), ..Default::default() }; - let url = format!("{}/post", super::get_api_url()); - CLIENT.get(&url).query(¶ms).send()?.json() + super::get("/post", ¶ms) } pub fn get_comments(post_id: PostId) -> std::result::Result, reqwest::Error> { @@ -20,10 +17,9 @@ pub fn get_comments(post_id: PostId) -> std::result::Result, re ..Default::default() }; - let url = format!("{}/comment/list", super::get_api_url()); - let mut comments = CLIENT.get(&url).query(¶ms).send()?.json::()?.comments; + let mut comments = super::get::("/comment/list", ¶ms)?.comments; - // hide removed comments + // hide removed and deleted comments comments.retain(|c| !c.comment.deleted && !c.comment.removed); Ok(comments) diff --git a/src/api/posts.rs b/src/api/posts.rs index b6225a3..eee5019 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -1,7 +1,5 @@ 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) -> std::result::Result, reqwest::Error> { let params = GetPosts { page: Some(page), @@ -9,6 +7,5 @@ pub fn list_posts(page: i64, community_name: Option) -> std::result::Res ..Default::default() }; - let url = format!("{}/post/list", super::get_api_url()); - Ok(CLIENT.get(&url).query(¶ms).send()?.json::()?.posts) + Ok(super::get::("/post/list", ¶ms)?.posts) } diff --git a/src/api/search.rs b/src/api/search.rs index c5a6e69..6a9d045 100644 --- a/src/api/search.rs +++ b/src/api/search.rs @@ -1,7 +1,5 @@ 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) -> std::result::Result { let params = Search { q: query, @@ -11,6 +9,5 @@ pub fn fetch_search(page: i64, query: String, search_type: Option) - ..Default::default() }; - let url = format!("{}/search", super::get_api_url()); - CLIENT.get(&url).query(¶ms).send()?.json() + super::get("/search", ¶ms) } \ No newline at end of file diff --git a/src/api/user.rs b/src/api/user.rs index 1eb0a32..9bccc43 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -1,5 +1,4 @@ use lemmy_api_common::{person::{GetPersonDetailsResponse, GetPersonDetails}}; -use crate::components::CLIENT; pub fn get_user(username: String, page: i64) -> std::result::Result { let params = GetPersonDetails { @@ -8,8 +7,7 @@ pub fn get_user(username: String, page: i64) -> std::result::Result GetPersonDetailsResponse { diff --git a/src/components/mod.rs b/src/components/mod.rs index 78238b4..df591af 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -4,10 +4,3 @@ pub mod profile_page; pub mod community_page; pub mod post_page; pub mod comment_row; - -use reqwest::blocking::Client; -use relm4::once_cell::sync::Lazy; - -pub static CLIENT: Lazy = Lazy::new(|| { - Client::new() -}); \ No newline at end of file