From 9285dd6839ee15fd1426b1014ebef39cec07ab68 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 14 Sep 2025 11:06:38 +0800 Subject: httpconnector: impl ntfy --- src/client/connector/connectors/httpconnector.rs | 39 ++++++++++++++++++------ 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/client/connector') diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs index e391771..a671b94 100644 --- a/src/client/connector/connectors/httpconnector.rs +++ b/src/client/connector/connectors/httpconnector.rs @@ -4,17 +4,18 @@ use crate::connector::{self, *}; use std::{thread::sleep, time::Duration}; pub(crate) struct HTTPConnector { - url: Url, + pull_url: Url, + ntfy_url: Url, client: Client, prev: ::Ok, } impl HTTPConnector { - pub fn new(url: &str) -> std::result::Result { + pub fn new(pull_url: &str, ntfy_url: &str) -> std::result::Result { Ok(Self { prev: String::new(), - url: Url::parse(url)?, - // TODO: keep alive + pull_url: Url::parse(pull_url)?, + ntfy_url: Url::parse(ntfy_url)?, client: Client::new(), }) } @@ -37,8 +38,9 @@ impl std::fmt::Display for Err { impl Connection for HTTPConnector { type Err = Err; + // consider StatusCode::NOT_MODIFIED fn update(&self) -> Result { - let resp = match self.client.get(self.url.as_str()).send() { + Result::Ok(match self.client.get(self.pull_url.as_str()).send() { Ok(resp) => match resp.status() { StatusCode::TOO_MANY_REQUESTS => { sleep(Duration::from_secs(5)); @@ -55,13 +57,30 @@ impl Connection for HTTPConnector { } }, Err(e) => return Result::Err(Err::Reqwest(e)), - }; - - Result::Ok(resp) + }) } - fn ntfy(&self, data: String) { - println!("{}", data) + fn ntfy(&self, data: String) -> Result { + Result::Ok( + match self.client.post(self.ntfy_url.as_str()).body(data).send() { + Ok(resp) => match resp.status() { + StatusCode::TOO_MANY_REQUESTS => { + sleep(Duration::from_secs(5)); + return self.update(); + } + status => { + if !status.is_success() { + return Result::Err(Err::StatusCode(status)); + } + match resp.text() { + Ok(text) => text, + Err(e) => return Result::Err(Err::Reqwest(e)), + } + } + }, + Err(e) => return Result::Err(Err::Reqwest(e)), + }, + ) } fn is_prev(&mut self, str: &Self::Ok) -> bool { -- cgit v1.2.3