meta: start refactoring
This commit is contained in:
parent
eb09729f38
commit
a15d380bc9
6 changed files with 228 additions and 163 deletions
|
|
@ -1,15 +1,10 @@
|
|||
use anyhow::Result;
|
||||
use std::time::Instant;
|
||||
use ureq::tls::{TlsConfig, TlsProvider};
|
||||
use ureq::Agent;
|
||||
use url::Url;
|
||||
|
||||
use crate::FeedStore;
|
||||
|
||||
pub struct Fetcher {
|
||||
agent: Agent,
|
||||
pub agent: Agent,
|
||||
/// FROM header for requests
|
||||
from: String,
|
||||
pub from: String,
|
||||
}
|
||||
|
||||
impl Fetcher {
|
||||
|
|
@ -36,41 +31,4 @@ impl Fetcher {
|
|||
from: from.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch(&self, url: Url, feed_store: &FeedStore) -> Result<bool> {
|
||||
let fetchdata = feed_store.load_fetchdata(&url)?;
|
||||
let mut builder = self
|
||||
.agent
|
||||
.get(url.to_string())
|
||||
.header("FROM", self.from.clone());
|
||||
if !fetchdata.etag.is_empty() {
|
||||
builder = builder.header("If-None-Match", fetchdata.etag);
|
||||
}
|
||||
if !fetchdata.last_modified.is_empty() {
|
||||
builder = builder.header("If-Modified-Since", fetchdata.last_modified);
|
||||
}
|
||||
|
||||
let start_instant = Instant::now();
|
||||
let result = builder.call();
|
||||
let duration = start_instant.elapsed();
|
||||
|
||||
let response = result?;
|
||||
debug!(
|
||||
"fetched with status {} in {} ms: {url}",
|
||||
response.status(),
|
||||
duration.as_millis()
|
||||
);
|
||||
let status = response.status();
|
||||
match status.as_u16() {
|
||||
304 => Ok(false), // Not Modified -> nothing to do
|
||||
200 => feed_store.store(&url, response),
|
||||
_ => {
|
||||
warn!(
|
||||
"HTTP Status {} not implemented for {url}",
|
||||
response.status()
|
||||
);
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue