Add high level function to assign files to a page

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent d576c7c675
commit 123574b3f7
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<FileIdentifier>) -> 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)]

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

Loading…
Cancel
Save