summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-14 11:06:38 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-14 11:06:38 +0800
commit9285dd6839ee15fd1426b1014ebef39cec07ab68 (patch)
tree2bb5284110cff32e9c81ae8234e83cef59d300e6
parent4259ab8d858bcbafe6a6c3f8d3be8979c61d80ac (diff)
httpconnector: impl ntfy
-rw-r--r--src/client/connector.rs2
-rw-r--r--src/client/connector/connectors/httpconnector.rs39
-rw-r--r--src/client/main.rs3
3 files changed, 32 insertions, 12 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs
index 88b1939..51b662a 100644
--- a/src/client/connector.rs
+++ b/src/client/connector.rs
@@ -17,7 +17,7 @@ pub(crate) trait Connection {
// should use channel to notify update
fn update(&self) -> Result<Self::Ok, Self::Err>; // None if no update(content is same)
- fn ntfy(&self, data: String);
+ fn ntfy(&self, data: String) -> Result<Self::Ok, Self::Err>;
fn is_prev(&mut self, str: &Self::Ok) -> bool;
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: <HTTPConnector as connector::Connection>::Ok,
}
impl HTTPConnector {
- pub fn new(url: &str) -> std::result::Result<Self, url::ParseError> {
+ pub fn new(pull_url: &str, ntfy_url: &str) -> std::result::Result<Self, url::ParseError> {
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<Self::Ok, Self::Err> {
- 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<Self::Ok, Self::Err> {
+ 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 {
diff --git a/src/client/main.rs b/src/client/main.rs
index bceaa82..1062ea3 100644
--- a/src/client/main.rs
+++ b/src/client/main.rs
@@ -5,5 +5,6 @@ mod connector;
mod executor;
fn main() {
- ConnectorManager::new(HTTPConnector::new("http://127.0.0.1").unwrap()).enter_loop();
+ let ip = "http://127.0.0.1:8080";
+ ConnectorManager::new(HTTPConnector::new(ip, ip).unwrap()).enter_loop();
}