Repository navigation
Expand file tree
/
Copy pathclient.rs
More file actions
166 lines (146 loc) · 5.21 KB
/
Copy pathclient.rs
File metadata and controls
166 lines (146 loc) · 5.21 KB
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
//! A blocking HTTP client: the only part of the crate that performs I/O.
use std::fmt;
use std::time::Duration;
use crate::api::{ApiError, Request, BASE_URL};
const TIMEOUT: Duration = Duration::from_secs(10);
/// Bytes of a response read before it is refused. Real answers are a few thousand.
const MAX_BODY_BYTES: u64 = 1_000_000;
pub struct Client {
agent: ureq::Agent,
base_url: String,
api_key: String,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Transport(TransportError),
Api(ApiError),
}
/// The request never produced a response: DNS, TLS, timeout and the like.
///
/// Opaque, so the HTTP library behind it can change without breaking callers.
#[derive(Debug)]
pub struct TransportError(Cause);
#[derive(Debug)]
enum Cause {
Http(ureq::Error),
UnusableKey,
PlainHttp,
}
impl Client {
/// Whitespace around the key, as read from a file, is dropped. A key no header can hold
/// makes every `send` fail without a request.
pub fn new(api_key: impl Into<String>) -> Self {
Self {
agent: agent(TIMEOUT),
base_url: BASE_URL.to_owned(),
api_key: api_key.into().trim().to_owned(),
}
}
/// Allows `timeout` for a whole exchange, body included, instead of 10 seconds.
/// Anything over a day is read as a day.
pub fn with_timeout(self, timeout: Duration) -> Self {
Self {
// A deadline is the clock plus the timeout, which overflows for `Duration::MAX`.
agent: agent(timeout.min(Duration::from_secs(86_400))),
..self
}
}
/// Points the client at another host, such as a staging stack or a mock.
pub fn with_base_url(self, base_url: impl Into<String>) -> Self {
Self {
base_url: base_url.into().trim_end_matches('/').to_owned(),
..self
}
}
pub fn send<T>(&self, request: &Request<T>) -> Result<T, Error> {
// A key no header can hold is refused before anything is sent.
if !self.api_key.bytes().all(|byte| byte.is_ascii_graphic()) || self.api_key.is_empty() {
return Err(Error::Transport(TransportError(Cause::UnusableKey)));
}
if plain_http(&self.base_url) {
return Err(Error::Transport(TransportError(Cause::PlainHttp)));
}
let call = self
.agent
.get(format!("{}{}", self.base_url, request.path))
.header("X-API-Key", &self.api_key);
let mut response = request
.query
.iter()
.fold(call, |call, (key, value)| call.query(key, value))
.call()
.map_err(failed)?;
let body = response
.body_mut()
.with_config()
.limit(MAX_BODY_BYTES)
.read_to_vec()
.map_err(failed)?;
// Lossy, so a proxy's page in another encoding keeps its status and is malformed.
let body = String::from_utf8_lossy(&body);
Ok(request.decode(response.status().as_u16(), &body)?)
}
}
fn agent(timeout: Duration) -> ureq::Agent {
ureq::Agent::config_builder()
// Error statuses carry a JSON body that `Request::decode` reads.
.http_status_as_error(false)
// A redirect would carry the key to another host, so none is followed.
.max_redirects(0)
.timeout_global(Some(timeout))
.build()
.into()
}
/// Whether a request would carry the key unencrypted to another machine. A mock on this
/// machine, at localhost, 127.0.0.1 or ::1, may use plain http.
fn plain_http(base_url: &str) -> bool {
let Ok(uri) = base_url.parse::<ureq::http::Uri>() else {
return false;
};
let local = matches!(uri.host(), Some("localhost" | "127.0.0.1" | "[::1]"));
uri.scheme_str() == Some("http") && !local
}
fn failed(error: ureq::Error) -> Error {
Error::Transport(TransportError(Cause::Http(error)))
}
impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Cause::Http(error) => error.fmt(f),
Cause::UnusableKey => f.write_str("unusable API key"),
Cause::PlainHttp => f.write_str("refusing to send the key over plain http"),
}
}
}
// Each `Display` here prints the error it wraps, so `source` skips to that error's own
// cause: a reporter that walks the chain would otherwise print one message three times.
impl std::error::Error for TransportError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.0 {
Cause::Http(error) => error.source(),
Cause::UnusableKey | Cause::PlainHttp => None,
}
}
}
impl From<ApiError> for Error {
fn from(error: ApiError) -> Self {
Self::Api(error)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Transport(error) => error.fmt(f),
Self::Api(error) => error.fmt(f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Transport(error) => error.source(),
Self::Api(error) => error.source(),
}
}
}