def _query_zendesk(self, endpoint, object_type, *endpoint_args, **endpoint_kwargs): """ Query Zendesk for items. If an id or list of ids are passed, attempt to locate these items in the relevant cache. If they cannot be found, or no ids are passed, execute a call to Zendesk to retrieve the items. :param endpoint: target endpoint. :param object_type: object type we are expecting. :param endpoint_args: args for endpoint :param endpoint_kwargs: kwargs for endpoint :return: either a ResultGenerator or a Zenpy object. """ _id = endpoint_kwargs.get('id', None) if _id: item = query_cache(object_type, _id) if item: return item else: return self._get(url=self._build_url(endpoint(*endpoint_args, **endpoint_kwargs))) elif 'ids' in endpoint_kwargs: cached_objects = [] # Check to see if we have all objects in the cache. # If we are missing even one we request them all again. # This could be optimized to only request the missing objects. for _id in endpoint_kwargs['ids']: obj = query_cache(object_type, _id) if obj: cached_objects.append(obj) else: return self._get(self._build_url(endpoint=endpoint(*endpoint_args, **endpoint_kwargs))) return cached_objects else: return self._get(self._build_url(endpoint=endpoint(*endpoint_args, **endpoint_kwargs)))
def test_remove_from_cache(self): cache_key = 1 zenpy_object = self.cache_item(id=cache_key) self.assertIs(query_cache(get_object_type(zenpy_object), cache_key), zenpy_object) delete_from_cache(zenpy_object) self.assertIs(query_cache(get_object_type(zenpy_object), cache_key), None)
def test_cache_object_with_non_id(self): cache_key = 'somekey' user_field = self.cache_item(UserField, key=cache_key) self.assertIs(query_cache(get_object_type(user_field), cache_key), user_field)
def test_cache_object(self): cache_key = 1 ticket = self.cache_item(id=cache_key) self.assertIs(query_cache(get_object_type(ticket), cache_key), ticket)