wwsvc-rs 2.0.3

A web client which is used to consume SoftENGINE's WEBSERVICES, a proprietary API for their software WEBWARE.
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use reqwest::Response;
use reqwest::header::{HeaderMap, HeaderValue};
use serde::de::DeserializeOwned;
use std::convert::{TryInto};
use std::collections::{HashMap};

use crate::{Cursor, Credentials};
use crate::AppHash;

/// A builder for constructing a WEBWARE client
pub struct WebwareClientBuilder {
    /// Host of the WEBWARE server
    host: String,
    /// Port of the WEBWARE server
    port: u16,
    /// Path to the WEBSERVICES endpoint, relative to the full URL
    webservice_path: String,
    /// Vendor hash of the application
    vendor_hash: String,
    /// Application hash of the application
    app_hash: String,
    /// Application secret, assigned by the WEBWARE instance
    secret: String,
    /// Revision of the application
    revision: u32,
    /// Allow unsafe certificates
    /// (only use this if you know what you are doing)
    /// (default: false)
    allow_unsafe_certs: bool,
    /// The timeout for the request in seconds
    /// (default: 60)
    timeout: u64,
    /// Maximum amount of objects that are returned in a request
    result_max_lines: u32,
    /// Service pass for the application
    service_pass: Option<String>,
    /// Application ID required for the application
    app_id: Option<String>,
}

impl Default for WebwareClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl WebwareClientBuilder {
    /// Creates a new instance of the builder
    pub fn new() -> Self {
        WebwareClientBuilder {
            host: String::new(),
            port: 443,
            webservice_path: "/WWSVC/".to_string(),
            vendor_hash: String::new(),
            app_hash: String::new(),
            secret: String::new(),
            revision: 0,
            allow_unsafe_certs: false,
            timeout: 60,
            result_max_lines: 1000,
            service_pass: None,
            app_id: None,
        }
    }

    /// Sets the host of the WEBWARE server
    /// 
    /// Don't include the protocol (http:// or https://)!
    /// 
    /// (default: "")
    pub fn host(mut self, host: &str) -> Self {
        self.host = host.to_string();
        self
    }

    /// Sets the port of the WEBWARE server
    /// 
    /// (default: 443)
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }


    /// Sets the path to the WEBSERVICES endpoint, relative to the full URL
    /// 
    /// (default: "/WWSVC/")
    pub fn webservice_path(mut self, path: &str) -> Self {
        self.webservice_path = path.to_string();
        self
    }

    /// Sets the vendor hash of the application
    /// 
    /// (default: "")
    pub fn vendor_hash(mut self, hash: &str) -> Self {
        self.vendor_hash = hash.to_string();
        self
    }

    /// Sets the application hash of the application
    /// 
    /// (default: "")
    pub fn app_hash(mut self, hash: &str) -> Self {
        self.app_hash = hash.to_string();
        self
    }

    /// Sets the application secret, assigned by the WEBWARE instance
    /// 
    /// (default: "")
    pub fn secret(mut self, secret: &str) -> Self {
        self.secret = secret.to_string();
        self
    }

    /// Sets the revision of the application
    /// 
    /// (default: 0)
    pub fn revision(mut self, revision: u32) -> Self {
        self.revision = revision;
        self
    }

    /// Sets whether to allow unsafe certificates
    /// 
    /// (default: false)
    pub fn allow_unsafe_certs(mut self, allow: bool) -> Self {
        self.allow_unsafe_certs = allow;
        self
    }

    /// Sets the timeout for the request in seconds
    /// 
    /// (default: 60)
    pub fn timeout(mut self, timeout: u64) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the maximum amount of objects that are returned in a response
    /// 
    /// (default: 1000)
    pub fn result_max_lines(mut self, lines: u32) -> Self {
        self.result_max_lines = lines;
        self
    }

    /// Sets the service pass for the application
    /// 
    /// (default: None)
    pub fn service_pass(mut self, pass: &str) -> Self {
        self.service_pass = Some(pass.to_string());

        self
    }

    /// Sets the application ID required for the application
    /// 
    /// (default: None)
    pub fn app_id(mut self, id: &str) -> Self {
        self.app_id = Some(id.to_string());

        self
    }

    /// Sets the credentials for the application
    /// 
    /// This will fill both `app_id` and `service_pass`.
    /// 
    /// (default: None)
    pub fn credentials(mut self, credentials: Credentials) -> Self {
        self.app_id = Some(credentials.app_id);
        self.service_pass = Some(credentials.service_pass);

        self
    }

    /// Builds the client
    pub fn build(self) -> WebwareClient {
        WebwareClient {
            webware_url: format!("https://{}:{}{}", self.host, self.port, self.webservice_path),
            vendor_hash: self.vendor_hash,
            app_hash: self.app_hash,
            secret: self.secret,
            revision: self.revision,
            result_max_lines: self.result_max_lines,
            app_id: self.app_id,
            current_request: 0,
            service_pass: self.service_pass,
            cursor: None,
            client: reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(self.timeout))
                .danger_accept_invalid_certs(self.allow_unsafe_certs)
                .build()
                .unwrap(),
        }
    }
}

