Skip to main content

HttpClient

Trait HttpClient 

Source
pub trait HttpClient: Send + Sync {
    // Required methods
    fn get<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        headers: &'life2 HashMap<String, String>,
    ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn post<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        headers: &'life2 HashMap<String, String>,
        body: Vec<u8>,
    ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn name(&self) -> &str;

    // Provided method
    fn default_headers(&self) -> HashMap<String, String> { ... }
}
Expand description

HTTP 客户端抽象 trait

实现此 trait 即可接入框架,例如:

  • ReqwestClient(默认,基于 reqwest)
  • WreqClient(基于 wreq)
  • Chrome CDP 客户端
  • Mock 客户端(用于测试)

Required Methods§

Source

fn get<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, headers: &'life2 HashMap<String, String>, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

发送 GET 请求

Source

fn post<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, headers: &'life2 HashMap<String, String>, body: Vec<u8>, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

发送 POST 请求

Source

fn name(&self) -> &str

返回客户端名称(用于日志/调试)

Provided Methods§

Source

fn default_headers(&self) -> HashMap<String, String>

返回客户端默认请求头(如 User-Agent)

Collector 会在调用 on_request 回调前将这些默认头合并到请求中, 确保回调能完整看到所有将要发送的请求头。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§