From c8d7053b87089055060a393b35c2abdb3124aede Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 6 May 2024 20:53:50 +0200 Subject: [PATCH] De-duplicate Code --- src/lemmy.rs | 176 ++++++++++++++------------------------------------- 1 file changed, 49 insertions(+), 127 deletions(-) diff --git a/src/lemmy.rs b/src/lemmy.rs index df6d61a..5d250e7 100644 --- a/src/lemmy.rs +++ b/src/lemmy.rs @@ -9,6 +9,7 @@ use lemmy_db_schema::newtypes::{CommunityId, PostId}; use lemmy_db_schema::{ListingType, PostFeatureType}; use reqwest::StatusCode; use std::collections::HashMap; +use serde::{Deserialize, Serialize}; use url::Url; use systemd_journal_logger::connected_to_journal; @@ -88,68 +89,21 @@ pub(crate) async fn login(config: &Config) -> Result { impl Lemmy { pub(crate) async fn post(&self, post: CreatePost) -> Result { - let response = match HTTP_CLIENT - .post(format!("{}/api/v3/post", &self.instance)) - .bearer_auth(&self.jwt_token.to_string()) - .json(&post) - .send() - .await - { - Ok(data) => match data.text().await { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let response = self.fetch_data_json("/api/v3/post", &post).await?; - let json_data = match serde_json::from_str::>(&response) { - Ok(mut data) => data.remove("post_view").expect("Element should be present"), - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let json_data: PostView = self.parse_json(&response).await?; Ok(json_data.post.id) } async fn feature(&self, params: FeaturePost) -> Result { - let response = match HTTP_CLIENT - .post(format!("{}/api/v3/post/feature", &self.instance)) - .bearer_auth(&self.jwt_token.to_string()) - .json(¶ms) - .send() - .await - { - Ok(data) => match data.text().await { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let response = self.fetch_data_json("/api/v3/post/feature", ¶ms).await?; let json_data = match serde_json::from_str::>(&response) { Ok(mut data) => data.remove("post_view").expect("Element should be present"), Err(e) => { let err_msg = format!("{e}"); - write_error(err_msg); + error!(err_msg); return Err(()); } }; @@ -182,36 +136,9 @@ impl Lemmy { ..Default::default() }; - let response = match HTTP_CLIENT - .get(format!("{}/api/v3/post/list", &self.instance)) - .bearer_auth(&self.jwt_token.to_string()) - .query(&list_params) - .send() - .await - { - Ok(data) => match data.text().await { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let response = self.fetch_data_query("/api/v3/post/list", &list_params).await?; - let json_data: GetPostsResponse = match serde_json::from_str(&response) { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let json_data: GetPostsResponse = self.parse_json(&response).await?; Ok(json_data .posts @@ -227,36 +154,9 @@ impl Lemmy { ..Default::default() }; - let response = match HTTP_CLIENT - .get(format!("{}/api/v3/post/list", &self.instance)) - .bearer_auth(&self.jwt_token.to_string()) - .query(&list_params) - .send() - .await - { - Ok(data) => match data.text().await { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let response = self.fetch_data_query("/api/v3/post/list", &list_params).await?; - let json_data: GetPostsResponse = match serde_json::from_str(&response) { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()); - } - }; + let json_data: GetPostsResponse = self.parse_json(&response).await?; Ok(json_data .posts @@ -272,15 +172,43 @@ impl Lemmy { ..Default::default() }; - let response = match HTTP_CLIENT - .get(format!("{}/api/v3/community/list", &self.instance)) + let response = self.fetch_data_query("/api/v3/community/list", &list_params).await?; + + let json_data: ListCommunitiesResponse = self.parse_json(&response).await?; + + let mut communities: HashMap = HashMap::new(); + for community_view in json_data.communities { + let community = community_view.community; + communities.insert(community.name, community.id); + } + + Ok(communities) + } + + async fn fetch_data_json(&self, route: &str, json: &T ) -> Result { + let res = HTTP_CLIENT + .post(format!("{}{route}", self.instance)) .bearer_auth(&self.jwt_token.to_string()) - .query(&list_params) + .json(&json) .send() - .await - { + .await; + self.extract_data(res).await + } + + async fn fetch_data_query(&self, route: &str, json: &T ) -> Result { + let res = HTTP_CLIENT + .post(format!("{}{route}", self.instance)) + .bearer_auth(&self.jwt_token.to_string()) + .query(&json) + .send() + .await; + self.extract_data(res).await + } + + async fn extract_data(&self, response: Result) -> Result { + match response { Ok(data) => match data.text().await { - Ok(data) => data, + Ok(data) => Ok(data), Err(e) => { let err_msg = format!("{e}"); error!(err_msg); @@ -292,23 +220,17 @@ impl Lemmy { error!(err_msg); Err(()) } - }; - - let json_data: ListCommunitiesResponse = match serde_json::from_str(&response) { - Ok(data) => data, + } + } + + async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result { + match serde_json::from_str::>(response) { + Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")), Err(e) => { let err_msg = format!("{e}"); error!(err_msg); Err(()) } - }; - - let mut communities: HashMap = HashMap::new(); - for community_view in json_data.communities { - let community = community_view.community; - communities.insert(community.name, community.id); } - - Ok(communities) } }