Source: Move around code and change some methods signature.
request_constructor is now a Source method. update_etags now takes &mut self instead of mut self. update_etags now uses source setters methods instead of raw fields. changed the naming of into_feed to to_feed according to rust convention.
This commit is contained in:
parent
9dc555cad7
commit
5c5faafc72
@ -51,8 +51,9 @@ impl Source {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set `last_modified` value.
|
/// Set `last_modified` value.
|
||||||
pub fn set_last_modified(&mut self, value: Option<&str>) {
|
pub fn set_last_modified(&mut self, value: Option<String>) {
|
||||||
self.last_modified = value.map(|x| x.to_string());
|
// self.last_modified = value.map(|x| x.to_string());
|
||||||
|
self.last_modified = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the Http Etag Header field.
|
/// Represents the Http Etag Header field.
|
||||||
@ -77,8 +78,7 @@ impl Source {
|
|||||||
|
|
||||||
/// Extract Etag and LastModifier from res, and update self and the
|
/// Extract Etag and LastModifier from res, and update self and the
|
||||||
/// corresponding db row.
|
/// corresponding db row.
|
||||||
// FIXME: With &mut self the closure is of type FnMut instead of FnOnce.
|
fn update_etag(&mut self, res: &Response) -> Result<()> {
|
||||||
fn update_etag(mut self, res: &Response) -> Result<()> {
|
|
||||||
let headers = res.headers();
|
let headers = res.headers();
|
||||||
|
|
||||||
let etag = headers.get::<ETag>();
|
let etag = headers.get::<ETag>();
|
||||||
@ -87,14 +87,21 @@ impl Source {
|
|||||||
if self.http_etag() != etag.map(|x| x.tag()) || self.last_modified != lmod.map(|x| {
|
if self.http_etag() != etag.map(|x| x.tag()) || self.last_modified != lmod.map(|x| {
|
||||||
format!("{}", x)
|
format!("{}", x)
|
||||||
}) {
|
}) {
|
||||||
self.http_etag = etag.map(|x| x.tag().to_string().to_owned());
|
self.set_http_etag(etag.map(|x| x.tag()));
|
||||||
self.last_modified = lmod.map(|x| format!("{}", x));
|
self.set_last_modified(lmod.map(|x| format!("{}", x)));
|
||||||
self.save()?;
|
self.save()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Construct a new `Source` with the given `uri` and index it.
|
||||||
|
///
|
||||||
|
/// This only indexes the `Source` struct, not the Podcast Feed.
|
||||||
|
pub fn from_url(uri: &str) -> Result<Source> {
|
||||||
|
NewSource::new(uri).into_source()
|
||||||
|
}
|
||||||
|
|
||||||
/// `Feed` constructor.
|
/// `Feed` constructor.
|
||||||
///
|
///
|
||||||
/// Fetches the latest xml Feed.
|
/// Fetches the latest xml Feed.
|
||||||
@ -103,13 +110,13 @@ impl Source {
|
|||||||
///
|
///
|
||||||
/// Consumes `self` and Returns the corresponding `Feed` Object.
|
/// Consumes `self` and Returns the corresponding `Feed` Object.
|
||||||
// TODO: Refactor into TryInto once it lands on stable.
|
// TODO: Refactor into TryInto once it lands on stable.
|
||||||
pub fn into_feed(
|
pub fn to_feed(
|
||||||
self,
|
mut self,
|
||||||
client: &Client<HttpsConnector<HttpConnector>>,
|
client: &Client<HttpsConnector<HttpConnector>>,
|
||||||
ignore_etags: bool,
|
ignore_etags: bool,
|
||||||
) -> Box<Future<Item = Feed, Error = Error>> {
|
) -> Box<Future<Item = Feed, Error = Error>> {
|
||||||
let id = self.id();
|
let id = self.id();
|
||||||
let feed = request_constructor(&self, client, ignore_etags)
|
let feed = self.request_constructor(client, ignore_etags)
|
||||||
.map_err(From::from)
|
.map_err(From::from)
|
||||||
.and_then(move |res| {
|
.and_then(move |res| {
|
||||||
self.update_etag(&res)?;
|
self.update_etag(&res)?;
|
||||||
@ -125,41 +132,34 @@ impl Source {
|
|||||||
Box::new(feed)
|
Box::new(feed)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a new `Source` with the given `uri` and index it.
|
// TODO: make ignore_etags an Enum for better ergonomics.
|
||||||
///
|
// #bools_are_just_2variant_enmus
|
||||||
/// This only indexes the `Source` struct, not the Podcast Feed.
|
fn request_constructor(
|
||||||
pub fn from_url(uri: &str) -> Result<Source> {
|
&self,
|
||||||
NewSource::new(uri).into_source()
|
client: &Client<HttpsConnector<HttpConnector>>,
|
||||||
}
|
ignore_etags: bool,
|
||||||
}
|
) -> Box<Future<Item = Response, Error = hyper::Error>> {
|
||||||
|
// FIXME: remove unwrap somehow
|
||||||
|
let uri = Uri::from_str(self.uri()).unwrap();
|
||||||
|
let mut req = Request::new(Method::Get, uri);
|
||||||
|
|
||||||
// TODO: make ignore_etags an Enum for better ergonomics.
|
if !ignore_etags {
|
||||||
// #bools_are_just_2variant_enmus
|
if let Some(foo) = self.http_etag() {
|
||||||
fn request_constructor(
|
req.headers_mut().set(IfNoneMatch::Items(vec![
|
||||||
s: &Source,
|
EntityTag::new(true, foo.to_owned()),
|
||||||
client: &Client<HttpsConnector<HttpConnector>>,
|
]));
|
||||||
ignore_etags: bool,
|
}
|
||||||
) -> Box<Future<Item = Response, Error = hyper::Error>> {
|
|
||||||
// FIXME: remove unwrap somehow
|
|
||||||
let uri = Uri::from_str(&s.uri()).unwrap();
|
|
||||||
let mut req = Request::new(Method::Get, uri);
|
|
||||||
|
|
||||||
if !ignore_etags {
|
if let Some(foo) = self.last_modified() {
|
||||||
if let Some(foo) = s.http_etag() {
|
if let Ok(x) = foo.parse::<HttpDate>() {
|
||||||
req.headers_mut().set(IfNoneMatch::Items(vec![
|
req.headers_mut().set(IfModifiedSince(x));
|
||||||
EntityTag::new(true, foo.to_owned()),
|
}
|
||||||
]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(foo) = s.last_modified() {
|
|
||||||
if let Ok(x) = foo.parse::<HttpDate>() {
|
|
||||||
req.headers_mut().set(IfModifiedSince(x));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let work = client.request(req).map_err(From::from);
|
let work = client.request(req).map_err(From::from);
|
||||||
Box::new(work)
|
Box::new(work)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response_to_channel(res: Response) -> Box<Future<Item = Channel, Error = Error>> {
|
fn response_to_channel(res: Response) -> Box<Future<Item = Channel, Error = Error>> {
|
||||||
@ -191,14 +191,15 @@ fn match_status(code: StatusCode) -> Result<()> {
|
|||||||
StatusCode::TemporaryRedirect => debug!("307: Temporary Redirect."),
|
StatusCode::TemporaryRedirect => debug!("307: Temporary Redirect."),
|
||||||
// TODO: Change the source uri to the new one
|
// TODO: Change the source uri to the new one
|
||||||
StatusCode::MovedPermanently | StatusCode::PermanentRedirect => {
|
StatusCode::MovedPermanently | StatusCode::PermanentRedirect => {
|
||||||
warn!("Feed was moved permanently.")
|
warn!("Feed was moved permanently.");
|
||||||
|
bail!("Feed was moved permanently.")
|
||||||
}
|
}
|
||||||
StatusCode::Unauthorized => bail!("401: Unauthorized."),
|
StatusCode::Unauthorized => bail!("401: Unauthorized."),
|
||||||
StatusCode::Forbidden => bail!("403: Forbidden."),
|
StatusCode::Forbidden => bail!("403: Forbidden."),
|
||||||
StatusCode::NotFound => bail!("404: Not found."),
|
StatusCode::NotFound => bail!("404: Not found."),
|
||||||
StatusCode::RequestTimeout => bail!("408: Request Timeout."),
|
StatusCode::RequestTimeout => bail!("408: Request Timeout."),
|
||||||
StatusCode::Gone => bail!("410: Feed was deleted."),
|
StatusCode::Gone => bail!("410: Feed was deleted."),
|
||||||
_ => (),
|
_ => info!("HTTP StatusCode: {}", code),
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -222,7 +223,7 @@ mod tests {
|
|||||||
let url = "http://www.newrustacean.com/feed.xml";
|
let url = "http://www.newrustacean.com/feed.xml";
|
||||||
let source = Source::from_url(url).unwrap();
|
let source = Source::from_url(url).unwrap();
|
||||||
|
|
||||||
let feed = source.into_feed(&client, true);
|
let feed = source.to_feed(&client, true);
|
||||||
|
|
||||||
assert!(core.run(feed).is_ok());
|
assert!(core.run(feed).is_ok());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,9 +35,8 @@ pub fn pipeline<S: IntoIterator<Item = Source>>(sources: S, ignore_etags: bool)
|
|||||||
|
|
||||||
let list = sources
|
let list = sources
|
||||||
.into_iter()
|
.into_iter()
|
||||||
// FIXME: Make proper indexing futures instead of wrapping up existing
|
.map(|s| s.to_feed(&client, ignore_etags))
|
||||||
// blocking functions
|
.map(|fut| fut.and_then(|feed| feed.index_async()))
|
||||||
.map(|s| s.into_feed(&client, ignore_etags).and_then(|feed| feed.index_async()))
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let f = core.run(collect_futures(list))?;
|
let f = core.run(collect_futures(list))?;
|
||||||
|
|||||||
@ -239,7 +239,7 @@ mod tests {
|
|||||||
// Copy it's id
|
// Copy it's id
|
||||||
let sid = source.id();
|
let sid = source.id();
|
||||||
// Convert Source it into a future Feed and index it
|
// Convert Source it into a future Feed and index it
|
||||||
let future = source.into_feed(&client, true);
|
let future = source.to_feed(&client, true);
|
||||||
let feed = core.run(future).unwrap();
|
let feed = core.run(future).unwrap();
|
||||||
feed.index().unwrap();
|
feed.index().unwrap();
|
||||||
|
|
||||||
|
|||||||
@ -147,7 +147,7 @@ mod tests {
|
|||||||
// Copy it's id
|
// Copy it's id
|
||||||
let sid = source.id();
|
let sid = source.id();
|
||||||
// Convert Source it into a future Feed and index it
|
// Convert Source it into a future Feed and index it
|
||||||
let future = source.into_feed(&client, true);
|
let future = source.to_feed(&client, true);
|
||||||
let feed = core.run(future).unwrap();
|
let feed = core.run(future).unwrap();
|
||||||
feed.index().unwrap();
|
feed.index().unwrap();
|
||||||
|
|
||||||
|
|||||||
@ -104,7 +104,7 @@ mod tests {
|
|||||||
// Copy it's id
|
// Copy it's id
|
||||||
let sid = source.id();
|
let sid = source.id();
|
||||||
// Convert Source it into a future Feed and index it
|
// Convert Source it into a future Feed and index it
|
||||||
let future = source.into_feed(&client, true);
|
let future = source.to_feed(&client, true);
|
||||||
let feed = core.run(future).unwrap();
|
let feed = core.run(future).unwrap();
|
||||||
feed.index().unwrap();
|
feed.index().unwrap();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user