How to use the zenpy.api_object.ApiCallGenerator function in zenpy

To help you get started, we’ve selected a few zenpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facetoe / zenpy / zenpy / api.py View on Github external
def all(self):
        return ApiCallGenerator(self.api, self.response_json)
github facetoe / zenpy / zenpy / api.py View on Github external
def __init__(self, api, response_json):
        self.response_json = response_json
        self.api = api
        if 'count' in self.response_json:
            self.count = self.response_json['count']
        elif any([result in self.response_json for result in ApiCallGenerator.single_results]):
            self.count = 1
github facetoe / zenpy / zenpy / api_object.py View on Github external
return api.object_cache[item_id]
            else:
                url = api.get_url(endpoint(id=item_id))

        elif isinstance(item_ids, list):
            if all([item_id in api.object_cache.keys() for item_id in item_ids]):
                return [api.object_cache[item_id] for item_id in item_ids]
            else:
                url = api.get_url(endpoint(ids=item_ids))
        else:
            raise Exception("Unknown type passed to get_items(): " + str(item_ids))

        # This is a bit hacky, pass the url to next_page so the api call won't be made until
        # the item is requested
        request_json = dict(next_page=url)
        return ApiCallGenerator(api, request_json)
github facetoe / zenpy / zenpy / api_object.py View on Github external
def __getattribute__(self, name):
        obj = object.__getattribute__(self, name)
        if name.endswith("_at") and obj:
            return dateutil.parser.parse(obj)

        if name in object.__getattribute__(self, 'SINGLE_GENERATORS'):
            requested_cache = object.__getattribute__(self, 'requested_cache')
            if name in requested_cache:
                return requested_cache[name]

            elif isinstance(obj, ApiCallGenerator) or isinstance(obj, types.GeneratorType):
                try:
                    requested_obj = obj.next()
                    requested_cache.update({name: requested_obj})
                    return requested_obj
                except StopIteration:
                    return None

        if isinstance(obj, list):
            if all([isinstance(o, dict) for o in obj]):
                return self.handle_dicts(obj)
        elif isinstance(obj, dict):
            class_name = self.api.get_class_name_from_json(obj)
            return ApiClassFactory(class_name, obj, self.api)

        return object.__getattribute__(self, name)
github facetoe / zenpy / zenpy / api.py View on Github external
def one(self):
        generator = ApiCallGenerator(self.api, self.response_json)
        if len(generator) > 1:
            raise TooManyResults("More than one result returned from query")

        try:
            return generator.next()
        except StopIteration:
            pass