Skip to content

Adding multipage support for Event.find_all #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

* 3.0.2
* Added multipage support for Event.find_all. (`#147 <https://github.com/jkeyes/python-intercom/pull/147>`_)
* 3.0.1
* Added support for HTTP keep-alive. (`#146 <https://github.com/jkeyes/python-intercom/pull/146>`_)
* 3.0
Expand Down
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ Events
}
)

# Retrieve event list for user with id:'123abc'
intercom.events.find_all(type='user', "intercom_user_id"="123abc)

Metadata Objects support a few simple types that Intercom can present on
your behalf

Expand Down
2 changes: 1 addition & 1 deletion intercom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MultipleMatchingUsersError, RateLimitExceeded, ResourceNotFound,
ServerError, ServiceUnavailableError, UnexpectedError, TokenUnauthorizedError)

__version__ = '3.0.1'
__version__ = '3.0.2'


RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \
Expand Down
4 changes: 3 additions & 1 deletion intercom/api_operations/find_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class FindAll(object):
"""A mixin that provides `find_all` functionality."""

proxy_class = CollectionProxy

def find_all(self, **params):
"""Find all instances of the resource based on the supplied parameters."""
collection = utils.resource_class_to_collection_name(
Expand All @@ -17,6 +19,6 @@ def find_all(self, **params):
else:
finder_url = "/%s" % (collection)
finder_params = params
return CollectionProxy(
return self.proxy_class(
self.client, self.collection_class, collection,
finder_url, finder_params)
12 changes: 11 additions & 1 deletion intercom/service/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
from intercom import event
from intercom.api_operations.bulk import Submit
from intercom.api_operations.save import Save
from intercom.api_operations.find_all import FindAll
from intercom.service.base_service import BaseService
from intercom.collection_proxy import CollectionProxy


class Event(BaseService, Save, Submit):
class EventCollectionProxy(CollectionProxy):

def paging_info_present(self, response):
return 'pages' in response and 'next' in response['pages']


class Event(BaseService, Save, Submit, FindAll):

proxy_class = EventCollectionProxy

@property
def collection_class(self):
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ def get_company(name):
}


def get_event(name="the-event-name"):
return {
"type": "event",
"event_name": name,
"created_at": 1389913941,
"user_id": "314159",
"metadata": {
"type": "user",
"invitee_email": "pi@example.org",
"invite_code": "ADDAFRIEND"
}
}


def page_of_users(include_next_link=False):
page = {
"type": "user.list",
Expand All @@ -182,6 +196,21 @@ def page_of_users(include_next_link=False):
return page


def page_of_events(include_next_link=False):
page = {
"type": "event.list",
"pages": {
"next": None,
},
"events": [
get_event("invited-friend"),
get_event("bought-sub")],
}
if include_next_link:
page["pages"]["next"] = "https://api.intercom.io/events?type=user&intercom_user_id=55a3b&before=144474756550" # noqa
return page


def page_of_companies(include_next_link=False):
page = {
"type": "company.list",
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from datetime import datetime
from intercom.client import Client
from intercom.user import User
from mock import call
from mock import patch
from nose.tools import eq_
from nose.tools import istest
from tests.unit import page_of_events


class EventTest(unittest.TestCase):
Expand All @@ -22,6 +25,29 @@ def setUp(self): # noqa
name="Jim Bob")
self.created_time = now - 300

@istest
def it_stops_iterating_if_no_next_link(self):
body = page_of_events(include_next_link=False)
with patch.object(Client, 'get', return_value=body) as mock_method: # noqa
event_names = [event.event_name for event in self.client.events.find_all(
type='user', email='joe@example.com')]
mock_method.assert_called_once_with(
'/events', {'type': 'user', 'email': 'joe@example.com'})
eq_(event_names, ['invited-friend', 'bought-sub']) # noqa

@istest
def it_keeps_iterating_if_next_link(self):
page1 = page_of_events(include_next_link=True)
page2 = page_of_events(include_next_link=False)
side_effect = [page1, page2]
with patch.object(Client, 'get', side_effect=side_effect) as mock_method: # noqa
event_names = [event.event_name for event in self.client.events.find_all(
type='user', email='joe@example.com')]
eq_([call('/events', {'type': 'user', 'email': 'joe@example.com'}),
call('/events?type=user&intercom_user_id=55a3b&before=144474756550', {})], # noqa
mock_method.mock_calls)
eq_(event_names, ['invited-friend', 'bought-sub'] * 2) # noqa

@istest
def it_creates_an_event_with_metadata(self):
data = {
Expand Down