Working Error Logging

This commit is contained in:
Neshura 2023-12-17 22:29:18 +01:00
parent bba7fae8b9
commit 47e6cc59c0
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
7 changed files with 253 additions and 123 deletions
src/lemmy

View file

@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::env;
use std::env::VarError;
use std::error::Error;
use lemmy_api_common::community::{ListCommunities};
use lemmy_api_common::person::{Login, LoginResponse};
use lemmy_api_common::post::{GetPosts};
@ -42,18 +41,21 @@ pub(crate) struct Lemmy {
instance: String,
}
pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result<Lemmy, Box<dyn Error>> {
pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result<Lemmy, String> {
let login_params = Login {
username_or_email: credentials.get_username(),
password: credentials.get_password(),
totp_2fa_token: None,
};
let response = HTTP_CLIENT
let response = match HTTP_CLIENT
.post(instance.to_string() + "/api/v3/user/login")
.json(&login_params)
.send()
.await?;
.await {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
};
match response.status() {
StatusCode::OK => {
@ -71,40 +73,55 @@ pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result<L
}
impl Lemmy {
pub(crate) async fn post(&self, post: CustomCreatePost) -> Result<PostId, Box<dyn Error>> {
let response = HTTP_CLIENT
pub(crate) async fn post(&self, post: CustomCreatePost) -> Result<PostId, String> {
let response = match HTTP_CLIENT
.post(format!("{}/api/v3/post", &self.instance))
.bearer_auth(&self.jwt_token.to_string())
.json(&post)
.send()
.await?
.text()
.await?;
.await {
Ok(data) => {
match data.text().await {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
}
},
Err(e) => return Err(format!("{}", e))
};
let json_data = serde_json::from_str::<HashMap<&str, CustomPostView>>(&response)?
.remove("post_view")
.expect("Element should be present");
let json_data = match serde_json::from_str::<HashMap<&str, CustomPostView>>(&response) {
Ok(mut data) => data.remove("post_view").expect("Element should be present"),
Err(e) => return Err(format!("{}", e))
};
Ok(json_data.post.id)
}
async fn feature(&self, params: CustomFeaturePost) -> Result<CustomPostView, Box<dyn Error>> {
let response = HTTP_CLIENT
async fn feature(&self, params: CustomFeaturePost) -> Result<CustomPostView, String> {
let response = match HTTP_CLIENT
.post(format!("{}/api/v3/post/feature", &self.instance))
.bearer_auth(&self.jwt_token.to_string())
.json(&params)
.send()
.await?
.text()
.await?;
.await {
Ok(data) => {
match data.text().await {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
}
},
Err(e) => return Err(format!("{}", e))
};
let json_data = match serde_json::from_str::<HashMap<&str, CustomPostView>>(&response) {
Ok(mut data) => data.remove("post_view").expect("Element should be present"),
Err(e) => return Err(format!("{}", e))
};
let json_data = serde_json::from_str::<HashMap<&str, CustomPostView>>(&response)?
.remove("post_view")
.expect("Element should be present");
Ok(json_data)
}
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<CustomPostView, Box<dyn Error>> {
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<CustomPostView, String> {
let pin_params = CustomFeaturePost {
post_id,
featured: false,
@ -113,7 +130,7 @@ impl Lemmy {
self.feature(pin_params).await
}
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<CustomPostView, Box<dyn Error>> {
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<CustomPostView, String> {
let pin_params = CustomFeaturePost {
post_id,
featured: true,
@ -122,23 +139,32 @@ impl Lemmy {
self.feature(pin_params).await
}
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<CustomPostView>, Box<dyn Error>> {
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<CustomPostView>, String> {
let list_params = GetPosts {
community_id: Some(community),
type_: Some(ListingType::Local),
..Default::default()
};
let response = HTTP_CLIENT
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?
.text()
.await?;
.await {
Ok(data) => {
match data.text().await {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
}
},
Err(e) => return Err(format!("{}", e))
};
let json_data: CustomGetPostsResponse = serde_json::from_str(&response)?;
let json_data: CustomGetPostsResponse = match serde_json::from_str(&response) {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
};
Ok(json_data.posts.iter().filter(|post| {
post.post.featured_community
@ -148,22 +174,31 @@ impl Lemmy {
)
}
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<CustomPostView>, Box<dyn Error>> {
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<CustomPostView>, String> {
let list_params = GetPosts {
type_: Some(ListingType::Local),
..Default::default()
};
let response = HTTP_CLIENT
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?
.text()
.await?;
.await {
Ok(data) => {
match data.text().await {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
}
},
Err(e) => return Err(format!("{}", e))
};
let json_data: CustomGetPostsResponse = serde_json::from_str(&response)?;
let json_data: CustomGetPostsResponse = match serde_json::from_str(&response) {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
};
Ok(json_data.posts.iter().filter(|post| {
post.post.featured_local
@ -173,22 +208,31 @@ impl Lemmy {
)
}
pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, Box<dyn Error>> {
pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, String> {
let list_params = ListCommunities {
type_: Some(ListingType::Local),
..Default::default()
};
let response = HTTP_CLIENT
let response = match HTTP_CLIENT
.get(format!("{}/api/v3/community/list", &self.instance))
.bearer_auth(&self.jwt_token.to_string())
.query(&list_params)
.send()
.await?
.text()
.await?;
.await {
Ok(data) => {
match data.text().await {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
}
},
Err(e) => return Err(format!("{}", e))
};
let json_data: CustomListCommunitiesResponse = serde_json::from_str(&response)?;
let json_data: CustomListCommunitiesResponse = match serde_json::from_str(&response) {
Ok(data) => data,
Err(e) => return Err(format!("{}", e))
};
let mut communities: HashMap<String, CommunityId> = HashMap::new();
for community_view in json_data.communities {