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
use crate::authentication::Authentication;
use crate::data::Category;
use crate::error::Error as ImageVaultError;
use crate::Client;
const GET_CATEGORIES_ENDPOINT: &'static str = "/apiv2/categoryservice/getcategories";
pub async fn get_categories<T: Authentication + Sync>(
client: &Client<T>,
) -> Result<Vec<Category>, ImageVaultError> {
let auth_unwrapped = client
.authentication
.as_ref()
.ok_or_else(|| ImageVaultError::AuthenticationMissing)?;
let auth_header = auth_unwrapped
.lock()
.await
.authenticate(
&client.client_identity,
&client.client_secret,
&client.base_url,
&client.reqwest_client,
)
.await?;
let full_url = client.base_url.join(GET_CATEGORIES_ENDPOINT)?;
let response = client
.reqwest_client
.get(full_url)
.bearer_auth(auth_header)
.send()
.await?;
if let Err(err) = response.error_for_status_ref() {
return Err(err.into());
} else {
let result = response.json::<Vec<Category>>().await?;
return Ok(result);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::authentication::DummyAuth;
use crate::testutil::get_test_data;
#[test]
fn get_categories_test() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let mock = mockito::mock("GET", GET_CATEGORIES_ENDPOINT)
.expect(1)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(get_test_data("get_categories_response"))
.create();
let auth = DummyAuth::new();
let client = Client::new("client_identity", "client_secret", &mockito::server_url())
.unwrap()
.with_authentication(auth);
let categories = get_categories(&client).await;
assert!(categories.is_ok());
mock.assert();
assert!(categories.unwrap().len() > 0);
});
}
}