Ejemplo n.º 1
0
    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')
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def setUp(self):
     self.c = DictCache()
     self.c.put('key', True)
Ejemplo n.º 4
0
 def test_apicache(self):
     eve = EVE(cache=DictCache())
     self.assertTrue(isinstance(eve.cache, DictCache))