Compare commits

..

2 commits

Author SHA1 Message Date
4fcbc6fec7 its_api: patch spaceapi response for homeassistant breakage 2024-01-23 20:32:45 +00:00
9b2978365c Add serde_json 2024-01-23 20:32:08 +00:00
3 changed files with 120 additions and 75 deletions

139
Cargo.lock generated
View file

@ -172,9 +172,9 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -183,9 +183,9 @@ version = "0.1.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a564d521dd56509c4c47480d00b80ee55f7e385ae48db5744c67ad50c92d2ebf"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -422,9 +422,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050"
dependencies = [
"heck",
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -594,8 +594,8 @@ checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
dependencies = [
"fnv",
"ident_case",
"proc-macro2 1.0.66",
"quote 1.0.31",
"proc-macro2 1.0.78",
"quote 1.0.35",
"strsim",
"syn 1.0.109",
]
@ -607,7 +607,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [
"darling_core",
"quote 1.0.31",
"quote 1.0.35",
"syn 1.0.109",
]
@ -649,8 +649,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f91d4cfa921f1c05904dc3c57b4a32c38aed3340cce209f3a6fd1478babafc4"
dependencies = [
"darling",
"proc-macro2 1.0.66",
"quote 1.0.31",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 1.0.109",
]
@ -696,9 +696,9 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -899,9 +899,9 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -1287,6 +1287,7 @@ dependencies = [
"never-say-never",
"reqwest",
"serde",
"serde_json",
"spaceapi",
"thiserror",
"time",
@ -1705,9 +1706,9 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -1832,9 +1833,9 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec2e072ecce94ec471b13398d5402c188e76ac03cf74dd1a975161b23a3f6d9c"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -1903,9 +1904,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.66"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [
"unicode-ident",
]
@ -1928,8 +1929,8 @@ checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4"
dependencies = [
"anyhow",
"itertools",
"proc-macro2 1.0.66",
"quote 1.0.31",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 1.0.109",
]
@ -1944,11 +1945,11 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.31"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2 1.0.66",
"proc-macro2 1.0.78",
]
[[package]]
@ -2327,8 +2328,8 @@ checksum = "0f82e91eb61cd86d9287303133ee55b54618eccb75a522cc22a42c15f5bda340"
dependencies = [
"once_cell",
"proc-macro-crate",
"proc-macro2 1.0.66",
"quote 1.0.31",
"proc-macro2 1.0.78",
"quote 1.0.35",
"ruma-identifiers-validation",
"serde",
"syn 1.0.109",
@ -2414,9 +2415,9 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.171"
version = "1.0.195"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9"
checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02"
dependencies = [
"serde_derive",
]
@ -2432,20 +2433,20 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.171"
version = "1.0.195"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682"
checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
name = "serde_json"
version = "1.0.103"
version = "1.0.111"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d03b412469450d4404fe8499a268edd7f8b79fecb074b0d812ad64ca21f4031b"
checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4"
dependencies = [
"itoa",
"ryu",
@ -2612,19 +2613,19 @@ version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"proc-macro2 1.0.78",
"quote 1.0.35",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.26"
version = "2.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970"
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"proc-macro2 1.0.78",
"quote 1.0.35",
"unicode-ident",
]
@ -2657,9 +2658,9 @@ version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -2742,9 +2743,9 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -2838,9 +2839,9 @@ version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]
[[package]]
@ -3072,9 +3073,9 @@ dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
"wasm-bindgen-shared",
]
@ -3096,7 +3097,7 @@ version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [
"quote 1.0.31",
"quote 1.0.35",
"wasm-bindgen-macro-support",
]
@ -3106,9 +3107,9 @@ version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -3292,7 +3293,7 @@ version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [
"proc-macro2 1.0.66",
"quote 1.0.31",
"syn 2.0.26",
"proc-macro2 1.0.78",
"quote 1.0.35",
"syn 2.0.48",
]

View file

@ -29,3 +29,4 @@ color-eyre = "0.6.2"
tracing = "0.1.37"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
serde_json = "1.0.111"

View file

@ -1,4 +1,5 @@
use reqwest::{Client, Url};
use serde::Deserialize;
use thiserror::Error;
use tracing::{event, instrument, Level};
@ -6,8 +7,10 @@ use tracing::{event, instrument, Level};
pub enum Error {
#[error("invalid URL")]
InvalidUrl(#[from] url::ParseError),
#[error("invalid response")]
InvalidResponse(#[source] reqwest::Error),
#[error("error retrieving API response")]
ResponseBody(#[source] reqwest::Error),
#[error("invalid JSON in response")]
InvalidJson(#[source] serde_json::Error),
#[error("network request failed")]
Network(#[source] reqwest::Error),
}
@ -20,6 +23,41 @@ pub struct ItsApi {
client: Client,
}
fn response_get_lastchange(value: &mut serde_json::Value) -> Option<&mut serde_json::Value> {
value
.as_object_mut()?
.get_mut("state")?
.as_object_mut()?
.get_mut("lastchange")
}
fn response_get_icon(
value: &mut serde_json::Value,
) -> Option<&mut serde_json::Map<String, serde_json::Value>> {
value
.as_object_mut()?
.get_mut("state")?
.as_object_mut()?
.get_mut("icon")?
.as_object_mut()
}
fn patch_response(value: &mut serde_json::Value) {
// https://github.com/home-assistant/core/pull/83871
if let Some(lastchange) = response_get_lastchange(value) {
if let Some(f) = lastchange.as_f64() {
*lastchange = (f as u64).into();
}
}
// https://github.com/home-assistant/core/pull/108596
if let Some(icon) = response_get_icon(value) {
if let Some(closed) = icon.remove("close") {
icon.insert("closed".to_owned(), closed);
}
}
}
impl ItsApi {
/// Constructs a new IT-Syndikat API handler.
#[instrument]
@ -40,15 +78,20 @@ impl ItsApi {
#[instrument]
pub async fn status(&self) -> Result<spaceapi::Status, Error> {
event!(Level::DEBUG, "requesting spaceapi status");
let status = self
let response = self
.client
.get(self.base_url.join("status.php")?)
.send()
.await
.map_err(Error::Network)?
.json()
.bytes()
.await
.map_err(Error::InvalidResponse)?;
.map_err(Error::ResponseBody)?;
let mut response = serde_json::from_slice(&response).map_err(Error::InvalidJson)?;
patch_response(&mut response);
let status = spaceapi::Status::deserialize(response).map_err(Error::InvalidJson)?;
event!(Level::DEBUG, ?status);
Ok(status)
@ -70,7 +113,7 @@ impl ItsApi {
.await
.map_err(Error::Network)?
.error_for_status()
.map_err(Error::InvalidResponse)?;
.map_err(Error::ResponseBody)?;
Ok(())
}