Further Bugfixing

This commit is contained in:
Neshura 2024-05-06 21:27:47 +02:00
parent 534a8022a9
commit 2dc695577e
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C

View file

@ -90,14 +90,14 @@ pub(crate) async fn login(config: &Config) -> Result<Lemmy, ()> {
impl Lemmy { impl Lemmy {
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> { pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
let response: String = self.post_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?; let json_data: PostView = self.parse_json_map(&response).await?;
Ok(json_data.post.id) Ok(json_data.post.id)
} }
async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> { async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
let response: String = self.post_data_json("/api/v3/post/feature", &params).await?; let response: String = self.post_data_json("/api/v3/post/feature", &params).await?;
let json_data: PostView = self.parse_json(&response).await?; let json_data: PostView = self.parse_json_map(&response).await?;
Ok(json_data) Ok(json_data)
} }
@ -212,6 +212,17 @@ impl Lemmy {
} }
async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result<T,()> { async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result<T,()> {
match serde_json::from_str::<T>(response) {
Ok(data) => Ok(data),
Err(e) => {
let err_msg = format!("{e}");
error!(err_msg);
Err(())
}
}
}
async fn parse_json_map<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result<T,()> {
match serde_json::from_str::<HashMap<&str, T>>(response) { match serde_json::from_str::<HashMap<&str, T>>(response) {
Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")), Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")),
Err(e) => { Err(e) => {