From a73e7de5326b18dfa050611c425b7bf32a99fb76 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 11 Jul 2021 18:11:57 +0200 Subject: [PATCH] Add search functionality to hydrus wrapper Signed-off-by: trivernis --- src/endpoints/searching_and_fetching_files.rs | 11 +--------- src/models/hydrus.rs | 22 +++++++++++++++++++ src/models/hydrus_file.rs | 9 ++++++++ tests/wrapper/test_hydrus.rs | 13 +++++++++++ 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/endpoints/searching_and_fetching_files.rs b/src/endpoints/searching_and_fetching_files.rs index 5739587..3a46d66 100644 --- a/src/endpoints/searching_and_fetching_files.rs +++ b/src/endpoints/searching_and_fetching_files.rs @@ -7,7 +7,6 @@ pub struct SearchFilesResponse { } pub enum FileSearchLocation { - All, Inbox, Archive, } @@ -16,14 +15,6 @@ impl FileSearchLocation { pub fn is_inbox(&self) -> bool { if let &Self::Inbox = &self { true - } else { - self.is_all() - } - } - - pub fn is_all(&self) -> bool { - if let &Self::All = &self { - true } else { false } @@ -33,7 +24,7 @@ impl FileSearchLocation { if let &Self::Archive = &self { true } else { - self.is_all() + false } } } diff --git a/src/models/hydrus.rs b/src/models/hydrus.rs index bfc1fe0..038c136 100644 --- a/src/models/hydrus.rs +++ b/src/models/hydrus.rs @@ -1,10 +1,13 @@ use crate::builders::import_builder::ImportBuilder; use crate::endpoints::common::FileIdentifier; +use crate::endpoints::searching_and_fetching_files::FileSearchLocation; use crate::error::Result; use crate::hydrus_file::HydrusFile; use crate::models::url::Url; use crate::models::version::Version; use crate::service::Services; +use crate::tag::Tag; +use crate::utils::tag_list_to_string_list; use crate::Client; pub struct Hydrus { @@ -64,4 +67,23 @@ impl Hydrus { Ok(HydrusFile::from_metadata(self.client.clone(), metadata)) } + + /// Searches for files that have the given tags and returns a list of hydrus files as a result + pub async fn search( + &self, + location: FileSearchLocation, + tags: Vec, + ) -> Result> { + let search_result = self + .client + .search_files(tag_list_to_string_list(tags), location) + .await?; + let files = search_result + .file_ids + .into_iter() + .map(|id| HydrusFile::from_id(self.client.clone(), id)) + .collect(); + + Ok(files) + } } diff --git a/src/models/hydrus_file.rs b/src/models/hydrus_file.rs index 1a2a733..e128c8e 100644 --- a/src/models/hydrus_file.rs +++ b/src/models/hydrus_file.rs @@ -26,6 +26,15 @@ pub struct HydrusFile { } impl HydrusFile { + pub(crate) fn from_id(client: Client, id: u64) -> Self { + Self { + client, + id: FileIdentifier::ID(id), + status: FileStatus::Unknown, + metadata: None, + } + } + pub(crate) fn from_raw_status_and_hash( client: Client, status: u8, diff --git a/tests/wrapper/test_hydrus.rs b/tests/wrapper/test_hydrus.rs index 5e04b21..7ce11c9 100644 --- a/tests/wrapper/test_hydrus.rs +++ b/tests/wrapper/test_hydrus.rs @@ -1,4 +1,5 @@ use super::super::common; +use hydrus_api::endpoints::searching_and_fetching_files::FileSearchLocation; use hydrus_api::service::ServiceType; use hydrus_api::url::UrlType; @@ -30,3 +31,15 @@ async fn it_retrieves_url_information() { assert_eq!(url.url_type, UrlType::Post) } + +#[tokio::test] +async fn it_searches() { + let hydrus = common::get_hydrus(); + hydrus + .search( + FileSearchLocation::Archive, + vec!["character:megumin".into()], + ) + .await + .unwrap(); +}