summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.rs47
-rw-r--r--src/tests.rs68
-rw-r--r--src/types.rs17
3 files changed, 89 insertions, 43 deletions
diff --git a/src/client.rs b/src/client.rs
index e4071b0..4847db8 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -9,30 +9,30 @@ pub struct HydrusClient {
}
impl HydrusClient {
- pub fn new(url: String) -> HydrusClient {
+ pub fn new(url: &str) -> HydrusClient {
HydrusClient {
apikey: None,
sessionkey: None,
- url,
+ url: url.to_string(),
}
}
- pub fn set_api_key(&mut self, key: String) {
- self.apikey = Some(key)
+ pub fn set_api_key(&mut self, key: &str) {
+ self.apikey = Some(key.to_owned())
}
- pub fn set_session_key(&mut self, key: String) {
- self.sessionkey = Some(key)
+ pub fn set_session_key(&mut self, key: &str) {
+ self.sessionkey = Some(key.to_owned())
}
pub fn request_new_permissions(
&self,
- name: String,
+ name: &str,
permissions: &[HydrusPermissions],
) -> Result<String> {
let mut req_url = self.url.to_owned();
- req_url.push_str("/request_new_permissions?name=");
- req_url.push_str(&name);
+ req_url.push_str("request_new_permissions?name=");
+ req_url.push_str(&urlencoding::encode(&name));
if permissions.is_empty() {
req_url.push_str("&permit_everything=true");
@@ -51,7 +51,7 @@ impl HydrusClient {
pub fn get_session_key(&self) -> Result<String> {
let mut req_url = self.url.to_owned();
- req_url.push_str("/session_key");
+ req_url.push_str("session_key");
let mut request = ureq::get(req_url);
@@ -66,9 +66,9 @@ impl HydrusClient {
Ok(key.session_key)
}
- pub fn verify_access_key(&self, key: String) -> Result<KeyInfo> {
+ 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");
+ req_url.push_str("verify_access_key");
let response = ureq::get(req_url)
.header("Hydrus-Client-API-Access-Key", key)
.call()?
@@ -80,10 +80,27 @@ impl HydrusClient {
Ok(data)
}
- pub fn get_service_name(&self, name: String) -> Result<Service> {
+ 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("get_service?service_name=");
req_url.push_str(&urlencoding::encode(&name));
- todo!()
+ let response = if let Some(key) = &self.sessionkey {
+ ureq::get(req_url)
+ .header("Hydrus-Client-API-Access-Key", key)
+ .call()?
+ .body_mut()
+ .read_to_vec()?
+ } else if let Some(key) = &self.apikey {
+ ureq::get(req_url)
+ .header("Hydrus-Client-API-Access-Key", key)
+ .call()?
+ .body_mut()
+ .read_to_vec()?
+ } else {
+ return Err(HydrusError::KeyNotSupplied);
+ };
+
+ let service: Service = musli::json::decode(response.as_slice())?;
+ Ok(service)
}
}
diff --git a/src/tests.rs b/src/tests.rs
index 4c2a049..d22dedd 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -1,30 +1,50 @@
+use crate::client::HydrusClient;
+use http::Uri;
+
#[cfg(test)]
-mod tests {
- use crate::types::*;
+use crate::types::*;
+#[test]
+fn correct_hydruspermissions_url_encode() {
+ let perms: [HydrusPermissions; 3] = [
+ HydrusPermissions::ImportAndEditURLs,
+ HydrusPermissions::ImportAndEditFiles,
+ HydrusPermissions::SeeLocalPaths,
+ ];
+ let json_string = musli::json::to_string(&perms).unwrap();
+ let encoded = urlencoding::encode(&json_string);
+
+ assert_eq!(encoded, "%5B0%2C1%2C13%5D")
+}
- #[test]
- fn correct_hydruspermissions_url_encode() {
- let perms: [HydrusPermissions; 3] = [
- HydrusPermissions::ImportAndEditURLs,
- HydrusPermissions::ImportAndEditFiles,
- HydrusPermissions::SeeLocalPaths,
- ];
- let json_string = musli::json::to_string(&perms).unwrap();
- let encoded = urlencoding::encode(&json_string);
+#[test]
+fn correct_hydruspermissions_decode() {
+ let perms: [HydrusPermissions; 3] = [
+ HydrusPermissions::ImportAndEditURLs,
+ HydrusPermissions::ImportAndEditFiles,
+ HydrusPermissions::SeeLocalPaths,
+ ];
+ let json_string = musli::json::to_string(&perms).unwrap();
- assert_eq!(encoded, "%5B0%2C1%2C13%5D")
- }
+ let res: [HydrusPermissions; 3] = musli::json::from_str(&json_string).unwrap();
+ assert_eq!(res, perms)
+}
- #[test]
- fn correct_hydruspermissions_decode() {
- let perms: [HydrusPermissions; 3] = [
- HydrusPermissions::ImportAndEditURLs,
- HydrusPermissions::ImportAndEditFiles,
- HydrusPermissions::SeeLocalPaths,
- ];
- let json_string = musli::json::to_string(&perms).unwrap();
+#[test]
+fn correct_hydrusservice_decode() {
+ let input: [ServiceType; 3] = [
+ ServiceType::AllLocalFiles,
+ ServiceType::ClientAPI,
+ ServiceType::Trash,
+ ];
+
+ let json_string = musli::json::to_string(&input).unwrap();
+
+ let res: [ServiceType; 3] = musli::json::from_str(&json_string).unwrap();
+ assert_eq!(res, input)
+}
- let res: [HydrusPermissions; 3] = musli::json::from_str(&json_string).unwrap();
- assert_eq!(res, perms)
- }
+#[test]
+fn get_keys() {
+ let client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
+ let _ = client.request_new_permissions("test client", &[]).unwrap();
}
diff --git a/src/types.rs b/src/types.rs
index d1abb8d..2cf1524 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -8,6 +8,8 @@ pub enum HydrusError {
NetworkError(ureq::Error),
#[error("failed to deserialize data")]
DeserializeError(musli::json::Error),
+ #[error("api or session key needed")]
+ KeyNotSupplied,
}
impl From<musli::json::Error> for HydrusError {
@@ -95,17 +97,17 @@ where
}
}
-#[derive(Decode)]
+#[derive(Decode, Debug)]
pub struct AccessKey {
pub access_key: String,
}
-#[derive(Decode)]
+#[derive(Decode, Debug)]
pub struct SessionKey {
pub session_key: String,
}
-#[derive(Decode)]
+#[derive(Decode, Debug)]
pub struct KeyInfo {
pub name: String,
pub permits_everything: bool,
@@ -190,7 +192,8 @@ where
where
D: Decoder<'de>,
{
- Ok(decoder.decode()?)
+ let val: u8 = decoder.decode()?;
+ Ok(val.into())
}
}
@@ -201,4 +204,10 @@ pub struct Service {
pub service_key: String,
pub servicetype: ServiceType,
pub type_pretty: String,
+ #[musli(default)]
+ pub star_shape: String,
+ #[musli(default)]
+ pub min_stars: u8,
+ #[musli(default)]
+ pub max_stars: u8,
}