|
11 | 11 | import time |
12 | 12 | import types |
13 | 13 | from intercom.api_operations.find import Find |
| 14 | +from intercom.api_operations.find_all import FindAll |
14 | 15 | from intercom.api_operations.save import Save |
| 16 | +from intercom.collection_proxy import CollectionProxy |
15 | 17 |
|
16 | 18 |
|
17 | 19 | class ArgumentError(ValueError): |
@@ -225,96 +227,6 @@ class DynamicClass(Resource): |
225 | 227 | return dyncls |
226 | 228 |
|
227 | 229 |
|
228 | | -class CollectionProxy(object): |
229 | | - |
230 | | - def __init__(self, cls, collection, finder_url, finder_params={}): |
231 | | - # needed to create class instances of the resource |
232 | | - self.collection_cls = cls |
233 | | - |
234 | | - # needed to reference the collection in the response |
235 | | - self.collection = collection |
236 | | - |
237 | | - # the original URL to retrieve the resources |
238 | | - self.finder_url = finder_url |
239 | | - |
240 | | - # the params to filter the resources |
241 | | - self.finder_params = finder_params |
242 | | - |
243 | | - # an iterator over the resources found in the response |
244 | | - self.resources = None |
245 | | - |
246 | | - # a link to the next page of results |
247 | | - self.next_page = None |
248 | | - |
249 | | - |
250 | | - def __iter__(self): |
251 | | - return self |
252 | | - |
253 | | - def next(self): |
254 | | - if self.resources is None: |
255 | | - # get the first page of results |
256 | | - self.get_first_page() |
257 | | - |
258 | | - # try to get a resource if there are no more in the |
259 | | - # current resource iterator (StopIteration is raised) |
260 | | - # try to get the next page of results first |
261 | | - try: |
262 | | - resource = self.resources.next() |
263 | | - except StopIteration: |
264 | | - self.get_next_page() |
265 | | - resource = self.resources.next() |
266 | | - |
267 | | - instance = self.collection_cls(**resource) |
268 | | - return instance |
269 | | - |
270 | | - def get_first_page(self): |
271 | | - # get the first page of results |
272 | | - return self.get_page(self.finder_url, self.finder_params) |
273 | | - |
274 | | - def get_next_page(self): |
275 | | - # get the next page of results |
276 | | - return self.get_page(self.next_page) |
277 | | - |
278 | | - def get_page(self, url, params={}): |
279 | | - # get a page of results |
280 | | - |
281 | | - # if there is no url stop iterating |
282 | | - if url is None: |
283 | | - raise StopIteration |
284 | | - |
285 | | - response = Intercom.get(url, **params) |
286 | | - collection = response[self.collection] |
287 | | - # if there are no resources in the response stop iterating |
288 | | - if collection is None: |
289 | | - raise StopIteration |
290 | | - |
291 | | - # create the resource iterator |
292 | | - self.resources = iter(collection) |
293 | | - # grab the next page URL if one exists |
294 | | - self.next_page = self.extract_next_link(response) |
295 | | - |
296 | | - def paging_info_present(self, response): |
297 | | - return 'pages' in response and 'type' in response['pages'] |
298 | | - |
299 | | - def extract_next_link(self, response): |
300 | | - if self.paging_info_present(response): |
301 | | - paging_info = response["pages"] |
302 | | - return paging_info["next"] |
303 | | - |
304 | | - |
305 | | -class FindAll(object): |
306 | | - |
307 | | - @classmethod |
308 | | - def find_all(cls, **params): |
309 | | - collection = utils.resource_class_to_collection_name(cls) |
310 | | - print "find_all %s in %s" % (params, collection) |
311 | | - if 'id' in params and 'type' not in params: |
312 | | - finder_url = "/%s/%s" % (collection, params['id']) |
313 | | - else: |
314 | | - finder_url = "/%s" % (collection) |
315 | | - finder_params = params |
316 | | - return CollectionProxy(cls, collection, finder_url, finder_params) |
317 | | - |
318 | 230 |
|
319 | 231 | class IncrementableAttributes(object): |
320 | 232 |
|
|
0 commit comments