From 905bb6aa048a036c439976fa2e68e4ac2a6e1d45 Mon Sep 17 00:00:00 2001 From: Cameron Stitt Date: Fri, 7 Jun 2013 09:53:17 -0700 Subject: [PATCH 1/2] Add companies to accepted parameters in User.create --- intercom/user.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/intercom/user.py b/intercom/user.py index effa2528..9dbb7930 100644 --- a/intercom/user.py +++ b/intercom/user.py @@ -3,7 +3,7 @@ # # License: http://jkeyes.mit-license.org/ # -""" User module. +""" User module. >>> from intercom import Intercom >>> Intercom.app_id = 'dummy-app-id' @@ -95,8 +95,9 @@ def find_by_user_id(cls, user_id): return cls(resp) @classmethod - def create(cls, user_id=None, email=None, name=None, created_at=None, - custom_data=None, last_seen_ip=None, last_seen_user_agent=None): + def create(cls, user_id=None, email=None, name=None, created_at=None, + custom_data=None, last_seen_ip=None, last_seen_user_agent=None, + companies=None): """ Create or update a user. >>> user = User.create(email="somebody@example.com") @@ -104,14 +105,15 @@ def create(cls, user_id=None, email=None, name=None, created_at=None, u'Somebody' """ - resp = Intercom.create_user(user_id=user_id, email=email, name=name, - created_at=created_at, custom_data=custom_data, - last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent) + resp = Intercom.create_user(user_id=user_id, email=email, name=name, + created_at=created_at, custom_data=custom_data, + last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent, + companies=companies) return cls(resp) @classmethod def all(cls): - """ Return all of the Users. + """ Return all of the Users. >>> users = User.all() >>> len(users) @@ -124,7 +126,7 @@ def all(cls): return [cls(u) for u in resp['users']] def save(self): - """ Creates or updates a User. + """ Creates or updates a User. >>> user = User() >>> user.email = "somebody@example.com" @@ -135,7 +137,7 @@ def save(self): """ attrs = {} for key in User.attributes: - value = dict.get(self, key) + value = dict.get(self, key) if value: attrs[key] = value resp = Intercom.update_user(**attrs) @@ -211,7 +213,7 @@ def created_at(self, value): @property def social_profiles(self): - """ Returns a list of SocialProfile objects for this User. + """ Returns a list of SocialProfile objects for this User. >>> users = User.all() >>> social_profiles = users[0].social_profiles @@ -231,7 +233,7 @@ def social_profiles(self): @property def location_data(self): - """ Returns a LocationData object for this User. + """ Returns a LocationData object for this User. >>> users = User.all() >>> location_data = users[0].location_data @@ -269,7 +271,7 @@ def custom_data(self): @custom_data.setter def custom_data(self, custom_data): - """ Sets the CustomData for this User. + """ Sets the CustomData for this User. >>> user = User(email="somebody@example.com") >>> user.custom_data = { 'max_monthly_spend': 200 } @@ -290,7 +292,7 @@ def custom_data(self, custom_data): class CustomData(dict): """ A dict that limits keys to strings, and values to real numbers - and strings. + and strings. >>> from intercom.user import CustomData >>> data = CustomData() @@ -314,7 +316,7 @@ def __setitem__(self, key, value): super(CustomData, self).__setitem__(key, value) class SocialProfile(dict): # pylint: disable=R0921 - """ Object representing http://docs.intercom.io/#SocialProfiles) + """ Object representing http://docs.intercom.io/#SocialProfiles) This object is read-only, and to hint at this __setitem__ is disabled. From 0f7d2959b357921f12c6fb2c8c512a8a4dfe8676 Mon Sep 17 00:00:00 2001 From: Cameron Stitt Date: Fri, 7 Jun 2013 10:06:08 -0700 Subject: [PATCH 2/2] Add last_request_at and last_impression_at to user create --- intercom/intercom.py | 64 +++++++++++++++++++++++--------------------- intercom/user.py | 6 +++-- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/intercom/intercom.py b/intercom/intercom.py index 4b64e363..1beddeea 100644 --- a/intercom/intercom.py +++ b/intercom/intercom.py @@ -3,7 +3,7 @@ # # License: http://jkeyes.mit-license.org/ # -""" intercom module +""" intercom module All of the API requests are created, and the API responses are parsed here. @@ -80,20 +80,20 @@ def _call(cls, method, url, params=None): req_params['headers'] = headers resp = requests.request(method, url, timeout=Intercom.timeout, \ - auth=(Intercom.app_id, Intercom.api_key), + auth=(Intercom.app_id, Intercom.api_key), **req_params) return resp @classmethod def _create_or_update_user(cls, method, **kwargs): """ Used by create_user and update_user. """ - user_dict = Intercom._call(method, Intercom.api_endpoint + 'users', + user_dict = Intercom._call(method, Intercom.api_endpoint + 'users', params=kwargs) return user_dict @classmethod def get_users(cls): - """ Return a dict for the user represented by the specified email or user_id. + """ Return a dict for the user represented by the specified email or user_id. >>> result = Intercom.get_users() >>> type(result) @@ -107,7 +107,7 @@ def get_users(cls): @classmethod def get_user(cls, email=None, user_id=None): - """ Return a dict for the user represented by the specified email or user_id. + """ Return a dict for the user represented by the specified email or user_id. >>> user = Intercom.get_user(user_id='123') >>> user['name'] @@ -120,9 +120,10 @@ def get_user(cls, email=None, user_id=None): return user_dict @classmethod - def create_user(cls, user_id=None, email=None, name=None, created_at=None, - custom_data=None, last_seen_ip=None, last_seen_user_agent=None, companies=None): - """ Create a user from the available parameters. + def create_user(cls, user_id=None, email=None, name=None, created_at=None, + custom_data=None, last_seen_ip=None, last_seen_user_agent=None, + companies=None, last_request_at=None, last_impression_at=None): + """ Create a user from the available parameters. >>> from datetime import datetime >>> import time @@ -136,14 +137,17 @@ def create_user(cls, user_id=None, email=None, name=None, created_at=None, u'smuggler' """ - return Intercom._create_or_update_user('POST', user_id=user_id, email=email, - name=name, created_at=created_at, custom_data=custom_data, - last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent, companies=companies) + return Intercom._create_or_update_user('POST', user_id=user_id, email=email, + name=name, created_at=created_at, custom_data=custom_data, + last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent, + companies=companies, last_request_at=last_request_at, + last_impression_at=last_impression_at) @classmethod - def update_user(cls, user_id=None, email=None, name=None, created_at=None, - custom_data=None, last_seen_ip=None, last_seen_user_agent=None, companies=None): - """ Update a user with the available parameters. + def update_user(cls, user_id=None, email=None, name=None, created_at=None, + custom_data=None, last_seen_ip=None, last_seen_user_agent=None, + companies=None): + """ Update a user with the available parameters. >>> user = Intercom.get_user(user_id='123') >>> user['name'] @@ -154,7 +158,7 @@ def update_user(cls, user_id=None, email=None, name=None, created_at=None, """ return Intercom._create_or_update_user('PUT', user_id=user_id, email=email, - name=name, created_at=created_at, custom_data=custom_data, + name=name, created_at=created_at, custom_data=custom_data, last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent, companies=companies) @classmethod @@ -166,7 +170,7 @@ def update_user_company(cls, user_id=None, company_id=None, company_data=None): @classmethod def create_impression(cls, user_id=None, email=None, user_ip=None, user_agent=None, location=None): - """ Create an impression. + """ Create an impression. >>> result = Intercom.create_impression(email="somebody@example.com", ... user_agent="MyApp/1.0", user_ip="2.3.4.5") @@ -174,17 +178,17 @@ def create_impression(cls, user_id=None, email=None, user_ip=None, 1 """ - params = { 'email': email, 'user_id': user_id, 'user_ip': user_ip, + params = { 'email': email, 'user_id': user_id, 'user_ip': user_ip, 'user_agent': user_agent, 'location': location } - user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/impressions', + user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/impressions', params=params) return user_dict @classmethod def get_message_threads(cls, user_id=None, email=None, thread_id=None): - """ If a thread_id is specified, this returns a specific MessageThread + """ If a thread_id is specified, this returns a specific MessageThread (if it can find one), otherwise it returns all MessageThreads for the - particular user. + particular user. >>> message_threads = Intercom.get_message_threads(email="somebody@example.com") >>> type(message_threads) @@ -196,15 +200,15 @@ def get_message_threads(cls, user_id=None, email=None, thread_id=None): """ params = { 'email': email, 'user_id': user_id, 'thread_id': thread_id } - msg_dict = Intercom._call('GET', Intercom.api_endpoint + 'users/message_threads', - params=params) + msg_dict = Intercom._call('GET', Intercom.api_endpoint + 'users/message_threads', + params=params) return msg_dict @classmethod def create_message_thread(cls, user_id=None, email=None, body=None): - """ Create a MessageThread. + """ Create a MessageThread. - >>> message_thread = Intercom.create_message_thread(email="somebody@example.com", + >>> message_thread = Intercom.create_message_thread(email="somebody@example.com", ... body="Uh, everything's under control. Situation normal.") >>> message_thread['thread_id'] 5591 @@ -215,16 +219,16 @@ def create_message_thread(cls, user_id=None, email=None, body=None): """ params = { 'email': email, 'user_id': user_id, 'body': body } - user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/message_threads', - params=params) + user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/message_threads', + params=params) return user_dict @classmethod - def reply_message_thread(cls, user_id=None, email=None, thread_id=None, + def reply_message_thread(cls, user_id=None, email=None, thread_id=None, body=None, read=None): """ Reply to the specific thread. - >>> message_thread = Intercom.reply_message_thread(email="somebody@example.com", + >>> message_thread = Intercom.reply_message_thread(email="somebody@example.com", ... thread_id=5591, body="If you're not talking to me you must be talking to someone") >>> len(message_thread) 6 @@ -234,8 +238,8 @@ def reply_message_thread(cls, user_id=None, email=None, thread_id=None, 2 """ - params = { 'email': email, 'user_id': user_id, 'thread_id': thread_id, + params = { 'email': email, 'user_id': user_id, 'thread_id': thread_id, 'body': body, 'read': read } - user_dict = Intercom._call('PUT', Intercom.api_endpoint + 'users/message_threads', + user_dict = Intercom._call('PUT', Intercom.api_endpoint + 'users/message_threads', params=params) return user_dict diff --git a/intercom/user.py b/intercom/user.py index 9dbb7930..b24e3ede 100644 --- a/intercom/user.py +++ b/intercom/user.py @@ -97,7 +97,8 @@ def find_by_user_id(cls, user_id): @classmethod def create(cls, user_id=None, email=None, name=None, created_at=None, custom_data=None, last_seen_ip=None, last_seen_user_agent=None, - companies=None): + companies=None, last_request_at=None, + last_impression_at=None): """ Create or update a user. >>> user = User.create(email="somebody@example.com") @@ -108,7 +109,8 @@ def create(cls, user_id=None, email=None, name=None, created_at=None, resp = Intercom.create_user(user_id=user_id, email=email, name=name, created_at=created_at, custom_data=custom_data, last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent, - companies=companies) + companies=companies, last_request_at=last_request_at, + last_impression_at=last_impression_at) return cls(resp) @classmethod