From 2b5599b821da2e415947387c92b0d9c130d4e8c9 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 27 Feb 2022 13:49:24 +0100 Subject: [PATCH] Add support for returning file hashes on search Signed-off-by: trivernis --- src/api_core/client.rs | 20 ++++++++++++++++--- src/api_core/searching_and_fetching_files.rs | 16 +++++++++++++++ .../test_searching_and_fetching_files.rs | 19 ++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/api_core/client.rs b/src/api_core/client.rs index cab5721..5c65472 100644 --- a/src/api_core/client.rs +++ b/src/api_core/client.rs @@ -22,8 +22,8 @@ use crate::api_core::managing_pages::{ GetPages, GetPagesResponse, }; use crate::api_core::searching_and_fetching_files::{ - FileMetadata, FileMetadataResponse, FileSearchOptions, GetFile, SearchFiles, - SearchFilesResponse, SearchQueryEntry, + FileMetadata, FileMetadataResponse, FileSearchOptions, GetFile, SearchFileHashes, + SearchFileHashesResponse, SearchFiles, SearchFilesResponse, SearchQueryEntry, }; use crate::api_core::Endpoint; use crate::error::{Error, Result}; @@ -241,7 +241,7 @@ impl Client { Ok(()) } - /// Searches for files in the inbox, the archive or both + /// Searches for files #[tracing::instrument(skip(self), level = "debug")] pub async fn search_files( &self, @@ -254,6 +254,20 @@ impl Client { .await } + /// Searches for file hashes + #[tracing::instrument(skip(self), level = "debug")] + pub async fn search_file_hashes( + &self, + query: Vec, + options: FileSearchOptions, + ) -> Result { + let mut args = options.into_query_args(); + args.push(("tags", search_query_list_to_json_array(query))); + args.push(("return_hashes", String::from("true"))); + self.get_and_parse::(&args) + .await + } + /// Returns the metadata for a given list of file_ids or hashes #[tracing::instrument(skip(self), level = "debug")] pub async fn get_file_metadata( diff --git a/src/api_core/searching_and_fetching_files.rs b/src/api_core/searching_and_fetching_files.rs index a7a56e6..9899d99 100644 --- a/src/api_core/searching_and_fetching_files.rs +++ b/src/api_core/searching_and_fetching_files.rs @@ -112,6 +112,22 @@ impl Endpoint for SearchFiles { } } +#[derive(Clone, Debug, Deserialize)] +pub struct SearchFileHashesResponse { + pub hashes: Vec, +} + +pub struct SearchFileHashes; + +impl Endpoint for SearchFileHashes { + type Request = (); + type Response = SearchFileHashesResponse; + + fn path() -> String { + String::from("get_files/search_files") + } +} + #[derive(Clone, Debug, Default, Deserialize)] pub struct FileMetadataResponse { pub metadata: Vec, diff --git a/tests/client/test_searching_and_fetching_files.rs b/tests/client/test_searching_and_fetching_files.rs index 0a1eed0..7d42220 100644 --- a/tests/client/test_searching_and_fetching_files.rs +++ b/tests/client/test_searching_and_fetching_files.rs @@ -22,6 +22,25 @@ async fn is_searches_files() { .unwrap(); } +#[tokio::test] +async fn is_searches_file_hashes() { + let client = common::get_client(); + let options = FileSearchOptions::new() + .sort_type(SORT_FILE_PIXEL_COUNT) + .tag_service_name("my tags") + .file_service_name("all known files"); + client + .search_file_hashes( + vec![ + "beach".into(), + SearchQueryEntry::OrChain(vec!["summer".to_string(), "winter".to_string()]), + ], + options, + ) + .await + .unwrap(); +} + #[tokio::test] async fn it_fetches_file_metadata() { let client = common::get_client();