From ff505e98ae1cb954d7388b98681379789f04f712 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 | 16 ++++++++++------ 3 files changed, 16 insertions(+), 9 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..92851a1 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,8 +111,8 @@ impl ItsApi { event!(Level::INFO, "sending spaceping"); self.client - .post(self.base_url.join("ping.php")?) - .query(&[("apikey", &self.spaceping_token)]) + .post(self.spaceping_url.clone()) + .form(&[("apikey", &self.spaceping_token)]) .send() .await .map_err(Error::Network)?