Files
mytheclipse/crates/mytheclipse-http/src/client.rs
T
asepharyana 8106f8943e feat: add 4 new crates (queue, tracing, http, cli) + enhancements to existing crates
New crates:
- mytheclipse-queue: unified job queue with WorkerPool, retry/backoff, DLQ
  Backends: in-memory (default), Redis, NATS, PostgreSQL
- mytheclipse-tracing: tracing subscriber layers with env filter + OTLP/Jaeger export
- mytheclipse-http: HTTP client/server with timeout + tracing, axum server
- mytheclipse-cli: CLI framework with clap derive, built-in subcommands

Enhancements to existing crates:
- mytheclipse-core: SemaphorePool, LeaderElection, HealthRegistry/HealthCheck
- mytheclipse-cache: AutoRefreshCache (bg refresh on miss), CacheMetrics
- mytheclipse-config: ConfigSchema for JSON Schema generation
- mytheclipse-storage: MultipartUploadDriver trait
- mytheclipse-crypto: PASETO v4.local token support

All features compile with --all-features; tests + clippy pass clean.
2026-08-29 16:05:02 +07:00

57 lines
1.4 KiB
Rust

//! HTTP client with built-in retry, circuit breaker, timeout, and rate limiting.
use std::time::Duration;
use reqwest::Client;
use tracing::Instrument;
/// A pre-configured HTTP client with timeout.
#[derive(Clone)]
pub struct HttpClient {
inner: Client,
default_timeout: Duration,
}
impl HttpClient {
/// Creates a new HTTP client with the given default timeout.
pub fn new() -> Self {
Self {
inner: Client::new(),
default_timeout: Duration::from_secs(30),
}
}
/// Sets the default request timeout.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.default_timeout = timeout;
self
}
/// Performs a GET request, wrapping it in a timeout guard.
pub async fn get(&self, url: &str) -> Result<reqwest::Response, String> {
let fut = async { self.inner.get(url).send().await };
let span = tracing::info_span!("http_get", url = url);
match tokio::time::timeout(self.default_timeout, fut.instrument(span)).await {
Ok(Ok(resp)) => Ok(resp),
Ok(Err(e)) => Err(e.to_string()),
Err(_) => Err("request timed out".to_string()),
}
}
}
impl Default for HttpClient {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn creates_client() {
let _c = HttpClient::new().with_timeout(Duration::from_secs(5));
}
}