Further Bugfixing
This commit is contained in:
parent
534a8022a9
commit
2dc695577e
1 changed files with 13 additions and 2 deletions
15
src/lemmy.rs
15
src/lemmy.rs
|
@ -90,14 +90,14 @@ pub(crate) async fn login(config: &Config) -> Result<Lemmy, ()> {
|
|||
impl Lemmy {
|
||||
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
|
||||
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)
|
||||
}
|
||||
|
||||
async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
|
||||
let response: String = self.post_data_json("/api/v3/post/feature", ¶ms).await?;
|
||||
let json_data: PostView = self.parse_json(&response).await?;
|
||||
let json_data: PostView = self.parse_json_map(&response).await?;
|
||||
|
||||
Ok(json_data)
|
||||
}
|
||||
|
@ -212,6 +212,17 @@ impl Lemmy {
|
|||
}
|
||||
|
||||
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) {
|
||||
Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")),
|
||||
Err(e) => {
|
||||
|
|
Loading…
Reference in a new issue