diff --git a/Cargo.toml b/Cargo.toml index 4ea2c91..a638bf6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydrus-api" -version = "0.7.0" +version = "0.7.1" authors = ["trivernis "] edition = "2018" license = "Apache-2.0" diff --git a/src/wrapper/builders/mod.rs b/src/wrapper/builders/mod.rs index 5e65902..a58dde9 100644 --- a/src/wrapper/builders/mod.rs +++ b/src/wrapper/builders/mod.rs @@ -3,3 +3,4 @@ pub mod or_chain_builder; pub mod search_builder; pub mod tag_builder; pub mod tagging_builder; +pub mod notes_builder; diff --git a/src/wrapper/builders/notes_builder.rs b/src/wrapper/builders/notes_builder.rs new file mode 100644 index 0000000..72511ea --- /dev/null +++ b/src/wrapper/builders/notes_builder.rs @@ -0,0 +1,47 @@ +use crate::api_core::common::FileIdentifier; +use crate::error::Result; +use crate::Client; +use std::collections::HashMap; + +/// Builder to create a request for adding notes to a given file +pub struct AddNotesBuilder { + client: Client, + file: FileIdentifier, + notes: HashMap, +} + +impl AddNotesBuilder { + /// Creates a new notes builder for the given file id + pub fn new(client: Client, file: FileIdentifier) -> Self { + Self { + client, + file, + notes: HashMap::new(), + } + } + + /// Adds a single note + pub fn add_note(mut self, name: S1, note: S2) -> Self { + self.notes.insert(name.to_string(), note.to_string()); + + self + } + + /// Adds multiple notes to the builder + pub fn add_notes, S1: ToString, S2: ToString>( + mut self, + notes: I, + ) -> Self { + let notes_iter = notes + .into_iter() + .map(|(k, v): (S1, S2)| (k.to_string(), v.to_string())); + self.notes.extend(notes_iter); + + self + } + + /// Adds all notes mentioned in the builder to the given file + pub async fn run(self) -> Result<()> { + self.client.set_notes(self.file, self.notes).await + } +} diff --git a/src/wrapper/hydrus_file.rs b/src/wrapper/hydrus_file.rs index 60c5cb8..70e2b4c 100644 --- a/src/wrapper/hydrus_file.rs +++ b/src/wrapper/hydrus_file.rs @@ -2,6 +2,7 @@ use crate::api_core::adding_tags::{AddTagsRequestBuilder, TagAction}; use crate::api_core::common::{FileIdentifier, FileMetadataInfo, FileRecord, ServiceIdentifier}; use crate::error::{Error, Result}; use crate::utils::tag_list_to_string_list; +use crate::wrapper::builders::notes_builder::AddNotesBuilder; use crate::wrapper::service::ServiceName; use crate::wrapper::tag::Tag; use crate::Client; @@ -318,6 +319,27 @@ impl HydrusFile { self.client.add_tags(reqwest.build()).await } + /// Creates a builder to add notes to the file + pub fn add_notes(&self) -> AddNotesBuilder { + AddNotesBuilder::new(self.client.clone(), self.id.clone()) + } + + /// Deletes a single note from the file + pub async fn delete_note(&self, name: S1) -> Result<()> { + self.client + .delete_notes(self.id.clone(), vec![name.to_string()]) + .await + } + + /// Deletes multiple notes from the file + pub async fn delete_notes, S: ToString>( + &self, + names: I, + ) -> Result<()> { + let names = names.into_iter().map(|n: S| n.to_string()).collect(); + self.client.delete_notes(self.id.clone(), names).await + } + /// Retrieves the file record bytes pub async fn retrieve(&self) -> Result { self.client.get_file(self.id.clone()).await diff --git a/tests/wrapper/test_files.rs b/tests/wrapper/test_files.rs index f464526..536ff4a 100644 --- a/tests/wrapper/test_files.rs +++ b/tests/wrapper/test_files.rs @@ -73,6 +73,23 @@ async fn it_modifies_tags() { .unwrap(); } +#[tokio::test] +async fn it_adds_notes() { + let file = get_file().await; + file.add_notes() + .add_note("My Note", "My notes content") + .add_notes(vec![("My note 2", "More content")]) + .run() + .await + .unwrap(); +} + +#[tokio::test] +async fn it_deletes_notes() { + let file = get_file().await; + file.delete_note("My Note").await.unwrap(); +} + #[tokio::test] async fn it_retrieves_content() { let file = get_file().await;