summary history branches tags files
commit:abaae1f00088af3c0e1fba850e00b240a4bf5fd3
author:Trevor Bentley
committer:Trevor Bentley
date:Wed Apr 12 19:05:12 2017 +0200
parents:60644155047b95fd5a6464aa32aa5e6cb7242948
Support null contexts
diff --git a/Cargo.toml b/Cargo.toml
line changes: +1/-0
index a3c29b9..3e3efe4
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@ rustc-serialize = "0.3.23"
 url = "1.4.0"
 rust-ini = "0.9"
 time = "0.1"
+timer = "0.1.6"
 
 [target."cfg(windows)".dependencies]
 [target."cfg(all(unix, not(target_os = \"macos\")))".dependencies]

diff --git a/src/webapi/mod.rs b/src/webapi/mod.rs
line changes: +7/-11
index 33b9a22..3cb2ec4
--- a/src/webapi/mod.rs
+++ b/src/webapi/mod.rs
@@ -1,4 +1,5 @@
 extern crate time;
+extern crate timer;
 
 use std::fmt;
 use std::iter;
@@ -107,7 +108,7 @@ pub struct PlayerState {
     pub item: ConnectPlaybackItem,
     pub shuffle_state: bool,
     pub repeat_state: String,
-    pub context: ConnectContext,
+    pub context: Option<ConnectContext>,
 }
 
 impl fmt::Display for PlayerState {
@@ -290,14 +291,6 @@ impl SpotifyConnectr {
             Some(s) => s,
             None => process::exit(0),
         };
-        let access = match settings.access_token {
-            Some(ref x) => x.clone(),
-            None => String::new(),
-        };
-        let refresh = match settings.refresh_token {
-            Some(ref x) => x.clone(),
-            None => String::new(),
-        };
         let expire = settings.expire_utc;
         let access = settings.access_token.clone();
         let refresh = settings.refresh_token.clone();
@@ -308,10 +301,13 @@ impl SpotifyConnectr {
                          expire_utc: expire,
                          device: None}
     }
-    pub fn connect(&mut self) {
+    fn is_token_expired(&self) -> bool {
         let now = time::now_utc().to_timespec().sec as u64;
         let expire_utc = self.expire_utc.unwrap_or(0);
-        if self.access_token.is_some() && now < expire_utc {
+        expire_utc <= (now - 60)
+    }
+    pub fn connect(&mut self) {
+        if self.access_token.is_some() && !self.is_token_expired() {
             println!("Reusing saved tokens");
             return ()
         }