0.0.2
This commit is contained in:
73
kb_lib/src/http_client.rs
Normal file
73
kb_lib/src/http_client.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// file: kb_lib/src/http_client.rs
|
||||
|
||||
//! Generic asynchronous HTTP client skeleton.
|
||||
//!
|
||||
//! The transport is intentionally minimal in `0.0.2`. Endpoint binding and
|
||||
//! client construction are stabilized now, while JSON-RPC request execution,
|
||||
//! throttling, and batching are scheduled for `0.4.x`.
|
||||
|
||||
/// Generic asynchronous HTTP client placeholder.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct HttpClient {
|
||||
endpoint: crate::KbHttpEndpointConfig,
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl HttpClient {
|
||||
/// Creates a new HTTP client bound to a named endpoint configuration.
|
||||
pub fn new(endpoint: crate::KbHttpEndpointConfig) -> Result<Self, crate::KbError> {
|
||||
if endpoint.name.trim().is_empty() {
|
||||
return Err(crate::KbError::Config(
|
||||
"http client endpoint name must not be empty".to_string(),
|
||||
));
|
||||
}
|
||||
let builder = reqwest::Client::builder()
|
||||
.connect_timeout(std::time::Duration::from_millis(
|
||||
endpoint.connect_timeout_ms,
|
||||
))
|
||||
.timeout(std::time::Duration::from_millis(
|
||||
endpoint.request_timeout_ms,
|
||||
));
|
||||
let client_result = builder.build();
|
||||
let client = match client_result {
|
||||
Ok(client) => client,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Http(format!(
|
||||
"cannot build reqwest client for endpoint '{}': {error}",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(Self { endpoint, client })
|
||||
}
|
||||
|
||||
/// Returns the endpoint name of this client.
|
||||
pub fn endpoint_name(&self) -> &str {
|
||||
&self.endpoint.name
|
||||
}
|
||||
|
||||
/// Returns the endpoint URL of this client.
|
||||
pub fn endpoint_url(&self) -> &str {
|
||||
&self.endpoint.url
|
||||
}
|
||||
|
||||
/// Returns the endpoint configuration of this client.
|
||||
pub fn endpoint_config(&self) -> &crate::KbHttpEndpointConfig {
|
||||
&self.endpoint
|
||||
}
|
||||
|
||||
/// Returns the underlying reqwest client reference.
|
||||
pub fn raw_client(&self) -> &reqwest::Client {
|
||||
&self.client
|
||||
}
|
||||
|
||||
/// Sends a JSON-RPC payload.
|
||||
pub async fn send_json_rpc_request(
|
||||
&self,
|
||||
_payload: &serde_json::Value,
|
||||
) -> Result<serde_json::Value, crate::KbError> {
|
||||
Err(crate::KbError::NotImplemented(
|
||||
"HttpClient::send_json_rpc_request is scheduled for version 0.4.x".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user