Add search functionality to hydrus wrapper

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 3 years ago
parent 43c5ac47ee
commit a73e7de532
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

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

@ -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<Tag>,
) -> Result<Vec<HydrusFile>> {
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)
}
}

@ -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<S: ToString>(
client: Client,
status: u8,

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

Loading…
Cancel
Save