spider_downloader/
traits.rs1use async_trait::async_trait;
4use bytes::Bytes;
5use http::StatusCode;
6use spider_util::error::SpiderError;
7use spider_util::request::Request;
8use spider_util::response::Response;
9#[cfg(feature = "stream")]
10use spider_util::stream_response::StreamResponse;
11use std::time::Duration;
12
13#[async_trait]
15pub trait SimpleHttpClient: Send + Sync {
16 async fn get_text(
18 &self,
19 url: &str,
20 timeout: Duration,
21 ) -> Result<(StatusCode, Bytes), SpiderError>;
22}
23
24#[async_trait]
26pub trait Downloader: Send + Sync + 'static {
27 type Client: Send + Sync;
28
29 async fn download(&self, request: Request) -> Result<Response, SpiderError>;
32
33 fn client(&self) -> &Self::Client;
35
36 #[cfg(feature = "stream")]
38 async fn download_stream(&self, request: Request) -> Result<StreamResponse, SpiderError> {
39 let response = self.download(request).await?;
41 response
42 .to_stream_response()
43 .await
44 .map_err(|e| SpiderError::IoError(e.to_string()))
45 }
46}
47