/// The web client to consume SoftENGINE's WEBSERVICES
pub struct WebwareClient {
    /// Full URL to the WEBWARE instance
    webware_url: String,
    /// Vendor hash of the application
    vendor_hash: String,
    /// Application hash of the application
    app_hash: String,
    /// Application secret, assigned by the WEBWARE instance
    secret: String,
    /// Revision of the application
    revision: u32,
    /// Application ID for the service pass, only populated after `register()` is ran
    app_id: Option<String>,
    /// Current request ID
    current_request: u32,
    /// Maximum amount of objects that are returned in a request
    result_max_lines: u32,
    /// Service pass of the client, only populated after `register()` is ran
    service_pass: Option<String>,
    /// Internal reqwest client
    client: reqwest::Client,
    /// Request cursor for pagination,
    cursor: Option<Cursor>,
}

impl WebwareClient {
    /// Creates a new builder for the client
    pub fn builder() -> WebwareClientBuilder {
        WebwareClientBuilder::new()
    }

    /// Creates a new pagination cursor and makes it available for the next requests (until it is closed)
    pub fn create_cursor(&mut self, max_lines: u32) {
        self.cursor = Some(Cursor::new(max_lines));
    }

    /// Returns whether the current cursor is closed.
    /// 
    /// Returns true if there is no cursor.
    pub fn cursor_closed(&self) -> bool {
        if let Some(c) = self.cursor.as_ref() {
            c.closed()
        } else {
            true
        }
    }

    /// Sets the maximum amount of results that are returned in a response
    pub fn set_result_max_lines(&mut self, max_lines: u32) {
        self.result_max_lines = max_lines;
    }

    /// Returns a set of headers, that are required on all requests to the WEBSERVICES (except `REGISTER`).
    ///
    /// This will automatically append necessary authentication headers and increase the request ID, if `register()` was successful.
    pub fn get_default_headers(&mut self, additional_headers: Option<HashMap<&str, &str>>) -> HeaderMap {
        let mut max_lines = self.result_max_lines;

        let mut header_vec = vec![
            ("WWSVC-EXECUTE-MODE", "SYNCHRON".to_string()),
            ("WWSVC-ACCEPT-RESULT-TYPE", "JSON".to_string())
        ];
        
        if self.app_id.is_some() {
            let app_id = self.app_id.as_deref().expect("msg");
            let app_hash = AppHash::new(self.current_request, app_id.to_string());
            self.current_request = app_hash.request_id;
            header_vec.append(&mut vec![
                ("WWSVC-REQID", format!("{}", self.current_request)),
                ("WWSVC-TS",  app_hash.date_formatted.to_string()),
                ("WWSVC-HASH", format!("{:x}", app_hash))
            ]);

            if let Some(cursor) = &self.cursor {
                if !cursor.closed() {
                    header_vec.append(&mut vec![
                        ("WWSVC-CURSOR", cursor.cursor_id.to_string())
                    ]);
                    max_lines = cursor.max_lines;
                }
            }
        }

        header_vec.push(("WWSVC-ACCEPT-RESULT-MAX-LINES", format!("{}", max_lines)));

        let mut headers: HashMap<String, String> = header_vec.iter()
            .map(|(s1, s2)|(s1.to_string(), s2.to_string()))
            .collect();
        
        if let Some(additional_headers) = additional_headers {
            for (key, value) in additional_headers {
                headers.insert(key.to_string(), value.to_string());
            }
        }

        (&headers).try_into().expect("invalid headers")
    }

    /// Returns the same set of headers, that `get_default_headers()` returns, except the result type header is set to `BIN` instead.
    pub fn get_bin_headers(&mut self, additional_headers: Option<HashMap<&str, &str>>) -> HeaderMap {
        let mut headers = self.get_default_headers(additional_headers);
        headers.remove("WWSVC-ACCEPT-RESULT-TYPE");
        headers.append("WWSVC-ACCEPT-RESULT-TYPE", HeaderValue::from_str("BIN").expect("valid header"));
        headers
    }

    /// Builds a valid WEBSERVICES URL from URL parts
    pub fn build_url(&self, parts: Vec<String>) -> String {
        let append = parts.join("/");
        return format!("{}{}", self.webware_url, append);
    }

