summaryrefslogtreecommitdiff
path: root/src/client.rs
blob: 0b3f1c72732c9ccbd235a72d6f7b7d0308b74671 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::{collections::HashMap, path::PathBuf};

use async_trait::async_trait;
use reqwest::{Body, RequestBuilder};
use serde::Deserialize;
use tokio_util::codec::{BytesCodec, FramedRead};

use crate::{traits::*, types::*};

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

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

impl HydrusClient {
    pub fn new(url: &str) -> HydrusClient {
        HydrusClient {
            client: reqwest::Client::new(),
            apikey: None,
            sessionkey: None,
            url: url.to_string(),
        }
    }

    pub fn set_api_key(&mut self, key: &str) {
        self.apikey = Some(key.to_owned())
    }

    pub fn set_session_key(&mut self, key: &str) {
        self.sessionkey = Some(key.to_owned())
    }

    fn set_get_request_key(&self, url: &str) -> Result<RequestBuilder> {
        let request = self.client.get(url);
        if let Some(key) = &self.sessionkey {
            Ok(request.header("Hydrus-Client-API-Access-Key", key))
        } else if let Some(key) = &self.apikey {
            Ok(request.header("Hydrus-Client-API-Access-Key", key))
        } else {
            Err(HydrusError::KeyNotSupplied)
        }
    }

    fn set_post_request_key(&self, url: &str) -> Result<RequestBuilder> {
        let request = self.client.post(url);
        if let Some(key) = &self.sessionkey {
            Ok(request.header("Hydrus-Client-API-Access-Key", key))
        } else if let Some(key) = &self.apikey {
            Ok(request.header("Hydrus-Client-API-Access-Key", key))
        } else {
            Err(HydrusError::KeyNotSupplied)
        }
    }
}

#[derive(Deserialize, Debug)]
struct HydrusResponse<T> {
    #[serde(
        alias = "service",
        alias = "services",
        alias = "access_key",
        alias = "session_key"
    )]
    body: T,
}

#[async_trait]
impl AccessManagement for HydrusClient {
    async fn request_new_permissions(
        &self,
        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);

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

        Ok(self
            .client
            .get(req_url)
            .send()
            .await?
            .json::<HydrusResponse<String>>()
            .await?
            .body)
    }

    async fn get_session_key(&self) -> Result<String> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("session_key");

        let mut request = self.client.get(req_url);

        if let Some(key) = &self.apikey {
            request = request.header("Hydrus-Client-API-Access-Key", key);
        }

        Ok(request
            .send()
            .await?
            .json::<HydrusResponse<String>>()
            .await?
            .body)
    }

    async fn verify_access_key(&self, key: &str) -> Result<KeyInfo> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("verify_access_key");

        Ok(self
            .client
            .get(req_url)
            .header("Hydrus-Client-API-Access-Key", key)
            .send()
            .await?
            .json::<KeyInfo>()
            .await?)
    }

    async 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));

        Ok(self
            .set_get_request_key(&req_url)?
            .send()
            .await?
            .json::<HydrusResponse<Service>>()
            .await?
            .body)
    }

    async fn get_service_key(&self, key: &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(key));

        Ok(self
            .set_get_request_key(&req_url)?
            .send()
            .await?
            .json::<HydrusResponse<Service>>()
            .await?
            .body)
    }

    async fn get_services(&self) -> Result<HashMap<String, Service>> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("get_services");

        let mut services = self
            .set_get_request_key(&req_url)?
            .send()
            .await?
            .json::<HydrusResponse<HashMap<String, Service>>>()
            .await?
            .body;

        for (key, service) in services.iter_mut() {
            service.service_key = key.to_string();
        }

        Ok(services)
    }
}

#[async_trait]
impl ImportingAndDeletingFiles for HydrusClient {
    async fn add_file_via_path(
        &self,
        path: &str,
        delete: Option<bool>,
        domains: Option<FileDomain>,
    ) -> Result<FileAddResponse> {
        let mut form = AddFileRequest {
            path: path.to_owned(),
            delete_after_successful_import: delete,
            ..Default::default()
        };

        if let Some(file_domains) = domains {
            match file_domains {
                FileDomain::FileServiceKey(key) => form.file_service_key = Some(key),
                FileDomain::FileServiceKeys(keys) => form.file_service_keys = Some(keys),
                FileDomain::DeletedFileServiceKey(key) => form.deleted_file_service_key = Some(key),
                FileDomain::DeletedFileServiceKeys(keys) => {
                    form.deleted_file_service_keys = Some(keys)
                }
            }
        }

        let mut req_url = self.url.to_owned();
        req_url.push_str("add_files/add_file");

        Ok(self
            .set_post_request_key(&req_url)?
            .header("Content-Type", "application/json")
            .json(&form)
            .send()
            .await?
            .json::<FileAddResponse>()
            .await?)
    }

    async fn add_file_via_file(&self, file: PathBuf) -> Result<FileAddResponse> {
        let mut req_url = self.url.to_owned();
        req_url.push_str("add_files/add_file");

        let file = tokio::fs::File::open(file).await?;

        let stream = FramedRead::new(file, BytesCodec::new());
        let body = Body::wrap_stream(stream);

        Ok(self
            .set_post_request_key(&req_url)?
            .header("Content-Type", "application/octet-stream")
            .body(body)
            .send()
            .await?
            .json::<FileAddResponse>()
            .await?)
    }
}