Add functions to add and modify tags to the file struct

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

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

@ -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<Tag>) -> 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<Tag>,
) -> 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
}
}

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

Loading…
Cancel
Save