Skip to content

Commit 15d6674

Browse files
committed
wip
1 parent 4ce8170 commit 15d6674

File tree

7 files changed

+121
-3
lines changed

7 files changed

+121
-3
lines changed

intercom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
MultipleMatchingUsersError, RateLimitExceeded, ResourceNotFound,
77
ServerError, ServiceUnavailableError, UnexpectedError, TokenUnauthorizedError)
88

9-
__version__ = '3.1.0'
9+
__version__ = '3.1.0.RYAN'
1010

1111

1212
RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \

intercom/api_operations/all.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
class All(object):
99
"""A mixin that provides `all` functionality."""
1010

11+
proxy_class = CollectionProxy
12+
1113
def all(self):
1214
"""Return a CollectionProxy for the resource."""
1315
collection = utils.resource_class_to_collection_name(
1416
self.collection_class)
1517
finder_url = "/%s" % (collection)
16-
return CollectionProxy(
18+
return self.proxy_class(
1719
self.client, self.collection_class, collection, finder_url)

intercom/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def users(self):
7070
from intercom.service import user
7171
return user.User(self)
7272

73+
@property
74+
def contacts(self):
75+
from intercom.service import contact
76+
return contact.Contact(self)
77+
7378
@property
7479
def leads(self):
7580
from intercom.service import lead

intercom/collection_proxy.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,17 @@ def get_page(self, url, params={}):
7979
raise StopIteration
8080

8181
response = self.client.get(url, params)
82+
8283
if response is None:
8384
raise HttpError('Http Error - No response entity returned')
8485

85-
collection = response[self.collection]
86+
# HACK: dealing with the fact that in unstable /tags is returning
87+
# {'type': 'list', 'data': []} instead of
88+
# {'type': 'tags.list', 'tags': []}
89+
if response['type'] == 'list':
90+
collection = response['data']
91+
else:
92+
collection = response[self.collection]
8693
# if there are no resources in the response stop iterating
8794
if collection is None:
8895
raise StopIteration

intercom/contact.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from intercom.traits.api_resource import Resource
4+
from intercom.traits.incrementable_attributes import IncrementableAttributes
5+
6+
7+
class Contact(Resource, IncrementableAttributes):
8+
9+
update_verb = 'post'
10+
identity_vars = ['id', 'email', 'user_id']
11+
12+
@property
13+
def flat_store_attributes(self):
14+
return ['custom_attributes']
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import six
4+
from intercom import HttpError
5+
from intercom import utils
6+
from intercom.collection_proxy import CollectionProxy
7+
8+
class CursorCollectionProxy(CollectionProxy):
9+
def __init__(self, client, collection_cls, collection, finder_url, finder_params={}):
10+
super(CursorCollectionProxy, self).__init__(client, collection_cls, collection,finder_url, finder_params=finder_params)
11+
self.paging_info = None
12+
13+
def get_page(self, url, params={}):
14+
# get a page of results
15+
# from intercom import Intercom
16+
17+
# if there is no url stop iterating
18+
if url is None:
19+
raise StopIteration
20+
21+
import pdb; pdb.set_trace()
22+
if "sort" in params or "query" in params:
23+
url = f"{url}/search"
24+
if self.paging_info and "next" in self.paging_info:
25+
params["pagination"] = {
26+
"per_page": self.paging_info["per_page"],
27+
"starting_after": self.paging_info["next"]["starting_after"]
28+
}
29+
response = self.client.post(url, params)
30+
else:
31+
response = self.client.get(url, params)
32+
33+
if response is None:
34+
raise HttpError('Http Error - No response entity returned')
35+
36+
# HACK: dealing with the fact that in unstable /tags is returning
37+
# {'type': 'list', 'data': []} instead of
38+
# {'type': 'tags.list', 'tags': []}
39+
if response['type'] == 'list':
40+
collection = response['data']
41+
else:
42+
collection = response[self.collection]
43+
# if there are no resources in the response stop iterating
44+
if collection is None:
45+
raise StopIteration
46+
47+
# create the resource iterator
48+
self.resources = iter(collection)
49+
# grab the next page URL if one exists
50+
self.next_page = self.extract_next_link(response)
51+
52+
def get_next_page(self):
53+
# get the next page of results
54+
return self.get_page(self.next_page, self.finder_params)
55+
56+
def extract_next_link(self, response):
57+
if self.paging_info_present(response):
58+
self.paging_info = response["pages"]
59+
print(self.paging_info)
60+
if "next" in self.paging_info:
61+
# '/users?per_page=50&page=2'
62+
# {'page': 2, 'starting_after': 'WzE1NzkxODQzNDEwMDAsIjVlMTZmOTg3MWEzNmFiMTFjMDY2YmMzZSIsMl0='}
63+
if self.finder_params:
64+
return '/{}'.format(self.collection)
65+
else:
66+
return '/{}?page={}&starting_after={}'.format(
67+
self.collection,
68+
self.paging_info["next"]["page"],
69+
self.paging_info["next"]["starting_after"])

intercom/service/contact.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from intercom import contact
4+
from intercom.api_operations.all import All
5+
from intercom.api_operations.bulk import Submit
6+
from intercom.api_operations.find import Find
7+
from intercom.api_operations.find_all import FindAll
8+
from intercom.api_operations.delete import Delete
9+
from intercom.api_operations.save import Save
10+
from intercom.api_operations.load import Load
11+
from intercom.extended_api_operations.tags import Tags
12+
from intercom.service.base_service import BaseService
13+
from intercom.cursor_collection_proxy import CursorCollectionProxy
14+
15+
class Contact(BaseService, All, Find, FindAll, Delete, Save, Load, Submit, Tags):
16+
17+
proxy_class = CursorCollectionProxy
18+
19+
@property
20+
def collection_class(self):
21+
return contact.Contact

0 commit comments

Comments
 (0)