summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjakka <jakka@jakka.su>2025-09-18 10:55:49 +0300
committerjakka <jakka@jakka.su>2025-09-18 10:55:49 +0300
commitbdc2e645c2d174881f7278139de95b141328431e (patch)
tree4d17de039a625ac740ef24ee6c5e53ceaf8213ad /src
parent770f10da05c2a85f5b6934c9062fd7548b0c1e5b (diff)
added documentation
Diffstat (limited to 'src')
-rw-r--r--src/client.rs16
-rw-r--r--src/lib.rs5
-rw-r--r--src/traits.rs22
-rw-r--r--src/types.rs23
4 files changed, 43 insertions, 23 deletions
diff --git a/src/client.rs b/src/client.rs
index 0b3f1c7..d8c7c40 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -9,6 +9,7 @@ use crate::{traits::*, types::*};
type Result<T> = std::result::Result<T, HydrusError>;
+/// hydrus client
pub struct HydrusClient {
client: reqwest::Client,
apikey: Option<String>,
@@ -17,6 +18,7 @@ pub struct HydrusClient {
}
impl HydrusClient {
+ /// create a new hydurs client object. requires a url to hydrus API endpoint
pub fn new(url: &str) -> HydrusClient {
HydrusClient {
client: reqwest::Client::new(),
@@ -25,11 +27,11 @@ impl HydrusClient {
url: url.to_string(),
}
}
-
+ /// set an api key
pub fn set_api_key(&mut self, key: &str) {
self.apikey = Some(key.to_owned())
}
-
+ /// set a session key
pub fn set_session_key(&mut self, key: &str) {
self.sessionkey = Some(key.to_owned())
}
@@ -182,10 +184,10 @@ impl AccessManagement for HydrusClient {
impl ImportingAndDeletingFiles for HydrusClient {
async fn add_file_via_path(
&self,
- path: &str,
+ path: PathBuf,
delete: Option<bool>,
domains: Option<FileDomain>,
- ) -> Result<FileAddResponse> {
+ ) -> Result<AddFileResponse> {
let mut form = AddFileRequest {
path: path.to_owned(),
delete_after_successful_import: delete,
@@ -212,11 +214,11 @@ impl ImportingAndDeletingFiles for HydrusClient {
.json(&form)
.send()
.await?
- .json::<FileAddResponse>()
+ .json::<AddFileResponse>()
.await?)
}
- async fn add_file_via_file(&self, file: PathBuf) -> Result<FileAddResponse> {
+ async fn add_file_via_file(&self, file: PathBuf) -> Result<AddFileResponse> {
let mut req_url = self.url.to_owned();
req_url.push_str("add_files/add_file");
@@ -231,7 +233,7 @@ impl ImportingAndDeletingFiles for HydrusClient {
.body(body)
.send()
.await?
- .json::<FileAddResponse>()
+ .json::<AddFileResponse>()
.await?)
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 6d5ae50..3d70b70 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,10 @@
+//! crate with hydrus client and traits necessary for accesing hydrus API
+
+/// this crates hydrus client implementation
pub mod client;
+/// traits for accessing hydrus API
pub mod traits;
+/// various objects for de/serializing requests
pub mod types;
#[cfg(test)]
diff --git a/src/traits.rs b/src/traits.rs
index d8f68f2..d3390a5 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -5,33 +5,37 @@ use async_trait::async_trait;
type Result<T> = std::result::Result<T, HydrusError>;
+/// trait for accessing and managing keys and services
#[async_trait]
pub trait AccessManagement {
+ /// request new api key
async fn request_new_permissions(
&self,
name: &str,
permissions: &[HydrusPermissions],
) -> Result<String>;
-
+ /// request new session key
async fn get_session_key(&self) -> Result<String>;
-
+ /// verify session or api key
async fn verify_access_key(&self, key: &str) -> Result<KeyInfo>;
-
+ /// get service info by providing service name
async fn get_service_name(&self, name: &str) -> Result<Service>;
-
+ /// get service info by providing service key
async fn get_service_key(&self, key: &str) -> Result<Service>;
-
+ /// get all service info
async fn get_services(&self) -> Result<HashMap<String, Service>>;
}
+/// trait for importing and deleting files
#[async_trait]
pub trait ImportingAndDeletingFiles {
+ /// import file into hydrus by providing a local (hydrus-local) file path
async fn add_file_via_path(
&self,
- path: &str,
+ path: PathBuf,
delete: Option<bool>,
domains: Option<FileDomain>,
- ) -> Result<FileAddResponse>;
-
- async fn add_file_via_file(&self, file: PathBuf) -> Result<FileAddResponse>;
+ ) -> Result<AddFileResponse>;
+ /// import file into hydrus by sending the file
+ async fn add_file_via_file(&self, file: PathBuf) -> Result<AddFileResponse>;
}
diff --git a/src/types.rs b/src/types.rs
index e5e0606..39bb3b7 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,7 +1,10 @@
+use std::path::PathBuf;
+
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use thiserror::Error;
+/// various errors
#[derive(Error, Debug)]
pub enum HydrusError {
#[error("failed to connect to Hydrus")]
@@ -32,6 +35,7 @@ impl From<reqwest::Error> for HydrusError {
}
}
+/// hydrus permissions
#[derive(PartialEq, Debug, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum HydrusPermissions {
@@ -52,6 +56,7 @@ pub enum HydrusPermissions {
Null = 255,
}
+/// hydrus key information struct
#[derive(Deserialize, Debug)]
pub struct KeyInfo {
pub name: String,
@@ -60,6 +65,7 @@ pub struct KeyInfo {
pub human_permissions: String,
}
+/// hydrus service type struct
#[derive(PartialEq, Debug, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ServiceType {
@@ -85,6 +91,7 @@ pub enum ServiceType {
Null = 255,
}
+/// hydrus service struct
#[derive(Deserialize, Debug)]
pub struct Service {
pub name: String,
@@ -100,6 +107,7 @@ pub struct Service {
pub max_stars: Option<u8>,
}
+/// hydrus file domains
pub enum FileDomain {
FileServiceKey(String),
FileServiceKeys(Vec<String>),
@@ -107,9 +115,10 @@ pub enum FileDomain {
DeletedFileServiceKeys(Vec<String>),
}
+/// payload for importing a file via providing a local path
#[derive(Serialize, Debug, Default)]
pub struct AddFileRequest {
- pub path: String,
+ pub path: PathBuf,
#[serde(skip)]
pub delete_after_successful_import: Option<bool>,
#[serde(skip)]
@@ -121,24 +130,24 @@ pub struct AddFileRequest {
#[serde(skip)]
pub deleted_file_service_keys: Option<Vec<String>>,
}
-
+/// file importing status
#[derive(PartialEq, Debug, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
-pub enum FileAddStatus {
+pub enum AddFileStatus {
SuccessfulImport = 1,
AlreadyInDatabase,
PreviouslyDeleted,
FailedToImport,
FileVetoed = 7,
}
-
+/// file importing api response
#[derive(Deserialize)]
-pub struct FileAddResponse {
- pub status: FileAddStatus,
+pub struct AddFileResponse {
+ pub status: AddFileStatus,
pub hash: String,
pub note: String,
}
-
+/// payload for deleting a file
#[derive(Serialize, Debug, Default)]
pub struct DeleteFileRequest {
pub path: String,