From 80b2d099784f99dfd4f6950eb793be3ef17c9ec7 Mon Sep 17 00:00:00 2001 From: jakka Date: Wed, 3 Sep 2025 22:46:20 +0300 Subject: continued adding api --- Cargo.lock | 21 +++++++++++ Cargo.toml | 1 + src/client.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 126 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c91696..bad92f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,6 +181,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", + "thiserror", "ureq", "urlencoding", ] @@ -551,6 +552,26 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror" +version = "2.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3467d614147380f2e4e374161426ff399c91084acd2363eaf549172b3d5e60c0" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c5e1be1c48b9172ee610da68fd9cd2770e7a4056cb3fc98710ee6906f0c7960" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.43" diff --git a/Cargo.toml b/Cargo.toml index 5f95c92..6637db2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2024" serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.143" serde_repr = "0.1.20" +thiserror = "2.0.16" ureq = { version = "3.1.0", features = ["json"] } urlencoding = "2.1.3" diff --git a/src/client.rs b/src/client.rs index 39f7d4b..a10c15c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,13 +1,19 @@ -use std::error::Error; - use serde::Deserialize; use serde_json::json; use serde_repr::{Deserialize_repr, Serialize_repr}; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum HydrusError { + #[error("failed to connect to Hydrus")] + Unavaliable(ureq::Error), +} -type Result = std::result::Result>; +type Result = std::result::Result; pub struct HydrusClient { apikey: Option, + sessionkey: Option, url: String, } @@ -30,33 +36,90 @@ pub enum HydrusPermissions { SeeLocalPaths, } +#[derive(Deserialize)] +struct AccessKey { + access_key: String, +} + #[derive(Deserialize)] struct SessionKey { session_key: String, } +#[derive(Deserialize)] +pub struct KeyInfo { + name: String, + permits_everything: bool, + basic_permissions: Vec, + human_permissions: String, +} + +#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] +#[repr(u8)] +pub enum ServiceType { + TagRepository = 0, + FileRepository, + LocalFileDomain, + LocalTagDomain = 5, + NumericalRating, + BoolRating, + AllKnownTags = 10, + AllKnownFiles, + LocalBooru, + IPFS, + Trash, + AllLocalFiles, + FileNotes = 17, + ClientAPI, + DeletedFromAnywhere, + LocalUpdates, + AllMyFiles, + IncDecRating, + ServerAdmin = 99, +} + +#[derive(Deserialize)] +pub struct Service { + name: String, + servicetype: ServiceType, + type_pretty: String, +} + +#[derive(Deserialize)] +pub struct GetService { + name: String, + service_key: String, + servicetype: ServiceType, + type_pretty: String, +} + impl HydrusClient { pub fn new(url: String) -> HydrusClient { - return HydrusClient { + HydrusClient { apikey: None, - url: url, - }; + sessionkey: None, + url, + } } pub fn set_api_key(&mut self, key: String) { self.apikey = Some(key) } + pub fn set_session_key(&mut self, key: String) { + self.sessionkey = Some(key) + } + pub fn request_new_permissions( &self, name: String, permissions: &[HydrusPermissions], - ) -> Result { + ) -> Result { let mut req_url = self.url.to_owned(); req_url.push_str("/request_new_permissions?name="); req_url.push_str(&name); - if permissions.len() == 0 { + if permissions.is_empty() { req_url.push_str("&permit_everything=true"); } else { req_url.push_str("&basic_permissions="); @@ -68,8 +131,39 @@ impl HydrusClient { let respose = ureq::get(req_url) .call()? .body_mut() - .read_json::()?; + .read_json::()?; + + Ok(respose.access_key) + } + + pub fn get_session_key(&self) -> Result { + let mut req_url = self.url.to_owned(); + req_url.push_str("/session_key"); + + let mut request = ureq::get(req_url); + + if let Some(key) = &self.apikey { + request = request.header("Hydrus-Client-API-Access-Key", key); + } + + let respose = request.call()?.body_mut().read_json::()?; - return Ok(respose.session_key); + Ok(respose.session_key) + } + + pub fn verify_access_key(&self, key: String) -> Result { + let mut req_url = self.url.to_owned(); + req_url.push_str("/verify_access_key"); + ureq::get(req_url) + .header("Hydrus-Client-API-Access-Key", key) + .call()? + .body_mut() + .read_json::() + } + + pub fn get_service_name(&self, name: String) -> Result { + let mut req_url = self.url.to_owned(); + req_url.push_str("/get_service?service_name="); + req_url.push_str(&name); } } -- cgit v1.3.1