From e1bd5b3593ab50f6e24f994e51085e8ad2ee2e6a Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 26 Jan 2024 19:20:32 +0000 Subject: [PATCH] Support new spaceping API --- config.toml.example | 3 ++- src/bot.rs | 6 ++++-- src/its_api.rs | 14 +++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/config.toml.example b/config.toml.example index 294de10..9fc4de6 100644 --- a/config.toml.example +++ b/config.toml.example @@ -2,6 +2,7 @@ command_prefix = "!" [space] -base_url = "https://spaceapi.it-syndikat.org/api/" +spaceapi_url = "https://spaceapi.it-syndikat.org/api/status.php" +spaceping_url = "https://homeassistant.asozial.it-syndikat.org/api/webhook/spaceping" spaceping_token = "foo" announce_rooms = ["#lobby:it-syndik.at"] diff --git a/src/bot.rs b/src/bot.rs index 5ac2a14..2e7a701 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -40,7 +40,8 @@ pub struct AppConfig { #[derive(Clone, Debug, Deserialize)] pub struct SpaceConfig { - base_url: Url, + spaceapi_url: Url, + spaceping_url: Url, spaceping_token: String, announce_rooms: Vec, } @@ -118,7 +119,8 @@ impl Bot { client.restore_login(session_data.session).await?; let api = ItsApi::new( - config.space.base_url.clone(), + config.space.spaceapi_url.clone(), + config.space.spaceping_url.clone(), config.space.spaceping_token.clone(), ); let open_state = get_open_state(&api).await?; diff --git a/src/its_api.rs b/src/its_api.rs index 2b86dd6..1addef3 100644 --- a/src/its_api.rs +++ b/src/its_api.rs @@ -18,8 +18,11 @@ pub enum Error { /// Handler for the IT-Syndikat API. #[derive(Clone, Debug)] pub struct ItsApi { - base_url: Url, + spaceapi_url: Url, + + spaceping_url: Url, spaceping_token: String, + client: Client, } @@ -61,9 +64,10 @@ fn patch_response(value: &mut serde_json::Value) { impl ItsApi { /// Constructs a new IT-Syndikat API handler. #[instrument] - pub fn new(base_url: Url, spaceping_token: String) -> Self { + pub fn new(spaceapi_url: Url, spaceping_url: Url, spaceping_token: String) -> Self { Self { - base_url, + spaceapi_url, + spaceping_url, spaceping_token, client: Client::new(), } @@ -80,7 +84,7 @@ impl ItsApi { event!(Level::DEBUG, "requesting spaceapi status"); let response = self .client - .get(self.base_url.join("status.php")?) + .get(self.spaceapi_url.clone()) .send() .await .map_err(Error::Network)? @@ -107,7 +111,7 @@ impl ItsApi { event!(Level::INFO, "sending spaceping"); self.client - .post(self.base_url.join("ping.php")?) + .post(self.spaceping_url.clone()) .query(&[("apikey", &self.spaceping_token)]) .send() .await