forked from SO4-Markets/so4-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.rs
More file actions
32 lines (28 loc) · 1.16 KB
/
Copy pathhttp.rs
File metadata and controls
32 lines (28 loc) · 1.16 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
//! Shared `reqwest` HTTP client.
//!
//! A single connection-pooled client is reused across all outbound calls
//! (price sources, Stellar RPC, Horizon, Friendbot). Replaces the Cloudflare
//! Worker `worker::Fetch` API now that the oracle runs as a native service.
use std::sync::OnceLock;
use std::time::Duration;
/// Max chars of an HTTP error body retained in error variants / logs.
pub const HTTP_ERROR_BODY_MAX_CHARS: usize = 512;
/// Truncate a response body for inclusion in error types.
pub fn truncate_error_body(body: &str) -> String {
let trimmed = body.trim();
if trimmed.chars().count() <= HTTP_ERROR_BODY_MAX_CHARS {
return trimmed.to_string();
}
trimmed.chars().take(HTTP_ERROR_BODY_MAX_CHARS).collect()
}
/// Returns the process-wide shared HTTP client, initializing it on first use (resolves #355).
pub fn client() -> &'static reqwest::Client {
static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
CLIENT.get_or_init(|| {
reqwest::Client::builder()
.timeout(Duration::from_secs(15))
.user_agent("so4-oracle/0.1")
.build()
.expect("failed to build reqwest client")
})
}