Skip to content

Commit 410deea

Browse files
committed
Adding support for per_page, page, tag_id, and tag_name.
Also adding unsubscribed_from_emails attribute to User. Thanks https://github.com/sdorazio
1 parent a305c14 commit 410deea

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

intercom/intercom.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,23 @@ def _create_or_update_user(cls, method, **kwargs):
111111

112112
@classmethod
113113
def get_users(cls, **kwargs):
114-
""" Return a dict for the user represented by the specified email
115-
or user_id.
114+
""" Returns a paginated list of all users in your application on Intercom.
115+
116+
**Arguments**
117+
118+
* ``page``: optional (defaults to 1)
119+
* ``per_page``: optional (defaults to 500, max value of 500)
120+
* ``tag_id``: optional — query for users that are tagged with a specific tag.
121+
* ``tag_name``: optional — query for users that are tagged with a specific tag.
122+
123+
**Response**
124+
125+
* ``users``: an array of User objects (same as returned by getting a single User)
126+
* ``total_count``: the total number of Users tracked in your Intercom application
127+
* ``page``: the current requested page
128+
* ``next_page``: the next page number, if any
129+
* ``previous_page``: the previous page number, if any
130+
* ``total_pages``: the total number of pages
116131
117132
>>> result = Intercom.get_users()
118133
>>> type(result)
@@ -121,8 +136,7 @@ def get_users(cls, **kwargs):
121136
3
122137
123138
"""
124-
user_dict = Intercom._call('GET', Intercom.api_endpoint + 'users', params=kwargs)
125-
return user_dict
139+
return Intercom._call('GET', Intercom.api_endpoint + 'users', params=kwargs)
126140

127141
@classmethod
128142
def get_user(cls, email=None, user_id=None):

intercom/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def all(cls):
147147
while page <= total_pages:
148148
resp = Intercom.get_users(page=page)
149149
page += 1
150-
total_pages = resp['total_pages']
150+
total_pages = resp.get('total_pages', 0)
151151
users.extend([cls(u) for u in resp['users']])
152152
return users
153153

tests/unit/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
# License: http://jkeyes.mit-license.org/
66
#
77

8+
import json
89
import os
910

1011
from mock import Mock
1112

1213
DIRPATH = os.path.dirname(__file__)
1314
FIXTURES = os.path.join(DIRPATH, 'fixtures')
1415

16+
1517
def create_response(status, fixture=None):
1618
def request(*args, **kwargs):
1719
response = Mock()
@@ -21,3 +23,15 @@ def request(*args, **kwargs):
2123
response.content = open(fixture_path).read()
2224
return response
2325
return request
26+
27+
28+
def local_response():
29+
def _call(*args, **kwargs):
30+
response = Mock()
31+
reply = {}
32+
for name, value in kwargs.items():
33+
reply[name] = value
34+
response.content = json.dumps(reply)
35+
response.status_code = 200
36+
return response
37+
return _call

tests/unit/test_intercom.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88

99
from . import create_response
10+
from . import local_response
1011
from mock import patch
1112
from nose.tools import raises
13+
from nose.tools import eq_
14+
from nose.tools import ok_
1215
from unittest import TestCase
1316

1417
from intercom import ServerError
@@ -79,3 +82,32 @@ def test_api_error(self):
7982
@patch('requests.request', create_response(500, 'invalid.json'))
8083
def test_api_error_when_json_is_invalid(self):
8184
Intercom.get_users()
85+
86+
@patch('requests.request', local_response())
87+
def test_get_users_params(self):
88+
resp = Intercom.get_users()
89+
ok_('params' in resp)
90+
ok_('page' not in resp['params'])
91+
ok_('per_page' not in resp['params'])
92+
ok_('tag_id' not in resp['params'])
93+
ok_('tag_id' not in resp['params'])
94+
95+
resp = Intercom.get_users(page=20)
96+
ok_('params' in resp)
97+
ok_('page' in resp['params'])
98+
eq_(resp['params']['page'], 20)
99+
100+
resp = Intercom.get_users(per_page=10)
101+
ok_('params' in resp)
102+
ok_('per_page' in resp['params'])
103+
eq_(resp['params']['per_page'], 10)
104+
105+
resp = Intercom.get_users(tag_id=100)
106+
ok_('params' in resp)
107+
ok_('tag_id' in resp['params'])
108+
eq_(resp['params']['tag_id'], 100)
109+
110+
resp = Intercom.get_users(tag_name="starter")
111+
ok_('params' in resp)
112+
ok_('tag_name' in resp['params'])
113+
eq_(resp['params']['tag_name'], "starter")

0 commit comments

Comments
 (0)