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
use crate::{Esi, EsiError};
use reqwest::{header, Client};
use std::time::Duration;
#[derive(Clone, Debug, Default)]
pub struct EsiBuilder {
pub(crate) version: Option<String>,
pub(crate) client_id: Option<String>,
pub(crate) client_secret: Option<String>,
pub(crate) callback_url: Option<String>,
pub(crate) scope: Option<String>,
pub(crate) access_token: Option<String>,
pub(crate) access_expiration: Option<u64>,
pub(crate) refresh_token: Option<String>,
pub(crate) user_agent: Option<String>,
pub(crate) http_timeout: Option<u64>,
}
impl EsiBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn version(mut self, val: &str) -> Self {
self.version = Some(val.to_owned());
self
}
pub fn client_id(mut self, val: &str) -> Self {
self.client_id = Some(val.to_owned());
self
}
pub fn client_secret(mut self, val: &str) -> Self {
self.client_secret = Some(val.to_owned());
self
}
pub fn callback_url(mut self, val: &str) -> Self {
self.callback_url = Some(val.to_owned());
self
}
pub fn scope(mut self, val: &str) -> Self {
self.scope = Some(val.to_owned());
self
}
pub fn access_token(mut self, val: Option<&str>) -> Self {
self.access_token = val.map(|v| v.to_owned());
self
}
pub fn access_expiration(mut self, val: Option<u64>) -> Self {
self.access_expiration = val;
self
}
pub fn refresh_token(mut self, val: Option<&str>) -> Self {
self.refresh_token = val.map(|v| v.to_owned());
self
}
pub fn user_agent(mut self, val: &str) -> Self {
self.user_agent = Some(val.to_owned());
self
}
pub fn http_timeout(mut self, val: Option<u64>) -> Self {
self.http_timeout = val;
self
}
pub(crate) fn construct_client(&self) -> Result<Client, EsiError> {
let http_timeout = self
.http_timeout
.map(Duration::from_millis)
.unwrap_or_else(|| Duration::from_secs(60));
let headers = {
let mut map = header::HeaderMap::new();
let user_agent = &self
.user_agent
.as_ref()
.ok_or_else(|| EsiError::EmptyClientValue("user_agent".to_owned()))?
.to_owned();
map.insert(
header::USER_AGENT,
header::HeaderValue::from_str(user_agent)?,
);
map.insert(
header::ACCEPT,
header::HeaderValue::from_static("application/json"),
);
map
};
let client = Client::builder()
.timeout(http_timeout)
.default_headers(headers)
.build()?;
Ok(client)
}
pub fn build(self) -> Result<Esi, EsiError> {
Esi::from_builder(self)
}
}
#[cfg(test)]
mod tests {
use super::EsiBuilder;
#[test]
fn test_builder_valid() {
let b = EsiBuilder::new()
.client_id("a")
.client_secret("b")
.callback_url("c")
.user_agent("d")
.build()
.unwrap();
assert_eq!(b.client_id, "a");
assert_eq!(b.client_secret, "b");
assert_eq!(b.callback_url, "c");
assert_eq!(b.version, "latest");
assert_eq!(b.access_token, None);
}
#[test]
fn test_builder_missing_value() {
let res = EsiBuilder::new().build();
assert!(res.is_err());
let s = format!("{}", res.unwrap_err());
assert_eq!(s, "Missing required builder struct value 'user_agent'");
}
}