From 45bfca8cc561bd382612f9a6ad55e166c95abeaf Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 6 May 2024 21:16:53 +0200 Subject: [PATCH] Bugfix due to bad deduplication --- src/lemmy.rs | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/lemmy.rs b/src/lemmy.rs index 5d250e7..e3a1f56 100644 --- a/src/lemmy.rs +++ b/src/lemmy.rs @@ -89,24 +89,15 @@ pub(crate) async fn login(config: &Config) -> Result { impl Lemmy { pub(crate) async fn post(&self, post: CreatePost) -> Result { - let response = self.fetch_data_json("/api/v3/post", &post).await?; - + let response: String = self.post_data_json("/api/v3/post", &post).await?; let json_data: PostView = self.parse_json(&response).await?; Ok(json_data.post.id) } async fn feature(&self, params: FeaturePost) -> Result { - 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}"); - error!(err_msg); - return Err(()); - } - }; + let response: String = self.post_data_json("/api/v3/post/feature", ¶ms).await?; + let json_data: PostView = self.parse_json(&response).await?; Ok(json_data) } @@ -136,8 +127,7 @@ impl Lemmy { ..Default::default() }; - let response = self.fetch_data_query("/api/v3/post/list", &list_params).await?; - + let response: String = self.get_data_query("/api/v3/post/list", &list_params).await?; let json_data: GetPostsResponse = self.parse_json(&response).await?; Ok(json_data @@ -154,8 +144,7 @@ impl Lemmy { ..Default::default() }; - let response = self.fetch_data_query("/api/v3/post/list", &list_params).await?; - + let response: String = self.get_data_query("/api/v3/post/list", &list_params).await?; let json_data: GetPostsResponse = self.parse_json(&response).await?; Ok(json_data @@ -172,8 +161,7 @@ impl Lemmy { ..Default::default() }; - let response = self.fetch_data_query("/api/v3/community/list", &list_params).await?; - + let response: String = self.get_data_query("/api/v3/community/list", &list_params).await?; let json_data: ListCommunitiesResponse = self.parse_json(&response).await?; let mut communities: HashMap = HashMap::new(); @@ -185,9 +173,9 @@ impl Lemmy { Ok(communities) } - async fn fetch_data_json(&self, route: &str, json: &T ) -> Result { + async fn post_data_json(&self, route: &str, json: &T ) -> Result { let res = HTTP_CLIENT - .post(format!("{}{route}", self.instance)) + .post(format!("{}{route}", &self.instance)) .bearer_auth(&self.jwt_token.to_string()) .json(&json) .send() @@ -195,11 +183,11 @@ impl Lemmy { self.extract_data(res).await } - async fn fetch_data_query(&self, route: &str, json: &T ) -> Result { + async fn get_data_query(&self, route: &str, param: &T ) -> Result { let res = HTTP_CLIENT - .post(format!("{}{route}", self.instance)) + .get(format!("{}{route}", &self.instance)) .bearer_auth(&self.jwt_token.to_string()) - .query(&json) + .query(¶m) .send() .await; self.extract_data(res).await