|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use feed_rs::parser::Parser; |
| 5 | + |
| 6 | +use crate::types::Feed; |
| 7 | + |
| 8 | +pub type ParseResult<T> = std::result::Result<T, ParserError>; |
| 9 | + |
| 10 | +#[derive(Debug, thiserror::Error)] |
| 11 | +pub enum ParserError { |
| 12 | + #[error("fetch failed")] |
| 13 | + Fetch(#[from] reqwest::Error), |
| 14 | + #[error("response size limit exceeded")] |
| 15 | + ResponseLimitExceed, |
| 16 | + |
| 17 | + #[error(transparent)] |
| 18 | + Other(#[from] anyhow::Error), |
| 19 | +} |
| 20 | + |
| 21 | +#[async_trait] |
| 22 | +pub trait FetchFeed: Send + Sync { |
| 23 | + async fn fetch(&self, url: String) -> ParseResult<Feed>; |
| 24 | +} |
| 25 | + |
| 26 | +/// Feed Process entry point |
| 27 | +pub struct FeedService { |
| 28 | + http: reqwest::Client, |
| 29 | + buff_limit: usize, |
| 30 | +} |
| 31 | + |
| 32 | +#[async_trait] |
| 33 | +impl FetchFeed for FeedService { |
| 34 | + async fn fetch(&self, url: String) -> ParseResult<Feed> { |
| 35 | + use futures::StreamExt; |
| 36 | + let mut stream = self |
| 37 | + .http |
| 38 | + .get(&url) |
| 39 | + .send() |
| 40 | + .await |
| 41 | + .map_err(ParserError::Fetch)? |
| 42 | + .error_for_status() |
| 43 | + .map_err(ParserError::Fetch)? |
| 44 | + .bytes_stream(); |
| 45 | + |
| 46 | + let mut buff = Vec::new(); |
| 47 | + |
| 48 | + while let Some(chunk) = stream.next().await { |
| 49 | + let chunk = chunk.map_err(ParserError::Fetch)?; |
| 50 | + if buff.len() + chunk.len() > self.buff_limit { |
| 51 | + return Err(ParserError::ResponseLimitExceed); |
| 52 | + } |
| 53 | + buff.extend(chunk); |
| 54 | + } |
| 55 | + |
| 56 | + self.parse(url, buff.as_slice()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl FeedService { |
| 61 | + pub fn new(user_agent: &str, buff_limit: usize) -> Self { |
| 62 | + let http = reqwest::ClientBuilder::new() |
| 63 | + .user_agent(user_agent) |
| 64 | + .timeout(Duration::from_secs(10)) |
| 65 | + .connect_timeout(Duration::from_secs(10)) |
| 66 | + .build() |
| 67 | + .unwrap(); |
| 68 | + |
| 69 | + Self { http, buff_limit } |
| 70 | + } |
| 71 | + |
| 72 | + pub fn parse<S>(&self, url: impl Into<String>, source: S) -> ParseResult<Feed> |
| 73 | + where |
| 74 | + S: std::io::Read, |
| 75 | + { |
| 76 | + let url = url.into(); |
| 77 | + let parser = self.build_parser(&url); |
| 78 | + |
| 79 | + match parser.parse(source) { |
| 80 | + Ok(feed) => Ok(Feed::from((url, feed))), |
| 81 | + // TODO: handle error |
| 82 | + Err(err) => Err(ParserError::Other(err.into())), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn build_parser(&self, base_uri: impl AsRef<str>) -> Parser { |
| 87 | + feed_rs::parser::Builder::new() |
| 88 | + .base_uri(Some(base_uri)) |
| 89 | + .build() |
| 90 | + } |
| 91 | +} |
0 commit comments