Skip to content

Commit ecd5c61

Browse files
committed
Moving FindAll into it's own module.
Moving CollectionProxy into it's own module.
1 parent 9fbd687 commit ecd5c61

File tree

3 files changed

+96
-90
lines changed

3 files changed

+96
-90
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from intercom import utils
2+
from intercom.collection_proxy import CollectionProxy
3+
4+
5+
class FindAll(object):
6+
7+
@classmethod
8+
def find_all(cls, **params):
9+
collection = utils.resource_class_to_collection_name(cls)
10+
print "find_all %s in %s" % (params, collection)
11+
if 'id' in params and 'type' not in params:
12+
finder_url = "/%s/%s" % (collection, params['id'])
13+
else:
14+
finder_url = "/%s" % (collection)
15+
finder_params = params
16+
return CollectionProxy(cls, collection, finder_url, finder_params)

intercom/collection_proxy.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from intercom import Intercom
2+
3+
4+
class CollectionProxy(object):
5+
6+
def __init__(self, cls, collection, finder_url, finder_params={}):
7+
# needed to create class instances of the resource
8+
self.collection_cls = cls
9+
10+
# needed to reference the collection in the response
11+
self.collection = collection
12+
13+
# the original URL to retrieve the resources
14+
self.finder_url = finder_url
15+
16+
# the params to filter the resources
17+
self.finder_params = finder_params
18+
19+
# an iterator over the resources found in the response
20+
self.resources = None
21+
22+
# a link to the next page of results
23+
self.next_page = None
24+
25+
26+
def __iter__(self):
27+
return self
28+
29+
def next(self):
30+
if self.resources is None:
31+
# get the first page of results
32+
self.get_first_page()
33+
34+
# try to get a resource if there are no more in the
35+
# current resource iterator (StopIteration is raised)
36+
# try to get the next page of results first
37+
try:
38+
resource = self.resources.next()
39+
except StopIteration:
40+
self.get_next_page()
41+
resource = self.resources.next()
42+
43+
instance = self.collection_cls(**resource)
44+
return instance
45+
46+
def get_first_page(self):
47+
# get the first page of results
48+
return self.get_page(self.finder_url, self.finder_params)
49+
50+
def get_next_page(self):
51+
# get the next page of results
52+
return self.get_page(self.next_page)
53+
54+
def get_page(self, url, params={}):
55+
# get a page of results
56+
57+
# if there is no url stop iterating
58+
if url is None:
59+
raise StopIteration
60+
61+
response = Intercom.get(url, **params)
62+
collection = response[self.collection]
63+
# if there are no resources in the response stop iterating
64+
if collection is None:
65+
raise StopIteration
66+
67+
# create the resource iterator
68+
self.resources = iter(collection)
69+
# grab the next page URL if one exists
70+
self.next_page = self.extract_next_link(response)
71+
72+
def paging_info_present(self, response):
73+
return 'pages' in response and 'type' in response['pages']
74+
75+
def extract_next_link(self, response):
76+
if self.paging_info_present(response):
77+
paging_info = response["pages"]
78+
return paging_info["next"]

intercom/user.py

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import time
1212
import types
1313
from intercom.api_operations.find import Find
14+
from intercom.api_operations.find_all import FindAll
1415
from intercom.api_operations.save import Save
16+
from intercom.collection_proxy import CollectionProxy
1517

1618

1719
class ArgumentError(ValueError):
@@ -225,96 +227,6 @@ class DynamicClass(Resource):
225227
return dyncls
226228

227229

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-
318230

319231
class IncrementableAttributes(object):
320232

0 commit comments

Comments
 (0)