mod client;
mod config;
mod error;
mod facade;
mod request;
mod response;
pub use client::HttpClient;
pub use config::{HttpClientConfig, HttpClientConfigBuilder, ProxyConfig, RetryStrategy};
pub use error::HttpError;
pub use response::Response;
pub use request::{
CancellableRequest, CancellationToken, CancellationTrigger,
Method, RequestBuilder, cancellation_pair,
};
pub use facade::{
delete, get, get_bytes, get_json, get_text, head, patch_json, post_form, post_json,
post_json_for_json, put_json,
};
use async_trait::async_trait;
use unistore_core::{Capability, CapabilityError, CapabilityInfo};
pub struct HttpCapability {
client: Option<HttpClient>,
config: HttpClientConfig,
}
impl HttpCapability {
pub fn new() -> Self {
Self {
client: None,
config: HttpClientConfig::default(),
}
}
pub fn with_config(config: HttpClientConfig) -> Self {
Self {
client: None,
config,
}
}
pub fn client(&self) -> Option<&HttpClient> {
self.client.as_ref()
}
}
impl Default for HttpCapability {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Capability for HttpCapability {
fn info(&self) -> CapabilityInfo {
CapabilityInfo::new("http", env!("CARGO_PKG_VERSION"))
.with_description("HTTP client capability with retry and timeout support")
.with_author("UniStore Team")
}
async fn start(&mut self) -> Result<(), CapabilityError> {
if self.client.is_some() {
return Ok(()); }
let client = HttpClient::with_config(self.config.clone())
.map_err(|e| CapabilityError::start_failed("http", e.to_string()))?;
self.client = Some(client);
Ok(())
}
async fn stop(&mut self) -> Result<(), CapabilityError> {
self.client = None;
Ok(())
}
async fn health_check(&self) -> Result<(), CapabilityError> {
if self.client.is_none() {
return Err(CapabilityError::unhealthy("http", "客户端未初始化"));
}
Ok(())
}
}