Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the cache expiration time to APIObjects. #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions pycrest/eve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down