nautilus_network/http.rs
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------
//! A high-performance HTTP client implementation.
use std::{collections::HashMap, hash::Hash, sync::Arc, time::Duration};
use bytes::Bytes;
use reqwest::{
header::{HeaderMap, HeaderName},
Method, Response, Url,
};
use crate::ratelimiter::{clock::MonotonicClock, RateLimiter};
/// Represents the HTTP methods supported by the `HttpClient`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.network")
)]
pub enum HttpMethod {
GET,
POST,
PUT,
DELETE,
PATCH,
}
#[allow(clippy::from_over_into)]
impl Into<Method> for HttpMethod {
fn into(self) -> Method {
match self {
Self::GET => Method::GET,
Self::POST => Method::POST,
Self::PUT => Method::PUT,
Self::DELETE => Method::DELETE,
Self::PATCH => Method::PATCH,
}
}
}
/// Represents the response from an HTTP request.
///
/// This struct encapsulates the status, headers, and body of an HTTP response,
/// providing easy access to the key components of the response.
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.network")
)]
pub struct HttpResponse {
/// The HTTP status code returned by the server.
pub status: u16,
/// The headers returned by the server as a map of key-value pairs.
pub(crate) headers: HashMap<String, String>,
/// The body of the response as raw bytes.
pub(crate) body: Bytes,
}
/// A high-performance HTTP client with rate limiting and timeout capabilities.
///
/// This struct is designed to handle HTTP requests efficiently, providing
/// support for rate limiting, timeouts, and custom headers. The client is
/// built on top of `reqwest` and can be used for both synchronous and
/// asynchronous HTTP requests.
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.network")
)]
pub struct HttpClient {
/// The rate limiter to control the request rate.
pub(crate) rate_limiter: Arc<RateLimiter<String, MonotonicClock>>,
/// The underlying HTTP client used to make requests.
pub(crate) client: InnerHttpClient,
}
/// Represents errors that can occur when using the `HttpClient`.
///
/// This enum provides variants for general HTTP errors and timeout errors,
/// allowing for more granular error handling.
#[derive(thiserror::Error, Debug)]
pub enum HttpClientError {
#[error("HTTP error occurred: {0}")]
Error(String),
#[error("HTTP request timed out: {0}")]
TimeoutError(String),
}
impl From<reqwest::Error> for HttpClientError {
fn from(source: reqwest::Error) -> Self {
if source.is_timeout() {
Self::TimeoutError(source.to_string())
} else {
Self::Error(source.to_string())
}
}
}
impl From<String> for HttpClientError {
fn from(value: String) -> Self {
Self::Error(value)
}
}
/// A high-performance `HttpClient` for HTTP requests.
///
/// The client is backed by a hyper Client which keeps connections alive and
/// can be cloned cheaply. The client also has a list of header fields to
/// extract from the response.
///
/// The client returns an [`HttpResponse`]. The client filters only the key value
/// for the give `header_keys`.
#[derive(Clone)]
pub struct InnerHttpClient {
pub(crate) client: reqwest::Client,
pub(crate) header_keys: Vec<String>,
}
impl InnerHttpClient {
/// Sends an HTTP request with the specified method, URL, headers, and body.
///
/// - `method`: The HTTP method to use (e.g., GET, POST).
/// - `url`: The URL to send the request to.
/// - `headers`: A map of header key-value pairs to include in the request.
/// - `body`: An optional body for the request, represented as a byte vector.
/// - `timeout_secs`: An optional timeout for the request in seconds.
pub async fn send_request(
&self,
method: Method,
url: String,
headers: HashMap<String, String>,
body: Option<Vec<u8>>,
timeout_secs: Option<u64>,
) -> Result<HttpResponse, HttpClientError> {
let reqwest_url = Url::parse(url.as_str())
.map_err(|e| HttpClientError::from(format!("URL parse error: {e}")))?;
let mut header_map = HeaderMap::new();
for (header_key, header_value) in &headers {
let key = HeaderName::from_bytes(header_key.as_bytes())
.map_err(|e| HttpClientError::from(format!("Invalid header name: {e}")))?;
let _ = header_map.insert(
key,
header_value
.parse()
.map_err(|e| HttpClientError::from(format!("Invalid header value: {e}")))?,
);
}
let mut request_builder = self.client.request(method, reqwest_url).headers(header_map);
if let Some(timeout_secs) = timeout_secs {
request_builder = request_builder.timeout(Duration::new(timeout_secs, 0));
}
let request = match body {
Some(b) => request_builder
.body(b)
.build()
.map_err(HttpClientError::from)?,
None => request_builder.build().map_err(HttpClientError::from)?,
};
tracing::trace!("{request:?}");
let response = self
.client
.execute(request)
.await
.map_err(HttpClientError::from)?;
self.to_response(response).await
}
/// Converts a `reqwest::Response` into an `HttpResponse`.
pub async fn to_response(&self, response: Response) -> Result<HttpResponse, HttpClientError> {
tracing::trace!("{response:?}");
let headers: HashMap<String, String> = self
.header_keys
.iter()
.filter_map(|key| response.headers().get(key).map(|val| (key, val)))
.filter_map(|(key, val)| val.to_str().map(|v| (key, v)).ok())
.map(|(k, v)| (k.clone(), v.to_owned()))
.collect();
let status = response.status().as_u16();
let body = response.bytes().await.map_err(HttpClientError::from)?;
Ok(HttpResponse {
status,
headers,
body,
})
}
}
impl Default for InnerHttpClient {
/// Creates a new default [`InnerHttpClient`] instance.
///
/// The default client is initialized with an empty list of header keys and a new `reqwest::Client`.
fn default() -> Self {
let client = reqwest::Client::new();
Self {
client,
header_keys: Default::default(),
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use std::net::{SocketAddr, TcpListener};
use axum::{
routing::{delete, get, patch, post},
serve, Router,
};
use http::status::StatusCode;
use super::*;
fn get_unique_port() -> u16 {
// Create a temporary TcpListener to get an available port
let listener =
TcpListener::bind("127.0.0.1:0").expect("Failed to bind temporary TcpListener");
let port = listener.local_addr().unwrap().port();
// Close the listener to free up the port
drop(listener);
port
}
fn create_router() -> Router {
Router::new()
.route("/get", get(|| async { "hello-world!" }))
.route("/post", post(|| async { StatusCode::OK }))
.route("/patch", patch(|| async { StatusCode::OK }))
.route("/delete", delete(|| async { StatusCode::OK }))
}
async fn start_test_server() -> Result<SocketAddr, Box<dyn std::error::Error + Send + Sync>> {
let port = get_unique_port();
let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}"))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
serve(listener, create_router()).await.unwrap();
});
Ok(addr)
}
#[tokio::test]
async fn test_get() {
let addr = start_test_server().await.unwrap();
let url = format!("http://{addr}");
let client = InnerHttpClient::default();
let response = client
.send_request(
reqwest::Method::GET,
format!("{url}/get"),
HashMap::new(),
None,
None,
)
.await
.unwrap();
assert_eq!(response.status, StatusCode::OK);
assert_eq!(String::from_utf8_lossy(&response.body), "hello-world!");
}
#[tokio::test]
async fn test_post() {
let addr = start_test_server().await.unwrap();
let url = format!("http://{addr}");
let client = InnerHttpClient::default();
let response = client
.send_request(
reqwest::Method::POST,
format!("{url}/post"),
HashMap::new(),
None,
None,
)
.await
.unwrap();
assert_eq!(response.status, StatusCode::OK);
}
#[tokio::test]
async fn test_post_with_body() {
let addr = start_test_server().await.unwrap();
let url = format!("http://{addr}");
let client = InnerHttpClient::default();
let mut body = HashMap::new();
body.insert(
"key1".to_string(),
serde_json::Value::String("value1".to_string()),
);
body.insert(
"key2".to_string(),
serde_json::Value::String("value2".to_string()),
);
let body_string = serde_json::to_string(&body).unwrap();
let body_bytes = body_string.into_bytes();
let response = client
.send_request(
reqwest::Method::POST,
format!("{url}/post"),
HashMap::new(),
Some(body_bytes),
None,
)
.await
.unwrap();
assert_eq!(response.status, StatusCode::OK);
}
#[tokio::test]
async fn test_patch() {
let addr = start_test_server().await.unwrap();
let url = format!("http://{addr}");
let client = InnerHttpClient::default();
let response = client
.send_request(
reqwest::Method::PATCH,
format!("{url}/patch"),
HashMap::new(),
None,
None,
)
.await
.unwrap();
assert_eq!(response.status, StatusCode::OK);
}
#[tokio::test]
async fn test_delete() {
let addr = start_test_server().await.unwrap();
let url = format!("http://{addr}");
let client = InnerHttpClient::default();
let response = client
.send_request(
reqwest::Method::DELETE,
format!("{url}/delete"),
HashMap::new(),
None,
None,
)
.await
.unwrap();
assert_eq!(response.status, StatusCode::OK);
}
}