|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +from __future__ import unicode_literals |
| 4 | + |
| 5 | +from abc import ABCMeta, abstractmethod |
| 6 | +import collections |
| 7 | + |
| 8 | +from six import add_metaclass |
| 9 | + |
| 10 | +from boxsdk.pagination.page import Page |
| 11 | + |
| 12 | + |
| 13 | +@add_metaclass(ABCMeta) |
| 14 | +class BoxObjectCollection(collections.Iterator, object): |
| 15 | + """ |
| 16 | + An iterator that represents a collection of Box objects (BaseObject). |
| 17 | +
|
| 18 | + A BoxObjectCollection instance contains everything it needs in order to retrieve and page through |
| 19 | + responses from Box API endpoints that return collections of Box objects. |
| 20 | +
|
| 21 | + This class only has two public methods: |
| 22 | +
|
| 23 | + 1). next(), which returns either a Page (sequence of BaseObjects) or individual BaseObjects based on |
| 24 | + the constructor argument 'return_full_pages'. |
| 25 | +
|
| 26 | + 2). next_pointer(), which returns the pointer (either an offset or a marker, based on the endpoint) that |
| 27 | + will be used to retrieve the next page of Box objects. This pointer can be used when requesting new |
| 28 | + BoxObjectCollection instances that start off from a particular page, instead of from the very beginning. |
| 29 | + """ |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + session, |
| 33 | + url, |
| 34 | + limit=None, |
| 35 | + fields=None, |
| 36 | + additional_params=None, |
| 37 | + return_full_pages=False, |
| 38 | + ): |
| 39 | + """ |
| 40 | + :param session: |
| 41 | + The Box session used to make requests. |
| 42 | + :type session: |
| 43 | + :class:`BoxSession` |
| 44 | + :param url: |
| 45 | + The endpoint url to hit. |
| 46 | + :type url: |
| 47 | + `unicode` |
| 48 | + :param limit: |
| 49 | + The number of entries for each page to return. The default, as well as the upper limit of this value, |
| 50 | + differs by endpoint. See https://developer.box.com/reference. If limit is set to None, then the default |
| 51 | + limit (returned by Box in the response) is used. |
| 52 | + :type limit: |
| 53 | + `int` or None |
| 54 | + :param fields: |
| 55 | + List of fields to request. If None, will return the default fields for the object. |
| 56 | + :type fields: |
| 57 | + `Iterable` of `unicode` or None |
| 58 | + :param additional_params: |
| 59 | + Additional HTTP params to send in the request. |
| 60 | + :type additional_params: |
| 61 | + `dict` or None |
| 62 | + :param return_full_pages: |
| 63 | + If True, then the returned iterator for this collection will return full pages of Box objects on each |
| 64 | + call to next(). If False, the iterator will return a single Box object on each next() call. |
| 65 | + :type return_full_pages: |
| 66 | + `bool` |
| 67 | + """ |
| 68 | + super(BoxObjectCollection, self).__init__() |
| 69 | + self._session = session |
| 70 | + self._url = url |
| 71 | + self._limit = limit |
| 72 | + self._fields = fields |
| 73 | + self._additional_params = additional_params |
| 74 | + self._return_full_pages = return_full_pages |
| 75 | + self._has_retrieved_all_items = False |
| 76 | + self._all_items = None |
| 77 | + |
| 78 | + def next(self): |
| 79 | + """ |
| 80 | + Returns either a Page (a Sequence of BaseObjects) or a BaseObject depending on self._return_full_pages. |
| 81 | +
|
| 82 | + Invoking this method may make an API call to Box. Any exceptions that can occur while making requests |
| 83 | + may be raised in this method. |
| 84 | +
|
| 85 | + :rtype: |
| 86 | + :class:`Page` or :class:`BaseObject` |
| 87 | + """ |
| 88 | + if self._all_items is None: |
| 89 | + self._all_items = self._items_generator() |
| 90 | + return next(self._all_items) |
| 91 | + |
| 92 | + __next__ = next |
| 93 | + |
| 94 | + def _items_generator(self): |
| 95 | + """ |
| 96 | + :rtype: |
| 97 | + :class:`Page` or :class:`BaseObject` |
| 98 | + """ |
| 99 | + while not self._has_retrieved_all_items: |
| 100 | + response_object = self._load_next_page() |
| 101 | + |
| 102 | + # If the limit was not specified, then it should default to whatever the server tells us. |
| 103 | + if self._limit is None: |
| 104 | + self._limit = response_object['limit'] |
| 105 | + |
| 106 | + self._update_pointer_to_next_page(response_object) |
| 107 | + self._has_retrieved_all_items = not self._has_more_pages(response_object) |
| 108 | + page = Page(self._session, response_object) |
| 109 | + |
| 110 | + if self._return_full_pages: |
| 111 | + yield page |
| 112 | + else: |
| 113 | + # It's possible for the Box API to return 0 items in a page, even if there are more items to be |
| 114 | + # retrieved on subsequent pages. When self._return_full_pages is True, then yielding a 0-item |
| 115 | + # page is fine because that's what the page returned. |
| 116 | + # But when we are iterating over individual items, and not pages, it's odd to yield a sequence of |
| 117 | + # Nones (for that page that had 0 items). So instead, we continue to request more pages until we |
| 118 | + # have Box objects to yield. |
| 119 | + if not page: |
| 120 | + continue |
| 121 | + for entry in page: |
| 122 | + yield entry |
| 123 | + |
| 124 | + def _load_next_page(self): |
| 125 | + """ |
| 126 | + Request the next page of entries from Box. Raises any network-related exceptions, including BoxAPIException. |
| 127 | + Returns a parsed dictionary of the JSON response from Box |
| 128 | +
|
| 129 | + :rtype: |
| 130 | + `dict` |
| 131 | + """ |
| 132 | + params = {} |
| 133 | + if self._limit is not None: |
| 134 | + params['limit'] = self._limit |
| 135 | + if self._fields: |
| 136 | + params['fields'] = ','.join(self._fields) |
| 137 | + if self._additional_params: |
| 138 | + params.update(self._additional_params) |
| 139 | + params.update(self._next_page_pointer_params()) |
| 140 | + box_response = self._session.get(self._url, params=params) |
| 141 | + return box_response.json() |
| 142 | + |
| 143 | + @abstractmethod |
| 144 | + def _update_pointer_to_next_page(self, response_object): |
| 145 | + """ |
| 146 | + Update the internal pointer attribute of this class to what will be used to request the next page |
| 147 | + of Box objects. |
| 148 | +
|
| 149 | + A "pointer" can either be a marker (for marker-based paging) or an offset (for limit-offset paging). |
| 150 | +
|
| 151 | + :param response_object: |
| 152 | + The parsed HTTP response from Box after requesting more pages. |
| 153 | + :type response_object: |
| 154 | + `dict` |
| 155 | + """ |
| 156 | + raise NotImplementedError |
| 157 | + |
| 158 | + @abstractmethod |
| 159 | + def _has_more_pages(self, response_object): |
| 160 | + """ |
| 161 | + Are there more pages of entries to query Box for? This gets invoked after self._update_pointer_to_next_page(). |
| 162 | +
|
| 163 | + :param response_object: |
| 164 | + The parsed HTTP response from Box after requesting more pages. |
| 165 | + :type response_object: |
| 166 | + `dict` |
| 167 | + :rtype: |
| 168 | + `bool` |
| 169 | + """ |
| 170 | + raise NotImplementedError |
| 171 | + |
| 172 | + @abstractmethod |
| 173 | + def _next_page_pointer_params(self): |
| 174 | + """ |
| 175 | + The dict of HTTP params that specify which page of Box objects to retrieve. |
| 176 | +
|
| 177 | + :rtype: |
| 178 | + `dict` |
| 179 | + """ |
| 180 | + raise NotImplementedError |
| 181 | + |
| 182 | + @abstractmethod |
| 183 | + def next_pointer(self): |
| 184 | + """ |
| 185 | + The pointer that will be used to request the next page of Box objects. |
| 186 | +
|
| 187 | + For limit-offset based paging, this is an offset. For marker-based paging, this is a marker. |
| 188 | +
|
| 189 | + The pointer only gets progressed upon successful page requests to Box. |
| 190 | +
|
| 191 | + :rtype: |
| 192 | + varies |
| 193 | + """ |
| 194 | + raise NotImplementedError |
0 commit comments