From 123574b3f7d3fb07c3fdda246228db3b8be1c983 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 31 Jul 2021 13:19:53 +0200 Subject: [PATCH] Add high level function to assign files to a page Signed-off-by: trivernis --- src/wrapper/page.rs | 29 +++++++++++++++++++++++++++-- tests/wrapper/test_page.rs | 12 ++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/wrapper/page.rs b/src/wrapper/page.rs index 1338a49..93669a2 100644 --- a/src/wrapper/page.rs +++ b/src/wrapper/page.rs @@ -1,4 +1,4 @@ -use crate::api_core::common::PageInformation; +use crate::api_core::common::{FileIdentifier, PageInformation}; use crate::error::Result; use crate::Client; @@ -32,11 +32,36 @@ impl HydrusPage { pub async fn focus(&self) -> Result<()> { self.client.focus_page(&self.key).await } - + /// Returns an identifier of the page pub fn id(&self) -> PageIdentifier { PageIdentifier::key(&self.key) } + + /// Adds files to a page + pub async fn add_files(&self, files: Vec) -> Result<()> { + let mut hashes = Vec::new(); + let mut ids = Vec::new(); + + for file in files { + match file { + FileIdentifier::ID(id) => ids.push(id), + FileIdentifier::Hash(hash) => hashes.push(hash), + } + } + // resolve file ids to hashes + if ids.len() > 0 && hashes.len() > 0 { + while let Some(id) = ids.pop() { + let metadata = self + .client + .get_file_metadata_by_identifier(FileIdentifier::ID(id)) + .await?; + hashes.push(metadata.hash); + } + } + + self.client.add_files_to_page(&self.key, ids, hashes).await + } } #[derive(Clone)] diff --git a/tests/wrapper/test_page.rs b/tests/wrapper/test_page.rs index 9bcf39a..666c44f 100644 --- a/tests/wrapper/test_page.rs +++ b/tests/wrapper/test_page.rs @@ -1,4 +1,5 @@ use super::super::common; +use hydrus_api::api_core::common::FileIdentifier; use hydrus_api::wrapper::page::HydrusPage; async fn get_page() -> HydrusPage { @@ -30,3 +31,14 @@ async fn it_has_a_id() { let page = get_page().await; page.id(); } + +#[tokio::test] +async fn it_can_have_files_assigned() { + let page = get_page().await; + let result = page + .add_files(vec![FileIdentifier::hash( + "0000000000000000000000000000000000000000000000000000000000000000", + )]) + .await; + assert!(result.is_err()) // root pages are not media pages +}