Skip to main content

anodizer_core/
http.rs

1//! HTTP client helpers shared by every stage that talks to a remote.
2//!
3//! All anodizer HTTP traffic should go through `blocking_client(...)` so that
4//! the `User-Agent`, default-roots, and timeout policy stay consistent across
5//! publishers, announcers, and the release backends.
6
7use std::time::Duration;
8
9use anyhow::{Context as _, Result};
10
11/// Canonical user-agent string sent with every anodizer HTTP request.
12///
13/// Versioning the UA matters for upstream services that rate-limit or
14/// fingerprint by client identity (Discourse, Reddit, GitHub, etc.).
15pub const USER_AGENT: &str = concat!("anodizer/", env!("CARGO_PKG_VERSION"));
16
17/// Build a blocking `reqwest::Client` configured with the canonical UA,
18/// the requested per-request timeout, and the platform's built-in roots.
19pub fn blocking_client(timeout: Duration) -> Result<reqwest::blocking::Client> {
20    reqwest::blocking::Client::builder()
21        .user_agent(USER_AGENT)
22        .timeout(timeout)
23        .build()
24        .context("build blocking HTTP client")
25}
26
27/// Async equivalent of `blocking_client`.
28pub fn async_client(timeout: Duration) -> Result<reqwest::Client> {
29    reqwest::Client::builder()
30        .user_agent(USER_AGENT)
31        .timeout(timeout)
32        .build()
33        .context("build async HTTP client")
34}