diff --git a/intercom/client.py b/intercom/client.py index ce37617b..5ddb9334 100644 --- a/intercom/client.py +++ b/intercom/client.py @@ -80,6 +80,11 @@ def jobs(self): from intercom.service import job return job.Job(self) + @property + def teams(self): + from intercom.service import team + return team.Team(self) + def _execute_request(self, request, params): result = request.execute(self.base_url, self._auth, params) self.rate_limit_details = request.rate_limit_details diff --git a/intercom/collection_proxy.py b/intercom/collection_proxy.py index 2b419f30..de9631b8 100644 --- a/intercom/collection_proxy.py +++ b/intercom/collection_proxy.py @@ -9,7 +9,7 @@ class CollectionProxy(six.Iterator): def __init__( self, client, collection_cls, collection, - finder_url, finder_params={}): + finder_url, finder_params={}, is_post_request=False): self.client = client @@ -37,6 +37,9 @@ def __init__( # a link to the next page of results self.next_page = None + # is this a post request + self.is_post_request = is_post_request + def __iter__(self): return self @@ -77,8 +80,11 @@ def get_page(self, url, params={}): # if there is no url stop iterating if url is None: raise StopIteration + if self.is_post_request: + response = self.client.post(url, params) + else: + response = self.client.get(url, params) - response = self.client.get(url, params) if response is None: raise HttpError('Http Error - No response entity returned') @@ -98,6 +104,6 @@ def paging_info_present(self, response): def extract_next_link(self, response): if self.paging_info_present(response): paging_info = response["pages"] - if paging_info["next"]: + if "next" in paging_info and paging_info["next"]: next_parsed = six.moves.urllib.parse.urlparse(paging_info["next"]) return '{}?{}'.format(next_parsed.path, next_parsed.query) diff --git a/intercom/extended_api_operations/search.py b/intercom/extended_api_operations/search.py new file mode 100644 index 00000000..e04d4362 --- /dev/null +++ b/intercom/extended_api_operations/search.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +"""Operation to search instances of a particular resource.""" + +from intercom import utils +from intercom.collection_proxy import CollectionProxy + + +class Search(object): + """A mixin that provides `search` functionality.""" + + proxy_class = CollectionProxy + + def search(self, **params): + """Search instances of the resource based on the supplied parameters.""" + collection = utils.resource_class_to_collection_name( + self.collection_class) + search_url = "/%s/search" % (collection) + finder_params = params + return self.proxy_class( + self.client, self.collection_class, collection, + search_url, finder_params, is_post_request=True) diff --git a/intercom/service/conversation.py b/intercom/service/conversation.py index 61f143a7..66a1f676 100644 --- a/intercom/service/conversation.py +++ b/intercom/service/conversation.py @@ -7,10 +7,11 @@ from intercom.api_operations.find_all import FindAll from intercom.api_operations.load import Load from intercom.api_operations.save import Save +from intercom.extended_api_operations.search import Search from intercom.service.base_service import BaseService -class Conversation(BaseService, Find, FindAll, Save, Load): +class Conversation(BaseService, Find, FindAll, Save, Search, Load): """Service class for Conversations.""" @property @@ -27,10 +28,22 @@ def resource_url(self, _id): """Return the URL for the specified resource in this collection.""" return "/%s/%s/reply" % (self.collection, _id) + def last_resource_url(self): + """Return the URL for the specified resource in this collection.""" + return "/%s/last/reply" % (self.collection,) + + def tag_resource_url(self, _id): + """Return the URL for the specified resource in this collection.""" + return "/%s/%s/tags" % (self.collection, _id) + def reply(self, **reply_data): """Reply to a message.""" return self.__reply(reply_data) + def reply_last(self, **reply_data): + """Reply to a message.""" + return self.__reply_last(reply_data) + def assign(self, **reply_data): """Assign a conversation to a user.""" reply_data['type'] = 'admin' @@ -49,6 +62,12 @@ def close(self, **reply_data): reply_data['message_type'] = 'close' return self.__reply(reply_data) + def snooze(self, **reply_data): + """Snooze the conversation until the passed in `snoozed_until` time""" + reply_data['type'] = 'admin' + reply_data['message_type'] = 'snoozed' + return self.__reply(reply_data) + def mark_read(self, _id): """Mark a conversation as read.""" data = {'read': True} @@ -61,3 +80,14 @@ def __reply(self, reply_data): reply_data['conversation_id'] = _id response = self.client.post(self.resource_url(_id), reply_data) return self.collection_class().from_response(response) + + def __reply_last(self, reply_data): + """Send requests to the resource handler.""" + response = self.client.post(self.last_resource_url(), reply_data) + return self.collection_class().from_response(response) + + def tag(self, tag_data): + """Send requests to the tag resource handler.""" + _id = tag_data.pop('conversation_id') + response = self.client.post(self.tag_resource_url(_id), tag_data) + return self.collection_class().from_response(response) diff --git a/intercom/service/team.py b/intercom/service/team.py new file mode 100644 index 00000000..2bfe7a5c --- /dev/null +++ b/intercom/service/team.py @@ -0,0 +1,10 @@ +from intercom import team +from intercom.api_operations.all import All +from intercom.service.base_service import BaseService + + +class Team(BaseService, All): + + @property + def collection_class(self): + return team.Team diff --git a/intercom/team.py b/intercom/team.py new file mode 100644 index 00000000..3cf40fa1 --- /dev/null +++ b/intercom/team.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +from intercom.traits.api_resource import Resource + + +class Team(Resource): + pass