def _process_cached_session(self): """Process cache_file to reuse token instead.""" if _exists_cache(self.cache_file): self.cache = _read_cache(self.cache_file) # if self.cache['token'] is None, the cache file was corrupted. # of if self.cache['account'] does not match with self.username # In both cases, a new auth token is required. if (self.cache['token'] is None) or \ (self.cache['account'] is None) or \ (self.cache['account'] != self.username): self._authenticate() else: # we need to set the self.token and self.params # to make use of the self.query() method self.token = self.cache['token'] self.params = { 'api_version': API_VERSION, 'auth_token': self.token } # test if token from cache_file is still valid and functional # if not, it should continue to get a new auth token url = API_URI + DEVICES_ENDPOINT req = self.query(url, raw=True) if req and req.status_code == 200: self._authenticate(session=req) else: self._authenticate() else: # first time executing, so we have to create a cache file self._authenticate()
def _process_cached_session(self): """Process cache_file to reuse token instead.""" if _exists_cache(self.cache_file): self.cache = _read_cache(self.cache_file) # if self.cache['token'] is None, the cache file was corrupted. # of if self.cache['account'] does not match with self.username # In both cases, a new auth token is required. if (self.cache['token'] is None) or \ (self.cache['account'] is None) or \ (self.cache['account'] != self.username): self._authenticate() else: # we need to set the self.token and self.params # to make use of the self.query() method self.token = self.cache['token'] self.params = {'api_version': API_VERSION, 'auth_token': self.token} # test if token from cache_file is still valid and functional # if not, it should continue to get a new auth token url = API_URI + DEVICES_ENDPOINT req = self.query(url, raw=True) if req.status_code == 200: self._authenticate(session=req) else: self._authenticate() else: # first time executing, so we have to create a cache file self._authenticate()
def test_read_cache_dict(self): """Test _read_cache with expected dict.""" self.assertTrue(_save_cache(CACHE_ATTRS, CACHE)) self.assertIsInstance(_read_cache(CACHE), dict) os.remove(CACHE)
def test_read_cache_eoferror(self): """Test _read_cache method.""" open(CACHE, 'a').close() self.assertIsInstance(_read_cache(CACHE), dict) os.remove(CACHE)
def test_read_cache(self): """Test _read_cache method.""" self.assertTrue(_save_cache(DATA, CACHE)) self.assertIsInstance(_read_cache(CACHE), dict) os.remove(CACHE)