mirror of
https://gitlab.com/news-flash/article_scraper.git
synced 2025-07-07 16:15:32 +02:00
impl from reqwest error
This commit is contained in:
parent
d62aa8c31a
commit
be40383b1a
2 changed files with 8 additions and 6 deletions
|
@ -23,3 +23,9 @@ pub enum ImageDownloadError {
|
|||
#[error("Unknown Error")]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for ImageDownloadError {
|
||||
fn from(_value: reqwest::Error) -> Self {
|
||||
Self::Http
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,11 +14,7 @@ pub struct ImageRequest {
|
|||
|
||||
impl ImageRequest {
|
||||
pub async fn new(url: String, client: &Client) -> Result<Self, ImageDownloadError> {
|
||||
let response = client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ImageDownloadError::Http)?;
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
let content_type = Self::get_content_type(&response)?;
|
||||
let content_length = Self::get_content_length(&response)?;
|
||||
|
@ -40,7 +36,7 @@ impl ImageRequest {
|
|||
|
||||
let mut result = Vec::with_capacity(self.content_length);
|
||||
while let Some(item) = stream.next().await {
|
||||
let chunk = item.map_err(|_| ImageDownloadError::Http)?;
|
||||
let chunk = item?;
|
||||
_ = tx.send(chunk.len()).await;
|
||||
for byte in chunk {
|
||||
result.push(byte);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue