Add implementation to fetch metadata

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent 5295dc1803
commit 697f7463e7
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -27,9 +27,17 @@ impl Client {
}
}
/// Returns the options of the PTR
#[tracing::instrument(skip(self), level = "debug")]
pub async fn options(&self) -> Result<OptionsResponse> {
self.get::<Options, ()>(&()).await
self.get::<OptionsEndpoint, ()>(&()).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<MetadataResponse> {
self.get::<MetadataEndpoint, _>(&[("since", since)]).await
}
/// Performs a get request to the given Get Endpoint

@ -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<Self>
where
Self: Sized,
{
let mut dict = HydrusDictionary::from_json(value)?;
let metadata = dict
.take_by_str::<HydrusSerWrapper<HydrusMetadata>>("metadata_slice")?
.inner;
Ok(MetadataResponse(metadata))
}
}

@ -1,8 +1,10 @@
mod metadata;
mod options;
use crate::Result;
use std::fmt::Debug;
pub use metadata::*;
pub use options::*;
pub trait Endpoint {

@ -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;
}

@ -0,0 +1,22 @@
use crate::hydrus_serializable::HydrusSerializable;
use serde::Deserialize;
#[derive(Clone, Debug, Deserialize)]
pub struct HydrusMetadata {
pub entries: Vec<MetadataEntry>,
pub next_update_due: u64,
}
#[derive(Clone, Debug, Deserialize)]
pub struct MetadataEntry {
pub update_index: u64,
pub update_hashes: Vec<String>,
pub time_begin: u64,
pub time_end: u64,
}
impl HydrusSerializable for HydrusMetadata {
fn type_id() -> u64 {
37
}
}

@ -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;

@ -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();
}

Loading…
Cancel
Save