diff --git a/pycrest/eve.py b/pycrest/eve.py index a1cf2b3..abc2376 100644 --- a/pycrest/eve.py +++ b/pycrest/eve.py @@ -143,7 +143,7 @@ def get(self, resource, params=None): cached = self.cache.get(key) if cached and cached['expires'] > time.time(): logger.debug('Cache hit for resource %s (params=%s)', resource, prms) - return cached['payload'] + return cached['payload'], cached['expires'] elif cached: logger.debug('Cache stale for resource %s (params=%s)', resource, prms) self.cache.invalidate(key) @@ -160,10 +160,11 @@ def get(self, resource, params=None): # cache result key = (resource, frozenset(self._session.headers.items()), frozenset(prms.items())) expires = self._get_expires(res) + expiration_time = time.time() + expires if expires > 0: - self.cache.put(key, {'expires': time.time() + expires, 'payload': ret}) + self.cache.put(key, {'expires': expiration_time, 'payload': ret}) - return ret + return ret, expiration_time def _get_expires(self, response): if 'Cache-Control' not in response.headers: @@ -198,7 +199,8 @@ def __init__(self, **kwargs): def __call__(self): if not self._data: - self._data = APIObject(self.get(self._endpoint), self) + data, expiration_time = self.get(self._endpoint) + self._data = APIObject(data, expiration_time, self) return self._data def __getattr__(self, item): @@ -265,14 +267,10 @@ def __init__(self, res, endpoint, oauth_endpoint, client_id=None, api_key=None, self._endpoint = endpoint self._session.headers.update({"Authorization": "Bearer %s" % self.token}) - def __call__(self): - if not self._data: - self._data = APIObject(self.get(self._endpoint), self) - return self._data - def whoami(self): if 'whoami' not in self._cache: - self._cache['whoami'] = self.get("https://login.eveonline.com/oauth/verify") + data, expiration_time = self.get("https://login.eveonline.com/oauth/verify") + self._cache['whoami'] = data return self._cache['whoami'] def refresh(self): @@ -289,12 +287,13 @@ def get(self, resource, params=None): class APIObject(object): - def __init__(self, parent, connection): + def __init__(self, parent, expires, connection): self._dict = {} self.connection = connection + self.expires = expires for k, v in parent.items(): if type(v) is dict: - self._dict[k] = APIObject(v, connection) + self._dict[k] = APIObject(v, self.expires, connection) elif type(v) is list: self._dict[k] = self._wrap_list(v) else: @@ -304,7 +303,7 @@ def _wrap_list(self, list_): new = [] for item in list_: if type(item) is dict: - new.append(APIObject(item, self.connection)) + new.append(APIObject(item, self.expires, self.connection)) elif type(item) is list: new.append(self._wrap_list(item)) else: @@ -319,7 +318,8 @@ def __getattr__(self, item): def __call__(self, **kwargs): # Caching is now handled by APIConnection if 'href' in self._dict: - return APIObject(self.connection.get(self._dict['href'], params=kwargs), self.connection) + data, expiration_time = self.connection.get(self._dict['href'], params=kwargs) + return APIObject(data, expiration_time, self.connection) else: return self diff --git a/tests/test_api.py b/tests/test_api.py index 64b074e..4408c7d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -249,6 +249,12 @@ def test_pagination(url, request): eve = pycrest.EVE(cache_dir='/cachedir') eve() + with mock.patch('time.time') as mock_time: + mock_time.return_value = 0 + self.assertEqual(eve.marketData().expires, 300) + + mock_time.return_value = 100000000 + self.assertEqual(eve.marketData().expires, 100000300) testing = pycrest.EVE(testing=True) self.assertEqual(testing._public_endpoint, "http://public-crest-sisi.testeveonline.com/")