diff --git a/pycrest/eve.py b/pycrest/eve.py index b27c508..7bb9f80 100644 --- a/pycrest/eve.py +++ b/pycrest/eve.py @@ -220,12 +220,18 @@ def get(self, resource, params={}): self._session.headers.items()), frozenset( prms.items())) expires = self._get_expires(res) + response = {'result': ret, + 'endpoint_version': res.headers['content-type'].split(';')[0], + 'status_code': res.status_code, + 'expires_in': expires, + 'expires_at': time.time() + expires} + if expires > 0: self.cache.put( key, { - 'expires': time.time() + expires, 'payload': ret}) + 'expires': time.time() + expires, 'payload': response}) - return ret + return response #post is not idempotent so there should be no caching def post(self, resource, data={}): @@ -406,12 +412,16 @@ def get(self, resource, params={}): self.refresh() return super(self.__class__, self).get(resource, params) + class APIObject(object): def __init__(self, parent, connection): self._dict = {} self.connection = connection for k, v in parent.items(): + # If we don't "pull in" 'result' it will cause the creation of an unwanted APIObject + if k == 'result': + v = parent[k] if isinstance(v, dict): self._dict[k] = APIObject(v, connection) elif isinstance(v, list): @@ -431,11 +441,15 @@ def _wrap_list(self, list_): return new def __getattr__(self, item): + # Try to get the property from the dictionary first if item in self._dict: return self._dict[item] - raise AttributeError(item) + if 'result' in self._dict: + if isinstance(self._dict['result'], APIObject): + return self._dict['result'].__getattr__(item) + raise AttributeError(item) def __call__(self, **kwargs): """carries out a CREST request diff --git a/tests/test_pull_37.py b/tests/test_pull_37.py new file mode 100644 index 0000000..dd2d8a5 --- /dev/null +++ b/tests/test_pull_37.py @@ -0,0 +1,100 @@ +''' +Created on Jun 27, 2016 + +@author: henk +''' +from pycrest.eve import EVE, APIObject +import httmock +import unittest + +@httmock.urlmatch( + scheme="https", + netloc=r"(api-sisi\.test)?(crest-tq\.)?eveonline\.com$", + path=r"^/?$") +def root_mock(url, request): + return httmock.response( + status_code=200, + content='''{ + "marketData": { + "href": "https://crest-tq.eveonline.com/market/prices/" + }, + "perverse": { + "href": "https://crest-tq.eveonline.com/perverse/" + }}''', headers={"content-type": "application/vnd.ccp.eve.Api-v5+json; charset=utf-8"}) + + +@httmock.urlmatch( + scheme="https", + netloc=r"(api-sisi\.test)?(crest-tq\.)?eveonline\.com$", + path=r"^/market/prices/?$") +def market_prices_mock(url, request): + return httmock.response( + status_code=200, + content='{"result": "10213", "items": [], "status_code": 500, "pa' + 'geCount_str": "1", "totalCount": 10213}', + headers={"content-type": "application/vnd.ccp.eve.Api-v5+json; charset=utf-8"}) + +@httmock.urlmatch( + scheme="https", + netloc=r"(api-sisi\.test)?(crest-tq\.)?eveonline\.com$", + path=r"^/perverse/?$") +def perverse_mock(url, request): + return httmock.response( + status_code=200, + content='''{ + "result": { + "result": "10213", + "status_code": 500 + }, + "items": [], + "status_code": { + "result": "10214", + "status_code": 501 + }, + "pageCount_str": "1", + "totalCount": 10213 +}''', + headers={"content-type": "application/vnd.ccp.eve.Api-v5+json; charset=utf-8"}) + + +all_httmocks = [ + root_mock, + market_prices_mock, + perverse_mock] + + +class TestEVE(unittest.TestCase): + + def setUp(self): + self.api = EVE() + + def test_root(self): + with httmock.HTTMock(*all_httmocks): + res = self.api() + self.assertTrue(isinstance(res, APIObject)) + self.assertEqual(res.endpoint_version, 'application/vnd.ccp.eve.Api-v5+json') + self.assertEqual(res.status_code, 200) + + def test_market_price(self): + with httmock.HTTMock(*all_httmocks): + res = self.api().marketData() + self.assertEqual(res.status_code, 200) + self.assertEqual(res.result.status_code, 500) + self.assertEqual(res.result.result, '10213') + + def test_perverse(self): + with httmock.HTTMock(*all_httmocks): + res = self.api().perverse() + self.assertEqual(res.status_code, 200) + self.assertEqual(res.pageCount_str, '1') + self.assertEqual(res.result.pageCount_str, '1') + self.assertEqual(res.totalCount, 10213) + self.assertEqual(res.result.totalCount, 10213) + self.assertEqual(res.items, []) + self.assertEqual(res.result.items, []) + self.assertEqual(res.result.status_code.status_code, 501) + self.assertEqual(res.result.status_code.result, '10214') + self.assertEqual(res.result.result.result, '10213') + +if __name__ == "__main__": + unittest.main()