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
use std::rc::Rc;
use crate::{error::WildlandHttpClientError, response_handler::check_status_code};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct ConfirmTokenReq {
pub email: String,
pub verification_token: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetStorageReq {
pub email: String,
pub pubkey: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetStorageRes {
pub encrypted_credentials: Option<String>,
}
#[derive(Clone, Default, Debug)]
pub struct EvsClient {
base_url: String,
}
impl EvsClient {
#[tracing::instrument(level = "debug", ret)]
pub fn new(base_url: &str) -> Self {
Self {
base_url: base_url.to_string(),
}
}
#[tracing::instrument(level = "debug", ret, skip(self))]
pub fn confirm_token(&self, request: ConfirmTokenReq) -> Result<(), WildlandHttpClientError> {
let url = format!("{}/confirm_token", self.base_url);
let response = minreq::put(url)
.with_json(&request)
.map_err(|e| WildlandHttpClientError::HttpLibError(Rc::new(e)))?
.send()
.map_err(Rc::new)?;
check_status_code(response)?;
Ok(())
}
#[tracing::instrument(level = "debug", ret, skip(self))]
pub fn get_storage(
&self,
request: GetStorageReq,
) -> Result<GetStorageRes, WildlandHttpClientError> {
let url = format!("{}/get_storage", self.base_url);
let response = minreq::put(url)
.with_json(&request)
.map_err(|e| WildlandHttpClientError::HttpLibError(Rc::new(e)))?
.send()
.map_err(Rc::new)?;
let response = check_status_code(response)?;
match response.status_code {
200 => Ok(response.json().map_err(Rc::new)?),
_ => Ok(GetStorageRes {
encrypted_credentials: None,
}),
}
}
}
#[cfg(test)]
mod tests {
use crate::evs::constants::test_utilities::{
EMAIL, ENCRYPTED_CREDENTIALS, PUBKEY, VERIFICATION_TOKEN,
};
use mockito::{mock, server_url};
use serde_json::json;
use super::*;
fn client() -> EvsClient {
EvsClient {
base_url: server_url(),
}
}
#[test]
fn should_confirm_token() {
let request = ConfirmTokenReq {
email: EMAIL.into(),
verification_token: VERIFICATION_TOKEN.into(),
};
let m = mock("PUT", "/confirm_token").create();
let response = client().confirm_token(request);
m.assert();
response.unwrap();
}
#[test]
fn should_get_storage() {
let request = GetStorageReq {
email: EMAIL.into(),
pubkey: PUBKEY.into(),
};
let m = mock("PUT", "/get_storage")
.with_body(json!({ "encrypted_credentials": ENCRYPTED_CREDENTIALS }).to_string())
.create();
let response = client().get_storage(request).unwrap();
m.assert();
assert_eq!(
response.encrypted_credentials.unwrap(),
ENCRYPTED_CREDENTIALS
);
}
}