Minor cleanup
This commit is contained in:
parent
771b7b3804
commit
1dd25f91fd
@ -8,6 +8,12 @@ use reqwest::header::{ETag, LastModified};
|
|||||||
use rss::Channel;
|
use rss::Channel;
|
||||||
use hyper;
|
use hyper;
|
||||||
|
|
||||||
|
// use futures::{future, Future, Stream};
|
||||||
|
// use hyper::Client;
|
||||||
|
// use hyper::client::HttpConnector;
|
||||||
|
// use hyper::Method;
|
||||||
|
// use hyper::Uri;
|
||||||
|
|
||||||
use schema::{episode, podcast, source};
|
use schema::{episode, podcast, source};
|
||||||
use feed::Feed;
|
use feed::Feed;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
@ -611,6 +617,14 @@ impl<'a> Source {
|
|||||||
self.http_etag = value.map(|x| x.to_string());
|
self.http_etag = value.map(|x| x.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
pub fn save(&self) -> Result<Source> {
|
||||||
|
let db = connection();
|
||||||
|
let tempdb = db.get()?;
|
||||||
|
|
||||||
|
Ok(self.save_changes::<Source>(&*tempdb)?)
|
||||||
|
}
|
||||||
|
|
||||||
/// Extract Etag and LastModifier from req, and update self and the
|
/// Extract Etag and LastModifier from req, and update self and the
|
||||||
/// corresponding db row.
|
/// corresponding db row.
|
||||||
fn update_etag(&mut self, req: &reqwest::Response) -> Result<()> {
|
fn update_etag(&mut self, req: &reqwest::Response) -> Result<()> {
|
||||||
@ -648,14 +662,6 @@ impl<'a> Source {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to easily save/"sync" current state of self to the Database.
|
|
||||||
pub fn save(&self) -> Result<Source> {
|
|
||||||
let db = connection();
|
|
||||||
let tempdb = db.get()?;
|
|
||||||
|
|
||||||
Ok(self.save_changes::<Source>(&*tempdb)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `Feed` constructor.
|
/// `Feed` constructor.
|
||||||
///
|
///
|
||||||
/// Fetches the latest xml Feed.
|
/// Fetches the latest xml Feed.
|
||||||
|
|||||||
@ -1,62 +1,48 @@
|
|||||||
|
use rss;
|
||||||
|
|
||||||
use hyper;
|
use hyper;
|
||||||
use std::io::{self, Write};
|
|
||||||
use std::str::FromStr;
|
|
||||||
use futures::{Future, Stream};
|
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
use hyper::client::HttpConnector;
|
use hyper::client::HttpConnector;
|
||||||
use hyper::Method;
|
use hyper::Method;
|
||||||
use hyper::Uri;
|
use hyper::Uri;
|
||||||
use tokio_core::reactor::Core;
|
|
||||||
use hyper_tls::HttpsConnector;
|
use hyper_tls::HttpsConnector;
|
||||||
use rss;
|
|
||||||
// use errors::*;
|
|
||||||
// use hyper::header::{ETag, LastModified};
|
// use hyper::header::{ETag, LastModified};
|
||||||
|
// use hyper::header::{ETag, LastModified};
|
||||||
|
|
||||||
|
use futures::{Future, Stream};
|
||||||
// use futures::future::join_all;
|
// use futures::future::join_all;
|
||||||
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
|
// use std::io::{self, Write};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use Source;
|
use Source;
|
||||||
|
// use errors::*;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn foo() {
|
fn request_constructor(
|
||||||
let uri = "https://www.rust-lang.org/".parse().unwrap();
|
s: &Source,
|
||||||
let mut core = Core::new().unwrap();
|
|
||||||
let handle = core.handle();
|
|
||||||
let client = Client::configure()
|
|
||||||
.connector(HttpsConnector::new(4, &handle).unwrap())
|
|
||||||
.build(&handle);
|
|
||||||
|
|
||||||
let work = client.get(uri).and_then(|res| {
|
|
||||||
println!("Response: {}", res.status());
|
|
||||||
|
|
||||||
res.body()
|
|
||||||
.for_each(|chunk| io::stdout().write_all(&chunk).map_err(From::from))
|
|
||||||
});
|
|
||||||
|
|
||||||
core.run(work).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn req_constructor(
|
|
||||||
client: &mut Client<HttpsConnector<HttpConnector>>,
|
client: &mut Client<HttpsConnector<HttpConnector>>,
|
||||||
s: &mut Source,
|
ignore_etags: bool,
|
||||||
) -> Box<Future<Item = hyper::Response, Error = hyper::Error>> {
|
) -> Box<Future<Item = hyper::Response, Error = hyper::Error>> {
|
||||||
use hyper::header::{EntityTag, HttpDate, IfModifiedSince, IfNoneMatch};
|
use hyper::header::{EntityTag, HttpDate, IfModifiedSince, IfNoneMatch};
|
||||||
|
|
||||||
let uri = Uri::from_str(&s.uri()).unwrap();
|
let uri = Uri::from_str(&s.uri()).unwrap();
|
||||||
let mut req = hyper::Request::new(Method::Get, uri);
|
let mut req = hyper::Request::new(Method::Get, uri);
|
||||||
|
|
||||||
// if !ignore_etags {
|
if !ignore_etags {
|
||||||
if let Some(foo) = s.http_etag() {
|
if let Some(foo) = s.http_etag() {
|
||||||
req.headers_mut().set(IfNoneMatch::Items(vec![
|
req.headers_mut().set(IfNoneMatch::Items(vec![
|
||||||
EntityTag::new(true, foo.to_owned()),
|
EntityTag::new(true, foo.to_owned()),
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(foo) = s.last_modified() {
|
if let Some(foo) = s.last_modified() {
|
||||||
if let Ok(x) = foo.parse::<HttpDate>() {
|
if let Ok(x) = foo.parse::<HttpDate>() {
|
||||||
req.headers_mut().set(IfModifiedSince(x));
|
req.headers_mut().set(IfModifiedSince(x));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
let work = client.request(req);
|
let work = client.request(req);
|
||||||
Box::new(work)
|
Box::new(work)
|
||||||
@ -80,11 +66,7 @@ mod tests {
|
|||||||
|
|
||||||
use database::truncate_db;
|
use database::truncate_db;
|
||||||
use Source;
|
use Source;
|
||||||
|
// use feed::Feed;
|
||||||
#[test]
|
|
||||||
fn test_foo() {
|
|
||||||
foo()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bar() {
|
fn test_bar() {
|
||||||
@ -98,15 +80,17 @@ mod tests {
|
|||||||
let url = "https://feeds.feedburner.com/InterceptedWithJeremyScahill";
|
let url = "https://feeds.feedburner.com/InterceptedWithJeremyScahill";
|
||||||
let mut source = Source::from_url(url).unwrap();
|
let mut source = Source::from_url(url).unwrap();
|
||||||
|
|
||||||
let channel = req_constructor(&mut client, &mut source)
|
let channel = request_constructor(&source, &mut client, false)
|
||||||
.map(|res| {
|
.map(|res| {
|
||||||
println!("Status: {}", res.status());
|
println!("Status: {}", res.status());
|
||||||
source.update_etag2(&res).unwrap();
|
source.update_etag2(&res).unwrap();
|
||||||
res
|
res
|
||||||
})
|
})
|
||||||
.and_then(|res| res_to_channel(res));
|
.and_then(|res| res_to_channel(res));
|
||||||
|
// .map(|chan| Feed::from_channel_source(chan, source));
|
||||||
|
|
||||||
let chan = core.run(channel).unwrap();
|
let chan = core.run(channel).unwrap();
|
||||||
|
|
||||||
// let c = chan.wait().unwrap();
|
// let c = chan.wait().unwrap();
|
||||||
println!("{:?}", chan);
|
println!("{:?}", chan);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user