Add support for notes api

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/13/head
trivernis 3 years ago
parent a0eea94340
commit d7a047ea90
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package] [package]
name = "hydrus-api" name = "hydrus-api"
version = "0.7.0" version = "0.7.1"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
license = "Apache-2.0" license = "Apache-2.0"

@ -3,3 +3,4 @@ pub mod or_chain_builder;
pub mod search_builder; pub mod search_builder;
pub mod tag_builder; pub mod tag_builder;
pub mod tagging_builder; pub mod tagging_builder;
pub mod notes_builder;

@ -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<String, String>,
}
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<S1: ToString, S2: ToString>(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<I: IntoIterator<Item = (S1, S2)>, 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
}
}

@ -2,6 +2,7 @@ use crate::api_core::adding_tags::{AddTagsRequestBuilder, TagAction};
use crate::api_core::common::{FileIdentifier, FileMetadataInfo, FileRecord, ServiceIdentifier}; use crate::api_core::common::{FileIdentifier, FileMetadataInfo, FileRecord, ServiceIdentifier};
use crate::error::{Error, Result}; use crate::error::{Error, Result};
use crate::utils::tag_list_to_string_list; use crate::utils::tag_list_to_string_list;
use crate::wrapper::builders::notes_builder::AddNotesBuilder;
use crate::wrapper::service::ServiceName; use crate::wrapper::service::ServiceName;
use crate::wrapper::tag::Tag; use crate::wrapper::tag::Tag;
use crate::Client; use crate::Client;
@ -318,6 +319,27 @@ impl HydrusFile {
self.client.add_tags(reqwest.build()).await 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<S1: ToString>(&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<I: IntoIterator<Item = S>, 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 /// Retrieves the file record bytes
pub async fn retrieve(&self) -> Result<FileRecord> { pub async fn retrieve(&self) -> Result<FileRecord> {
self.client.get_file(self.id.clone()).await self.client.get_file(self.id.clone()).await

@ -73,6 +73,23 @@ async fn it_modifies_tags() {
.unwrap(); .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] #[tokio::test]
async fn it_retrieves_content() { async fn it_retrieves_content() {
let file = get_file().await; let file = get_file().await;

Loading…
Cancel
Save