def __init__( self, additional_headers=None, user_agent=None, transport_adapter=None, **kwargs): '''Initialises a PyCrest object Keyword arguments: additional_headers - a list of http headers that will be sent to the server user_agent - a custom user agent cache - an instance of an APICache object that will cache HTTP Requests. Default is DictCache, pass cache=None to disable caching. ''' # Set up a Requests Session session = requests.Session() if additional_headers is None: additional_headers = {} if user_agent is None: user_agent = "PyCrest/{0} +https://github.com/pycrest/PyCrest"\ .format(version) if isinstance(transport_adapter, HTTPAdapter): session.mount('http://', transport_adapter) session.mount('https://', transport_adapter) session.headers.update({ "User-Agent": user_agent, "Accept": "application/json", }) session.headers.update(additional_headers) self._session = session if 'cache' not in kwargs: self.cache = DictCache() else: cache = kwargs.pop('cache') if isinstance(cache, APICache): self.cache = cache elif cache is None: self.cache = DummyCache() else: raise ValueError('Provided cache must implement APICache')
class TestDictCache(unittest.TestCase): def setUp(self): self.c = DictCache() self.c.put('key', True) def test_put(self): self.assertEqual(self.c._dict['key'], True) def test_get(self): self.assertEqual(self.c.get('key'), True) def test_invalidate(self): self.c.invalidate('key') self.assertIsNone(self.c.get('key')) def test_cache_dir(self): pass
def setUp(self): self.c = DictCache() self.c.put('key', True)
def test_apicache(self): eve = EVE(cache=DictCache()) self.assertTrue(isinstance(eve.cache, DictCache))