crawlkit_core/client.rs
1//! HTTP 客户端抽象 trait
2//!
3//! `HttpClient` trait 定义了所有 HTTP 后端必须实现的统一接口。
4//! 具体的实现(如 ReqwestClient、WreqClient、ChromeClient)位于 `crawlkit-fetcher` crate。
5
6use async_trait::async_trait;
7use std::collections::HashMap;
8
9use crate::error::Result;
10use crate::response::Response;
11
12/// HTTP 客户端抽象 trait
13///
14/// 实现此 trait 即可接入框架,例如:
15/// - `ReqwestClient`(默认,基于 reqwest)
16/// - `WreqClient`(基于 wreq)
17/// - Chrome CDP 客户端
18/// - Mock 客户端(用于测试)
19#[async_trait]
20pub trait HttpClient: Send + Sync {
21 /// 发送 GET 请求
22 async fn get(&self, url: &str, headers: &HashMap<String, String>) -> Result<Response>;
23
24 /// 发送 POST 请求
25 async fn post(
26 &self,
27 url: &str,
28 headers: &HashMap<String, String>,
29 body: Vec<u8>,
30 ) -> Result<Response>;
31
32 /// 返回客户端名称(用于日志/调试)
33 fn name(&self) -> &str;
34
35 /// 返回客户端默认请求头(如 User-Agent)
36 ///
37 /// Collector 会在调用 `on_request` 回调前将这些默认头合并到请求中,
38 /// 确保回调能完整看到所有将要发送的请求头。
39 fn default_headers(&self) -> HashMap<String, String> {
40 HashMap::new()
41 }
42}