From 43c5ac47eeba2d307734066bbf18ae40eabb07a0 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 11 Jul 2021 17:28:22 +0200 Subject: [PATCH] Add functions to add and modify tags to the file struct Signed-off-by: trivernis --- src/endpoints/adding_tags.rs | 1 + src/models/hydrus_file.rs | 31 +++++++++++++++++++++++++++++++ tests/wrapper/test_files.rs | 25 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/endpoints/adding_tags.rs b/src/endpoints/adding_tags.rs index c78f396..15357b8 100644 --- a/src/endpoints/adding_tags.rs +++ b/src/endpoints/adding_tags.rs @@ -42,6 +42,7 @@ pub struct AddTagsRequestBuilder { } /// List of actions for a given tag +#[derive(Clone, Debug)] pub enum TagAction { /// Add to a local tag service. AddToLocalService, diff --git a/src/models/hydrus_file.rs b/src/models/hydrus_file.rs index d60a37e..1a2a733 100644 --- a/src/models/hydrus_file.rs +++ b/src/models/hydrus_file.rs @@ -1,7 +1,9 @@ +use crate::endpoints::adding_tags::{AddTagsRequestBuilder, TagAction}; use crate::endpoints::common::{FileIdentifier, FileMetadataInfo}; use crate::error::Result; use crate::service::ServiceName; use crate::tag::Tag; +use crate::utils::tag_list_to_string_list; use crate::Client; use std::collections::HashMap; @@ -137,4 +139,33 @@ impl HydrusFile { Ok(tag_list) } + + /// Adds tags for a specific service to the file + pub async fn add_tags(&mut self, service: ServiceName, tags: Vec) -> Result<()> { + let hash = self.hash().await?; + let request = AddTagsRequestBuilder::default() + .add_hash(hash) + .add_tags(service.0, tag_list_to_string_list(tags)) + .build(); + + self.client.add_tags(request).await + } + + /// Allows modification of tags by using the defined tag actions + pub async fn modify_tags( + &mut self, + service: ServiceName, + action: TagAction, + tags: Vec, + ) -> Result<()> { + let hash = self.hash().await?; + let mut reqwest = AddTagsRequestBuilder::default().add_hash(hash); + + for tag in tags { + reqwest = + reqwest.add_tag_with_action(service.0.clone(), tag.to_string(), action.clone()); + } + + self.client.add_tags(reqwest.build()).await + } } diff --git a/tests/wrapper/test_files.rs b/tests/wrapper/test_files.rs index 48f2c83..73291f6 100644 --- a/tests/wrapper/test_files.rs +++ b/tests/wrapper/test_files.rs @@ -1,6 +1,8 @@ use super::super::common; +use hydrus_api::endpoints::adding_tags::TagAction; use hydrus_api::endpoints::common::FileIdentifier; use hydrus_api::hydrus_file::HydrusFile; +use hydrus_api::service::ServiceName; async fn get_file() -> HydrusFile { let hydrus = common::get_hydrus(); @@ -47,3 +49,26 @@ async fn it_has_tags() { assert!(tags.len() > 0) // test data needs to be prepared this way } + +#[tokio::test] +async fn it_adds_tags() { + let mut file = get_file().await; + file.add_tags( + ServiceName::public_tag_repository(), + vec!["character:megumin".into(), "ark mage".into()], + ) + .await + .unwrap(); +} + +#[tokio::test] +async fn it_modifies_tags() { + let mut file = get_file().await; + file.modify_tags( + ServiceName::public_tag_repository(), + TagAction::RescindPendFromRepository, + vec!["ark mage".into()], + ) + .await + .unwrap(); +}