pub struct HttpClient { /* private fields */ }Expand description
A thin wrapper around reqwest::Client shared by all hooksmith service crates.
Service crates (e.g. discord_hook, slack_hook) hold one of these,
configure it at construction time, and call HttpClient::post_json to
fire requests.
TLS configuration is the responsibility of the service crate — build a
reqwest::Client with your chosen TLS backend and pass it in via
HttpClient::with_reqwest.
Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a client backed by a freshly-constructed reqwest::Client.
A 30-second request timeout is applied by default so that a slow or
unresponsive endpoint can never hang your application indefinitely.
Override this by building your own reqwest::Client and passing it
to HttpClient::with_reqwest.
Sourcepub fn with_reqwest(client: Client) -> Self
pub fn with_reqwest(client: Client) -> Self
Wrap an existing reqwest::Client.
Use this to share a connection pool or inject custom configuration (timeouts, proxies, etc.) across your application.
Sourcepub async fn post_json(
&self,
url: &str,
body: &impl Serialize,
) -> Result<Response, Error>
pub async fn post_json( &self, url: &str, body: &impl Serialize, ) -> Result<Response, Error>
POST body serialized as JSON to url and return the raw response.
When the tracing feature is enabled this method emits an
info_span named hooksmith.post_json capturing the request URL,
HTTP status, and wall-clock latency.
Sourcepub async fn post_json_with_retry(
&self,
url: &str,
body: &impl Serialize,
policy: &RetryPolicy,
) -> Result<Response, Error>
pub async fn post_json_with_retry( &self, url: &str, body: &impl Serialize, policy: &RetryPolicy, ) -> Result<Response, Error>
POST body serialized as JSON to url, retrying on failure according
to the supplied RetryPolicy.
Each retry is separated by an exponentially increasing delay
(base_delay × 2ⁿ). When policy.jitter is true a random
fraction of the current step is added to the delay.
Returns the first successful reqwest::Response, or the error from
the final attempt if all attempts are exhausted.
§Example
use hooksmith_core::RetryPolicy;
let policy = RetryPolicy { max_attempts: 4, ..Default::default() };
let resp = client.post_json_with_retry(url, &payload, &policy).await?;Sourcepub fn inner(&self) -> &Client
pub fn inner(&self) -> &Client
Access the underlying reqwest::Client for advanced use-cases.