summaryrefslogtreecommitdiff
path: root/src/client.rs
blob: 06d55d6a176e8c149372966d9ea676d3e6d4fd8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::types::*;

type Result<T> = std::result::Result<T, HydrusError>;

pub struct HydrusClient {
    apikey: Option<String>,
    sessionkey: Option<String>,
    url: String,
}

impl HydrusClient {
    pub fn new(url: String) -> HydrusClient {
        HydrusClient {
            apikey: None,
            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<String> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("/request_new_permissions?name=");
        req_url.push_str(&name);

        if permissions.is_empty() {
            req_url.push_str("&permit_everything=true");
        } else {
            req_url.push_str("&basic_permissions=");
            let json_string = musli::json::to_string(&permissions).unwrap();
            req_url.push_str(&urlencoding::encode(&json_string));
        };

        let response = ureq::get(req_url).call();

        if let Err(error) = response {
            return Err(HydrusError::NetworkError(error));
        }

        let key = response.unwrap().body_mut().read_to_vec();

        if let Err(error) = key {
            return Err(HydrusError::DeserializeError(error));
        };

        let accesskey: String = musli::json::decode(key.unwrap().as_slice()).unwrap();

        Ok(key.unwrap().access_key)
    }

    pub fn get_session_key(&self) -> Result<String> {
        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 response = request.call();

        if let Err(error) = response {
            return Err(HydrusError::NetworkError(error));
        }

        let key = response.unwrap().body_mut().read_json::<SessionKey>();

        if let Err(error) = key {
            return Err(HydrusError::DeserializeError(error));
        }

        Ok(key.unwrap().session_key)
    }

    pub fn verify_access_key(&self, key: String) -> Result<KeyInfo> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("/verify_access_key");
        let response = ureq::get(req_url)
            .header("Hydrus-Client-API-Access-Key", key)
            .call();

        if let Err(error) = response {
            return Err(HydrusError::NetworkError(error));
        }

        let data = response.unwrap().body_mut().read_json::<KeyInfo>();

        if let Err(error) = data {
            return Err(HydrusError::DeserializeError(error));
        }

        Ok(data.unwrap())
    }

    pub fn get_service_name(&self, name: String) -> Result<GetService> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("/get_service?service_name=");
        req_url.push_str(&urlencoding::encode(&name));
        todo!()
    }
}