Minor cleanup

This commit is contained in:
Jordan Petridis 2018-01-13 05:47:23 +02:00
parent 771b7b3804
commit 1dd25f91fd
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 41 additions and 51 deletions

View File

@ -8,6 +8,12 @@ use reqwest::header::{ETag, LastModified};
use rss::Channel;
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 feed::Feed;
use errors::*;
@ -611,6 +617,14 @@ impl<'a> Source {
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
/// corresponding db row.
fn update_etag(&mut self, req: &reqwest::Response) -> Result<()> {
@ -648,14 +662,6 @@ impl<'a> Source {
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.
///
/// Fetches the latest xml Feed.

View File

@ -1,62 +1,48 @@
use rss;
use hyper;
use std::io::{self, Write};
use std::str::FromStr;
use futures::{Future, Stream};
use hyper::Client;
use hyper::client::HttpConnector;
use hyper::Method;
use hyper::Uri;
use tokio_core::reactor::Core;
use hyper_tls::HttpsConnector;
use rss;
// use errors::*;
// use hyper::header::{ETag, LastModified};
// use hyper::header::{ETag, LastModified};
use futures::{Future, Stream};
// use futures::future::join_all;
use tokio_core::reactor::Core;
// use std::io::{self, Write};
use std::str::FromStr;
use Source;
// use errors::*;
#[allow(dead_code)]
fn foo() {
let uri = "https://www.rust-lang.org/".parse().unwrap();
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(
fn request_constructor(
s: &Source,
client: &mut Client<HttpsConnector<HttpConnector>>,
s: &mut Source,
ignore_etags: bool,
) -> Box<Future<Item = hyper::Response, Error = hyper::Error>> {
use hyper::header::{EntityTag, HttpDate, IfModifiedSince, IfNoneMatch};
let uri = Uri::from_str(&s.uri()).unwrap();
let mut req = hyper::Request::new(Method::Get, uri);
// if !ignore_etags {
if let Some(foo) = s.http_etag() {
req.headers_mut().set(IfNoneMatch::Items(vec![
EntityTag::new(true, foo.to_owned()),
]));
}
if !ignore_etags {
if let Some(foo) = s.http_etag() {
req.headers_mut().set(IfNoneMatch::Items(vec![
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));
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);
Box::new(work)
@ -80,11 +66,7 @@ mod tests {
use database::truncate_db;
use Source;
#[test]
fn test_foo() {
foo()
}
// use feed::Feed;
#[test]
fn test_bar() {
@ -98,15 +80,17 @@ mod tests {
let url = "https://feeds.feedburner.com/InterceptedWithJeremyScahill";
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| {
println!("Status: {}", res.status());
source.update_etag2(&res).unwrap();
res
})
.and_then(|res| res_to_channel(res));
// .map(|chan| Feed::from_channel_source(chan, source));
let chan = core.run(channel).unwrap();
// let c = chan.wait().unwrap();
println!("{:?}", chan);
}