summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.rs70
-rw-r--r--src/tests.rs18
-rw-r--r--src/types.rs108
3 files changed, 88 insertions, 108 deletions
diff --git a/src/client.rs b/src/client.rs
index 49d903e..e13750d 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -38,13 +38,11 @@ impl HydrusClient {
req_url.push_str("&permit_everything=true");
} else {
req_url.push_str("&basic_permissions=");
- let json_string = musli::json::to_string(&permissions)?;
+ let json_string = serde_json::to_string(&permissions)?;
req_url.push_str(&urlencoding::encode(&json_string));
};
- let response = ureq::get(req_url).call()?.body_mut().read_to_vec()?;
-
- let key: AccessKey = musli::json::decode(response.as_slice())?;
+ let key: AccessKey = ureq::get(req_url).call()?.body_mut().read_json()?;
Ok(key.access_key)
}
@@ -59,9 +57,7 @@ impl HydrusClient {
request = request.header("Hydrus-Client-API-Access-Key", key);
}
- let response = request.call()?.body_mut().read_to_vec()?;
-
- let key: SessionKey = musli::json::decode(response.as_slice())?;
+ let key: SessionKey = request.call()?.body_mut().read_json()?;
Ok(key.session_key)
}
@@ -69,38 +65,80 @@ impl HydrusClient {
pub fn verify_access_key(&self, key: &str) -> Result<KeyInfo> {
let mut req_url = self.url.to_owned();
req_url.push_str("verify_access_key");
- let response = ureq::get(req_url)
+ let keyinfo: KeyInfo = ureq::get(req_url)
.header("Hydrus-Client-API-Access-Key", key)
.call()?
.body_mut()
- .read_to_vec()?;
-
- let data: KeyInfo = musli::json::decode(response.as_slice())?;
+ .read_json()?;
- Ok(data)
+ Ok(keyinfo)
}
pub fn get_service_name(&self, name: &str) -> Result<Service> {
let mut req_url = self.url.to_owned();
req_url.push_str("get_service?service_name=");
req_url.push_str(&urlencoding::encode(name));
- let response = if let Some(key) = &self.sessionkey {
+ let service: Service = if let Some(key) = &self.sessionkey {
+ ureq::get(req_url)
+ .header("Hydrus-Client-API-Access-Key", key)
+ .call()?
+ .body_mut()
+ .read_json()?
+ } else if let Some(key) = &self.apikey {
+ ureq::get(req_url)
+ .header("Hydrus-Client-API-Access-Key", key)
+ .call()?
+ .body_mut()
+ .read_json()?
+ } else {
+ return Err(HydrusError::KeyNotSupplied);
+ };
+
+ Ok(service)
+ }
+
+ pub fn get_service_key(&self, name: &str) -> Result<Service> {
+ let mut req_url = self.url.to_owned();
+ req_url.push_str("get_service?service_key=");
+ req_url.push_str(&urlencoding::encode(name));
+ let service: Service = if let Some(key) = &self.sessionkey {
ureq::get(req_url)
.header("Hydrus-Client-API-Access-Key", key)
.call()?
.body_mut()
- .read_to_vec()?
+ .read_json()?
} else if let Some(key) = &self.apikey {
ureq::get(req_url)
.header("Hydrus-Client-API-Access-Key", key)
.call()?
.body_mut()
- .read_to_vec()?
+ .read_json()?
} else {
return Err(HydrusError::KeyNotSupplied);
};
- let service: Service = musli::json::decode(response.as_slice())?;
Ok(service)
}
+
+ pub fn get_services(&self) -> Result<Vec<Service>> {
+ let mut req_url = self.url.to_owned();
+ req_url.push_str("get_services");
+ let response: ServiceResponse = if let Some(key) = &self.sessionkey {
+ ureq::get(req_url)
+ .header("Hydrus-Client-API-Access-Key", key)
+ .call()?
+ .body_mut()
+ .read_json()?
+ } else if let Some(key) = &self.apikey {
+ ureq::get(req_url)
+ .header("Hydrus-Client-API-Access-Key", key)
+ .call()?
+ .body_mut()
+ .read_json()?
+ } else {
+ return Err(HydrusError::KeyNotSupplied);
+ };
+ println!("{:?}", response);
+ Ok(vec![])
+ }
}
diff --git a/src/tests.rs b/src/tests.rs
index 3fafdbe..9dc56e1 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -8,7 +8,7 @@ fn correct_hydruspermissions_url_encode() {
HydrusPermissions::ImportAndEditFiles,
HydrusPermissions::SeeLocalPaths,
];
- let json_string = musli::json::to_string(&perms).unwrap();
+ let json_string = serde_json::to_string(&perms).unwrap();
let encoded = urlencoding::encode(&json_string);
assert_eq!(encoded, "%5B0%2C1%2C13%5D")
@@ -21,9 +21,9 @@ fn correct_hydruspermissions_decode() {
HydrusPermissions::ImportAndEditFiles,
HydrusPermissions::SeeLocalPaths,
];
- let json_string = musli::json::to_string(&perms).unwrap();
+ let json_string = serde_json::to_string(&perms).unwrap();
- let res: [HydrusPermissions; 3] = musli::json::from_str(&json_string).unwrap();
+ let res: [HydrusPermissions; 3] = serde_json::from_str(&json_string).unwrap();
assert_eq!(res, perms)
}
@@ -35,14 +35,16 @@ fn correct_hydrusservice_decode() {
ServiceType::Trash,
];
- let json_string = musli::json::to_string(&input).unwrap();
+ let json_string = serde_json::to_string(&input).unwrap();
- let res: [ServiceType; 3] = musli::json::from_str(&json_string).unwrap();
+ let res: [ServiceType; 3] = serde_json::from_str(&json_string).unwrap();
assert_eq!(res, input)
}
#[test]
-fn get_keys() {
- let client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
- let _ = client.request_new_permissions("test client", &[]).unwrap();
+fn client_test() {
+ let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
+ client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
+ let test = client.get_services().unwrap();
+ println!("{:?}", test);
}
diff --git a/src/types.rs b/src/types.rs
index ed00efb..6f720c6 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,5 +1,6 @@
-use musli::{Allocator, Decode, Decoder, Encode, Encoder};
-use strum_macros::FromRepr;
+use serde::Deserialize;
+use serde_repr::{Deserialize_repr, Serialize_repr};
+use std::collections::HashMap;
use thiserror::Error;
#[derive(Error, Debug)]
@@ -7,13 +8,13 @@ pub enum HydrusError {
#[error("failed to connect to Hydrus")]
NetworkError(ureq::Error),
#[error("failed to deserialize data")]
- DeserializeError(musli::json::Error),
+ DeserializeError(serde_json::Error),
#[error("api or session key needed")]
KeyNotSupplied,
}
-impl From<musli::json::Error> for HydrusError {
- fn from(value: musli::json::Error) -> Self {
+impl From<serde_json::Error> for HydrusError {
+ fn from(value: serde_json::Error) -> Self {
HydrusError::DeserializeError(value)
}
}
@@ -24,7 +25,7 @@ impl From<ureq::Error> for HydrusError {
}
}
-#[derive(PartialEq, Debug, Clone, FromRepr)]
+#[derive(PartialEq, Debug, Clone, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum HydrusPermissions {
ImportAndEditURLs = 0,
@@ -44,51 +45,17 @@ pub enum HydrusPermissions {
Null = 255,
}
-impl<M> Encode<M> for HydrusPermissions {
- type Encode = Self;
-
- #[inline]
- fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
- where
- E: Encoder<Mode = M>,
- {
- encoder.encode(self.clone() as u8)
- }
-
- #[inline]
- fn as_encode(&self) -> &Self::Encode {
- self
- }
-}
-
-impl<'de, M, A> Decode<'de, M, A> for HydrusPermissions
-where
- A: Allocator,
-{
- #[inline]
- fn decode<D>(decoder: D) -> Result<Self, D::Error>
- where
- D: Decoder<'de>,
- {
- if let Some(val) = HydrusPermissions::from_repr(decoder.decode()?) {
- return Ok(val);
- } else {
- return Ok(Self::Null);
- }
- }
-}
-
-#[derive(Decode, Debug)]
+#[derive(Deserialize, Debug)]
pub struct AccessKey {
pub access_key: String,
}
-#[derive(Decode, Debug)]
+#[derive(Deserialize, Debug)]
pub struct SessionKey {
pub session_key: String,
}
-#[derive(Decode, Debug)]
+#[derive(Deserialize, Debug)]
pub struct KeyInfo {
pub name: String,
pub permits_everything: bool,
@@ -96,7 +63,7 @@ pub struct KeyInfo {
pub human_permissions: String,
}
-#[derive(PartialEq, Debug, Clone, FromRepr)]
+#[derive(PartialEq, Debug, Clone, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum ServiceType {
TagRepository = 0,
@@ -121,51 +88,24 @@ pub enum ServiceType {
Null = 255,
}
-impl<M> Encode<M> for ServiceType {
- type Encode = Self;
-
- #[inline]
- fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
- where
- E: Encoder<Mode = M>,
- {
- encoder.encode(self.clone() as u8)
- }
-
- #[inline]
- fn as_encode(&self) -> &Self::Encode {
- self
- }
-}
-
-impl<'de, M, A> Decode<'de, M, A> for ServiceType
-where
- A: Allocator,
-{
- #[inline]
- fn decode<D>(decoder: D) -> Result<Self, D::Error>
- where
- D: Decoder<'de>,
- {
- if let Some(val) = ServiceType::from_repr(decoder.decode()?) {
- return Ok(val);
- } else {
- return Ok(Self::Null);
- }
- }
-}
-
-#[derive(Decode)]
+#[derive(Deserialize, Debug)]
pub struct Service {
pub name: String,
- #[musli(default)]
+ #[serde(default)]
pub service_key: String,
- pub servicetype: ServiceType,
+ pub r#type: ServiceType,
pub type_pretty: String,
- #[musli(default)]
+ #[serde(default)]
pub star_shape: String,
- #[musli(default)]
+ #[serde(default)]
pub min_stars: u8,
- #[musli(default)]
+ #[serde(default)]
pub max_stars: u8,
}
+
+#[derive(Deserialize, Debug)]
+pub struct ServiceResponse {
+ pub services: HashMap<String, Service>,
+ #[serde(flatten)]
+ _extra: HashMap<String, serde_json::Value>,
+}