Added Error handling for dropped connections
This commit is contained in:
parent
cf82a4535b
commit
813f661ee2
1 changed files with 41 additions and 17 deletions
56
src/main.rs
56
src/main.rs
|
@ -44,36 +44,53 @@ impl Bot {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn login(&mut self) {
|
||||
/// Get JWT Token
|
||||
///
|
||||
/// * `return` : Returns true if token was succesfully retrieved, false otherwise
|
||||
pub(crate) fn login(&mut self) -> bool {
|
||||
let login_params = Login {
|
||||
username_or_email: self.secrets.lemmy.get_username(),
|
||||
password: self.secrets.lemmy.get_password(),
|
||||
totp_2fa_token: None,
|
||||
};
|
||||
|
||||
let res = CLIENT
|
||||
let res = match CLIENT
|
||||
.post(self.config.instance.clone() + "/api/v3/user/login")
|
||||
.json(&login_params)
|
||||
.send()
|
||||
.unwrap();
|
||||
.send() {
|
||||
Ok(data) => data,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
|
||||
if res.status() == StatusCode::OK {
|
||||
let data: &LoginResponse = &res.json().unwrap();
|
||||
|
||||
let jwt = data.jwt.clone().expect("JWT Token could not be acquired");
|
||||
self.auth = jwt;
|
||||
return true;
|
||||
} else {
|
||||
println!("Error Code: {:?}", res.status());
|
||||
panic!("JWT Token could not be acquired");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn post(&mut self, post_data: CreatePost) {
|
||||
let res = CLIENT
|
||||
/// Make Post to Lemmy Instance
|
||||
///
|
||||
/// * `post_data` : Object of type [CreatePost] containing post info
|
||||
/// * `return` : Returns true if Post was succesful, false otherwise
|
||||
pub(crate) fn post(&mut self, post_data: CreatePost) -> bool {
|
||||
let res = match CLIENT
|
||||
.post(self.config.instance.clone() + "/api/v3/post")
|
||||
.json(&post_data)
|
||||
.send()
|
||||
.unwrap();
|
||||
.send() {
|
||||
Ok(data) => data,
|
||||
Err(_) => return false
|
||||
};
|
||||
|
||||
// TODO: process res to get info about if post was successfuly (mostly if jwt token was valid)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pub(crate) fn run_once(&mut self, mut prev_time: NaiveTime) {
|
||||
|
@ -91,7 +108,10 @@ impl Bot {
|
|||
|
||||
post_queue.iter().for_each(|post| {
|
||||
println!("Posting: {}", post.name);
|
||||
self.post(post.clone());
|
||||
loop {
|
||||
if self.post(post.clone()) {break};
|
||||
println!("Post attempt failed, retrying");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -99,7 +119,10 @@ impl Bot {
|
|||
while Utc::now().time() - self.start_time.time() < chrono::Duration::seconds(30) {
|
||||
sleep(time::Duration::from_secs(10));
|
||||
}
|
||||
let _ = reqwest::blocking::get("https://status.neshweb.net/api/push/7s1CjPPzrV?status=up&msg=OK&ping=");
|
||||
match reqwest::blocking::get("https://status.neshweb.net/api/push/7s1CjPPzrV?status=up&msg=OK&ping=") {
|
||||
Ok(_) => {},
|
||||
Err(err) => println!("{}", err)
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) fn print_info(&self) {
|
||||
|
@ -136,10 +159,10 @@ fn list_posts(auth: &Sensitive<String>, base: String) -> GetPostsResponse {
|
|||
}
|
||||
|
||||
fn run_bot() {
|
||||
// Get all needed auth tokens at the start
|
||||
let mut old = Utc::now().time();
|
||||
let mut this = Bot::new();
|
||||
this.login();
|
||||
// Get all needed auth tokens at the start
|
||||
let mut old = Utc::now().time();
|
||||
let mut this = Bot::new();
|
||||
if this.login() {
|
||||
this.community_ids.load(&this.auth, &this.config.instance);
|
||||
|
||||
// Enter a loop (not for debugging)
|
||||
|
@ -148,7 +171,8 @@ fn run_bot() {
|
|||
this.run_once(old);
|
||||
this.print_info();
|
||||
this.idle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() -> Result<(), io::Error> {
|
||||
run_bot();
|
||||
|
|
Loading…
Reference in a new issue