Add low level notes api

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

@ -12,11 +12,11 @@ repository = "https://github.com/trivernis/hydrus-api-rs"
[dependencies] [dependencies]
serde = { version = "1.0.136", features = ["derive"] } serde = { version = "1.0.136", features = ["derive"] }
reqwest = { version = "0.11.9", features = ["json"] } reqwest = { version = "0.11.10", features = ["json"] }
tracing = "0.1.31" tracing = "0.1.32"
mime = "0.3.16" mime = "0.3.16"
chrono = "0.4.19" chrono = "0.4.19"
regex = "1.5.4" regex = "1.5.5"
lazy_static = "1.4.0" lazy_static = "1.4.0"
[dev-dependencies] [dev-dependencies]

@ -0,0 +1,73 @@
use crate::api_core::common::FileIdentifier;
use crate::api_core::Endpoint;
use std::collections::HashMap;
pub struct SetNotes;
impl Endpoint for SetNotes {
type Request = SetNotesRequest;
type Response = ();
fn path() -> String {
String::from("add_notes/set_notes")
}
}
#[derive(Serialize, Clone, Debug, Default)]
pub struct SetNotesRequest {
notes: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
hash: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
file_id: Option<u64>,
}
impl SetNotesRequest {
pub fn new(id: FileIdentifier, notes: HashMap<String, String>) -> Self {
let mut request = Self {
notes,
..Default::default()
};
match id {
FileIdentifier::ID(id) => request.file_id = Some(id),
FileIdentifier::Hash(hash) => request.hash = Some(hash),
}
request
}
}
pub struct DeleteNotes;
impl Endpoint for DeleteNotes {
type Request = DeleteNotesRequest;
type Response = ();
fn path() -> String {
String::from("add_notes/delete_notes")
}
}
#[derive(Serialize, Clone, Debug, Default)]
pub struct DeleteNotesRequest {
note_names: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
hash: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
file_id: Option<u64>,
}
impl DeleteNotesRequest {
pub fn new(id: FileIdentifier, note_names: Vec<String>) -> Self {
let mut request = Self {
note_names,
..Default::default()
};
match id {
FileIdentifier::ID(id) => request.file_id = Some(id),
FileIdentifier::Hash(hash) => request.hash = Some(hash),
}
request
}
}

@ -6,6 +6,7 @@ use crate::api_core::adding_files::{
AddFile, AddFileRequest, AddFileResponse, ArchiveFiles, ArchiveFilesRequest, DeleteFiles, AddFile, AddFileRequest, AddFileResponse, ArchiveFiles, ArchiveFilesRequest, DeleteFiles,
DeleteFilesRequest, UnarchiveFiles, UnarchiveFilesRequest, UndeleteFiles, UndeleteFilesRequest, DeleteFilesRequest, UnarchiveFiles, UnarchiveFilesRequest, UndeleteFiles, UndeleteFilesRequest,
}; };
use crate::api_core::adding_notes::{DeleteNotes, DeleteNotesRequest, SetNotes, SetNotesRequest};
use crate::api_core::adding_tags::{AddTags, AddTagsRequest, CleanTags, CleanTagsResponse}; use crate::api_core::adding_tags::{AddTags, AddTagsRequest, CleanTags, CleanTagsResponse};
use crate::api_core::adding_urls::{ use crate::api_core::adding_urls::{
AddUrl, AddUrlRequest, AddUrlResponse, AssociateUrl, AssociateUrlRequest, GetUrlFiles, AddUrl, AddUrlRequest, AddUrlResponse, AssociateUrl, AssociateUrlRequest, GetUrlFiles,
@ -33,6 +34,7 @@ use crate::utils::{
use reqwest::Response; use reqwest::Response;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::collections::HashMap;
use std::fmt::Debug; use std::fmt::Debug;
static ACCESS_KEY_HEADER: &str = "Hydrus-Client-API-Access-Key"; static ACCESS_KEY_HEADER: &str = "Hydrus-Client-API-Access-Key";
@ -375,6 +377,28 @@ impl Client {
Ok(()) Ok(())
} }
/// Sets the notes for the file
#[tracing::instrument(skip(self), level = "debug")]
pub async fn set_notes(
&self,
id: FileIdentifier,
notes: HashMap<String, String>,
) -> Result<()> {
self.post::<SetNotes>(SetNotesRequest::new(id, notes))
.await?;
Ok(())
}
/// Deletes the notes of a file
#[tracing::instrument(skip(self), level = "debug")]
pub async fn delete_notes(&self, id: FileIdentifier, note_names: Vec<String>) -> Result<()> {
self.post::<DeleteNotes>(DeleteNotesRequest::new(id, note_names))
.await?;
Ok(())
}
/// Returns all pages of the client /// Returns all pages of the client
#[tracing::instrument(skip(self), level = "debug")] #[tracing::instrument(skip(self), level = "debug")]
pub async fn get_pages(&self) -> Result<GetPagesResponse> { pub async fn get_pages(&self) -> Result<GetPagesResponse> {

@ -80,6 +80,22 @@ impl FileIdentifier {
pub fn hash<S: ToString>(hash: S) -> Self { pub fn hash<S: ToString>(hash: S) -> Self {
Self::Hash(hash.to_string()) Self::Hash(hash.to_string())
} }
pub fn as_hash(&self) -> Option<&String> {
if let Self::Hash(h) = &self {
Some(h)
} else {
None
}
}
pub fn as_id(&self) -> Option<u64> {
if let Self::ID(id) = &self {
Some(*id)
} else {
None
}
}
} }
#[derive(Clone)] #[derive(Clone)]

@ -12,6 +12,7 @@ pub mod common;
pub mod managing_cookies_and_http_headers; pub mod managing_cookies_and_http_headers;
pub mod managing_pages; pub mod managing_pages;
pub mod searching_and_fetching_files; pub mod searching_and_fetching_files;
pub mod adding_notes;
pub use searching_and_fetching_files::file_sort_type; pub use searching_and_fetching_files::file_sort_type;

@ -5,3 +5,4 @@ mod test_adding_urls;
mod test_managing_cookies_and_http_headers; mod test_managing_cookies_and_http_headers;
mod test_managing_pages; mod test_managing_pages;
mod test_searching_and_fetching_files; mod test_searching_and_fetching_files;
mod test_deleting_notes;

@ -0,0 +1,27 @@
use super::super::common;
use crate::common::test_data::TEST_HASH_1;
use hydrus_api::api_core::common::FileIdentifier;
use std::collections::HashMap;
#[tokio::test]
async fn it_sets_notes() {
let client = common::get_client();
common::create_testdata(&client).await;
let mut test_notes = HashMap::new();
test_notes.insert("test".to_string(), "value".to_string());
test_notes.insert("test2".to_string(), "value".to_string());
client
.set_notes(FileIdentifier::hash(TEST_HASH_1), test_notes)
.await
.unwrap();
}
#[tokio::test]
async fn it_deletes_notes() {
let client = common::get_client();
client
.delete_notes(FileIdentifier::hash(TEST_HASH_1), vec!["test".to_string()])
.await
.unwrap();
}
Loading…
Cancel
Save