pub struct HttpClient { /* private fields */ }Expand description
HTTP 客户端
封装 reqwest,提供:
- 合理的默认配置
- 自动重试
- 与取消系统集成
§使用层级
- Level 3 - 便捷函数:
get(),post_json() - Level 2 - 客户端实例:
HttpClient::new(),HttpClient::builder() - Level 1 - 底层访问:
client.inner()获取 reqwest::Client
§Example
ⓘ
// 默认配置
let client = HttpClient::new()?;
let resp = client.get("https://api.example.com/data").await?;
// 自定义配置
let client = HttpClient::builder()
.timeout(Duration::from_secs(60))
.max_retries(5)
.build_client()?;Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn with_config(config: HttpClientConfig) -> Result<Self, HttpError>
pub fn with_config(config: HttpClientConfig) -> Result<Self, HttpError>
使用自定义配置创建客户端
Sourcepub fn builder() -> HttpClientConfigBuilder
pub fn builder() -> HttpClientConfigBuilder
创建配置构建器
Sourcepub fn config(&self) -> &HttpClientConfig
pub fn config(&self) -> &HttpClientConfig
获取当前配置
Sourcepub async fn post_json<T: Serialize>(
&self,
url: &str,
body: &T,
) -> Result<Response, HttpError>
pub async fn post_json<T: Serialize>( &self, url: &str, body: &T, ) -> Result<Response, HttpError>
POST 请求(JSON body)
Sourcepub async fn post_form<T: Serialize>(
&self,
url: &str,
body: &T,
) -> Result<Response, HttpError>
pub async fn post_form<T: Serialize>( &self, url: &str, body: &T, ) -> Result<Response, HttpError>
POST 请求(表单 body)
Sourcepub async fn put_json<T: Serialize>(
&self,
url: &str,
body: &T,
) -> Result<Response, HttpError>
pub async fn put_json<T: Serialize>( &self, url: &str, body: &T, ) -> Result<Response, HttpError>
PUT 请求(JSON body)
Sourcepub async fn patch_json<T: Serialize>(
&self,
url: &str,
body: &T,
) -> Result<Response, HttpError>
pub async fn patch_json<T: Serialize>( &self, url: &str, body: &T, ) -> Result<Response, HttpError>
PATCH 请求(JSON body)
Sourcepub fn request(&self, method: Method, url: &str) -> RequestBuilder
pub fn request(&self, method: Method, url: &str) -> RequestBuilder
创建请求构建器
Sourcepub async fn execute(
&self,
builder: RequestBuilder,
) -> Result<Response, HttpError>
pub async fn execute( &self, builder: RequestBuilder, ) -> Result<Response, HttpError>
执行请求(带自动重试)
Sourcepub async fn execute_cancellable(
&self,
request: CancellableRequest,
) -> Result<Response, HttpError>
pub async fn execute_cancellable( &self, request: CancellableRequest, ) -> Result<Response, HttpError>
执行可取消的请求
Trait Implementations§
Source§impl Clone for HttpClient
impl Clone for HttpClient
Source§fn clone(&self) -> HttpClient
fn clone(&self) -> HttpClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for HttpClient
impl Debug for HttpClient
Auto Trait Implementations§
impl Freeze for HttpClient
impl !RefUnwindSafe for HttpClient
impl Send for HttpClient
impl Sync for HttpClient
impl Unpin for HttpClient
impl !UnwindSafe for HttpClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more