    /// Sends a `REGISTER` request to the WEBWARE instance and returns whether the request succeeded or not.
    ///
    /// If the result is not `true`, the client has no valid service pass and cannot perform requests!
    pub async fn register(&mut self) -> Result<bool, Box<dyn std::error::Error>> {
        let target_url = self.build_url(vec!["WWSERVICE".to_string(), "REGISTER".to_string(), self.vendor_hash.clone(), self.app_hash.clone(), self.secret.clone(), self.revision.clone().to_string()]);
        let response = self.client.get(target_url).send().await?;
        let response_obj = response.json::<HashMap<String, serde_json::Value>>().await?;

        if !response_obj.contains_key("SERVICEPASS") {
            return Ok(false);
        }

        let service_pass = response_obj["SERVICEPASS"].as_object().unwrap();
        self.service_pass = Some(service_pass["PASSID"].as_str().unwrap().to_string());
        self.app_id = Some(service_pass["APPID"].as_str().unwrap().to_string());

        Ok(true)
    }

    /// Sends a `DEREGISTER` request to the WEBWARE instance, in order to invalidate the service pass.
    ///
    /// If the client was not authenticated using `register()` before, it will instead just return true;
    pub async fn deregister(&mut self) -> bool {
        if self.service_pass.is_none() {
            return true;
        }
        let target_url = self.build_url(vec!["WWSERVICE".to_string(), "DEREGISTER".to_string(), self.service_pass.clone().unwrap()]);
        let headers = self.get_default_headers(None);
        let _ = self.client.get(target_url).headers(headers).send().await;
        self.service_pass = None;
        self.app_id = None;
        true
    }

    /// Performs a request to the WEBSERVICES and returns a JSON value.
    pub async fn request(&mut self, method: reqwest::Method, function: &str, version: u32, parameters: HashMap<&str, &str>, additional_headers: Option<HashMap<&str, &str>>) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
        return self.request_generic::<serde_json::Value>(method, function, version, parameters, additional_headers).await;
    }

    /// Performs a request to the WEBSERVICES and returns a response object.
    pub async fn request_as_response(&mut self, method: reqwest::Method, function: &str, version: u32, parameters: HashMap<&str, &str>, additional_headers: Option<HashMap<&str, &str>>) -> Result<Response, Box<dyn std::error::Error>> {
        let target_url = self.build_url(vec!["EXECJSON".to_string()]);
        let headers = self.get_default_headers(additional_headers);
        let mut param_vec: Vec<HashMap<String, String>> = Vec::new();
        let app_hash_header = headers.get("WWSVC-HASH");
        let timestamp_header = headers.get("WWSVC-TS");
        let app_hash: String = app_hash_header.unwrap_or(&HeaderValue::from_str("").unwrap()).to_str().expect("msg").to_string();
        let timestamp: String = timestamp_header.unwrap_or(&HeaderValue::from_str("").unwrap()).to_str().expect("msg").to_string();

        for (p_key, p_value) in parameters {
            let mut map: HashMap<String, String> = HashMap::new();
            map.insert("PNAME".to_string(), p_key.to_string());
            map.insert("PCONTENT".to_string(), p_value.to_string());
            param_vec.push(map);
        }
        let body = json!({
            "WWSVC_FUNCTION": {
                "FUNCTIONNAME": function,
                "PARAMETER": param_vec,
                "REVISION": version
            },
            "WWSVC_PASSINFO": {
                "SERVICEPASS": self.service_pass.as_deref().unwrap_or(""),
                "APPHASH": app_hash,
                "TIMESTAMP": timestamp,
                "REQUESTID": self.current_request,
                "EXECUTE_MODE": "SYNCHRON"
            }
        });
        let response = self.client.request(method, target_url)
            .headers(headers)
            .json(&body)
            .send().await?;
        
        if let Some(cursor) = &mut self.cursor {
            if !cursor.closed() && response.headers().contains_key("WWSVC-CURSOR") {
                cursor.set_cursor_id(response.headers().get("WWSVC-CURSOR").unwrap().to_str().unwrap().to_string());
            }
        }
        
        Ok(response)
    }

    /// Performs a request to the WEBSERVICES and deserializes the response to the type `T`.
    ///
    /// **NOTE:** Due to the nature of the WEBSERVICES, deserialization might fail due to structural issues. In that case, use `request()` instead.
    pub async fn request_generic<T>(&mut self, method: reqwest::Method, function: &str, version: u32, parameters: HashMap<&str, &str>, additional_headers: Option<HashMap<&str, &str>>) -> Result<T, Box<dyn std::error::Error>> 
    where
        T:DeserializeOwned
    {
        let response = self.request_as_response(method, function, version, parameters, additional_headers).await?;
        let response_obj = response.json::<T>().await?;
        Ok(response_obj)
    }

    /// Generates a set of credentials from the current client.
    pub fn credentials(&self) -> Option<Credentials> {
        if self.service_pass.is_none() || self.app_id.is_none() {
            return None;
        }
        Some(Credentials::new(self.service_pass.clone().unwrap(), self.app_id.clone().unwrap()))
    }
}