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
mod r#async {
use std::time::Duration;
use http_body_util::{BodyExt, Full};
use hyper::body::Bytes;
use hyper_rustls::HttpsConnector;
use hyper_util::{
client::legacy::{connect::HttpConnector, Client},
rt::TokioExecutor,
};
use serde::de::DeserializeOwned;
use tracing::trace;
use crate::{
api::{Credentials, UnauthenticatedRequest},
error::{Error, MaybeTypedResponse},
};
/// Handles authentication and exposes the Hetzner Robot API functionality
/// with a simple interface.
///
/// # Example
/// an [`AsyncRobot`] using the `HROBOT_USERNAME` and `HROBOT_PASSWORD`
/// environment variables:
/// ```rust
/// # #[tokio::main]
/// # async fn main() {
/// # std::env::set_var("HROBOT_USERNAME", "username");
/// # std::env::set_var("HROBOT_PASSWORD", "password");
/// let robot = hrobot::AsyncRobot::default();
/// # }
/// ```
///
/// If you want to customize the [`hyper::Client`] see:
/// * [`AsyncRobot::from_env`] if you still want to use the environment variables, or
/// * [`AsyncRobot::new`] if you want to provide client and credentials yourself.
///
#[derive(Debug, Clone)]
pub struct AsyncRobot {
credentials: Credentials,
client: Client<HttpsConnector<HttpConnector>, Full<Bytes>>,
}
impl Default for AsyncRobot {
fn default() -> Self {
let https: HttpsConnector<HttpConnector> = hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
.https_only()
.enable_http1()
.build();
let client = Client::builder(TokioExecutor::new())
.pool_idle_timeout(Some(Duration::from_secs(15)))
.build(https);
Self::from_env(client).unwrap()
}
}
impl AsyncRobot {
/// Construct a new [`AsyncRobot`] using the environment variables
/// `HROBOT_USERNAME` and `HROBOT_PASSWORD` for credentials,
/// and the given client.
///
/// # Example
/// Construct an [`AsyncRobot`] using a [`hyper_util::client::legacy::Client`] and [`hyper_rustls`].
/// ```rust
/// # #[tokio::main]
/// # async fn main() {
/// let https = hyper_rustls::HttpsConnectorBuilder::new()
/// .with_webpki_roots()
/// .https_only()
/// .enable_http1()
/// .build();
///
/// let client = hyper_util::client::legacy::Client::builder(
/// hyper_util::rt::TokioExecutor::new()
/// ).build(https);
///
/// let robot = hrobot::AsyncRobot::from_env(client);
/// # }
/// ```
pub fn from_env(
client: Client<HttpsConnector<HttpConnector>, Full<Bytes>>,
) -> Result<Self, std::env::VarError> {
Ok(Self::new(
client,
&std::env::var("HROBOT_USERNAME")?,
&std::env::var("HROBOT_PASSWORD")?,
))
}
/// Construct a new [`AsyncRobot`], using the given client, username and password.
///
/// # Example
/// Construct an [`AsyncRobot`] using a custom [`hyper_util::client::legacy::Client`].
/// ```rust
/// # #[tokio::main]
/// # async fn main() {
/// let https = hyper_rustls::HttpsConnectorBuilder::new()
/// .with_webpki_roots()
/// .https_only()
/// .enable_http1()
/// .build();
///
/// let client = hyper_util::client::legacy::Client::builder(
/// hyper_util::rt::TokioExecutor::new()
/// ).build(https);
///
/// let robot = hrobot::AsyncRobot::new(client, "#ws+username", "p@ssw0rd");
/// # }
/// ```
pub fn new(
client: Client<HttpsConnector<HttpConnector>, Full<Bytes>>,
username: &str,
password: &str,
) -> Self {
AsyncRobot {
credentials: Credentials::new(username, password),
client,
}
}
/// Construct a new [`AsyncRobot`], using the default [`hyper_util::client::legacy::Client`]
/// and the provided username and password.
///
/// # Example
/// Construct an [`AsyncRobot`] using a given username and password
/// ```rust
/// # #[tokio::main]
/// # async fn main() {
/// let robot = hrobot::AsyncRobot::new_with_default_client("#ws+username", "p@ssw0rd");
/// # }
/// ```
pub fn new_with_default_client(username: &str, password: &str) -> Self {
let https: HttpsConnector<HttpConnector> = hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
.https_only()
.enable_http1()
.build();
let client = Client::builder(TokioExecutor::new()).build(https);
Self::new(client, username, password)
}
/// Shorthand for authenticating and sending the request.
#[tracing::instrument]
pub(crate) async fn go<Response: DeserializeOwned + Send + 'static>(
&self,
request: UnauthenticatedRequest<Response>,
) -> Result<Response, Error> {
trace!("{request:?}");
let authenticated_request = request.authenticate(&self.credentials);
let body = match authenticated_request.body() {
None => Full::default(),
Some(value) => Full::from(value.to_owned()),
};
let request = hyper::Request::builder()
.uri(authenticated_request.uri())
.method(authenticated_request.method())
.header(
"Authorization",
authenticated_request.authorization_header(),
)
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.body(body)
.map_err(Error::transport)?;
let response = self
.client
.request(request)
.await
.map_err(Error::transport)?;
let body = response
.into_body()
.collect()
.await
.map_err(Error::transport)?
.to_bytes();
let stringified = String::from_utf8_lossy(&body);
trace!("response body: {stringified}");
// We do explicit deserialization here, since some endpoints can return empty responses.
//
// I initialize used a #[derive(Serialize, Deserialize)] enum which encapsulated both success and
// error states, but deserializing an untagged enum, even when the encapsulated "Ok"
// result is just a unit type (), deserialization will fail on empty input.
if let Ok(result) = serde_json::from_str::<Response>(&stringified) {
Ok(result)
} else {
match serde_json::from_str::<MaybeTypedResponse>(&stringified) {
Ok(api_error) => Err(Error::Api(api_error.error.into())),
Err(serde) => Err(Error::Deserialization(serde)),
}
}
}
}
}
pub use r#async::*;