diff --git a/src/client.rs b/src/client.rs index c232e40..ed6c1f5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -27,9 +27,17 @@ impl Client { } } + /// Returns the options of the PTR #[tracing::instrument(skip(self), level = "debug")] pub async fn options(&self) -> Result { - self.get::(&()).await + self.get::(&()).await + } + + /// Returns information about all available updates since the given ID + /// and when the next check for updates should be made + #[tracing::instrument(skip(self), level = "debug")] + pub async fn metadata(&self, since: u64) -> Result { + self.get::(&[("since", since)]).await } /// Performs a get request to the given Get Endpoint diff --git a/src/endpoints/metadata.rs b/src/endpoints/metadata.rs new file mode 100644 index 0000000..a06ac41 --- /dev/null +++ b/src/endpoints/metadata.rs @@ -0,0 +1,34 @@ +use crate::hydrus_serializable::dictionary::HydrusDictionary; +use crate::hydrus_serializable::metadata::HydrusMetadata; +use crate::hydrus_serializable::wrapper::HydrusSerWrapper; +use crate::{Endpoint, FromJson, GetEndpoint}; +use serde_json::Value; + +pub struct MetadataEndpoint; + +impl Endpoint for MetadataEndpoint { + fn path() -> &'static str { + "metadata" + } +} + +impl GetEndpoint for MetadataEndpoint { + type Response = MetadataResponse; +} + +#[derive(Clone, Debug)] +pub struct MetadataResponse(pub HydrusMetadata); + +impl FromJson for MetadataResponse { + fn from_json(value: Value) -> crate::Result + where + Self: Sized, + { + let mut dict = HydrusDictionary::from_json(value)?; + let metadata = dict + .take_by_str::>("metadata_slice")? + .inner; + + Ok(MetadataResponse(metadata)) + } +} diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs index 607f24d..f036562 100644 --- a/src/endpoints/mod.rs +++ b/src/endpoints/mod.rs @@ -1,8 +1,10 @@ +mod metadata; mod options; use crate::Result; use std::fmt::Debug; +pub use metadata::*; pub use options::*; pub trait Endpoint { diff --git a/src/endpoints/options.rs b/src/endpoints/options.rs index d4b12ec..506ade3 100644 --- a/src/endpoints/options.rs +++ b/src/endpoints/options.rs @@ -5,15 +5,15 @@ use crate::Result; use crate::{Endpoint, FromJson, GetEndpoint}; use serde_json::Value; -pub struct Options; +pub struct OptionsEndpoint; -impl Endpoint for Options { +impl Endpoint for OptionsEndpoint { fn path() -> &'static str { "options" } } -impl GetEndpoint for Options { +impl GetEndpoint for OptionsEndpoint { type Response = OptionsResponse; } diff --git a/src/hydrus_serializable/metadata.rs b/src/hydrus_serializable/metadata.rs new file mode 100644 index 0000000..c5a3499 --- /dev/null +++ b/src/hydrus_serializable/metadata.rs @@ -0,0 +1,22 @@ +use crate::hydrus_serializable::HydrusSerializable; +use serde::Deserialize; + +#[derive(Clone, Debug, Deserialize)] +pub struct HydrusMetadata { + pub entries: Vec, + pub next_update_due: u64, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct MetadataEntry { + pub update_index: u64, + pub update_hashes: Vec, + pub time_begin: u64, + pub time_end: u64, +} + +impl HydrusSerializable for HydrusMetadata { + fn type_id() -> u64 { + 37 + } +} diff --git a/src/hydrus_serializable/mod.rs b/src/hydrus_serializable/mod.rs index 26db458..7e0b881 100644 --- a/src/hydrus_serializable/mod.rs +++ b/src/hydrus_serializable/mod.rs @@ -7,6 +7,7 @@ use std::fmt::Formatter; use std::marker::PhantomData; pub mod dictionary; +pub mod metadata; pub mod tag_filter; pub mod wrapper; diff --git a/tests/endpoints.rs b/tests/endpoints.rs index 2a76f14..a6f0a45 100644 --- a/tests/endpoints.rs +++ b/tests/endpoints.rs @@ -5,3 +5,9 @@ async fn test_options() { let client = common::get_client(); client.options().await.unwrap(); } + +#[tokio::test] +async fn test_metadata() { + let client = common::get_client(); + client.metadata(0).await.unwrap(); +}