Skip to content

Commit f7aadaa

Browse files
committed
docstrings and doctests for intercom.py
pylint'd too.
1 parent 8bcabb1 commit f7aadaa

File tree

1 file changed

+99
-10
lines changed

1 file changed

+99
-10
lines changed

intercom/intercom.py

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,38 @@
33
#
44
# License: http://jkeyes.mit-license.org/
55
#
6+
""" intercom module
7+
8+
All of the API requests are created, and the API responses are parsed here.
9+
10+
>>> from intercom import Intercom
11+
>>> Intercom.app_id = 'dummy-app-id'
12+
>>> Intercom.api_key = 'dummy-api-key'
13+
14+
"""
615

716
import functools
817
import json
918
import requests
10-
import sys
1119

1220
DEFAULT_TIMEOUT = 10 # seconds
1321

1422
class IntercomError(StandardError):
23+
""" Base error. """
1524
def __init__(self, message, result=None):
1625
super(IntercomError, self).__init__(message)
1726
self.result = result
1827

1928
class AuthenticationError(IntercomError):
29+
""" Raised when a request cannot be authenticated by the API. """
2030
pass
2131

2232
class ResourceNotFound(IntercomError):
33+
""" Raised when a resource cannot be found e.g. a non-existant User. """
2334
pass
2435

2536
class ServerError(IntercomError):
37+
""" Raised when the API returns an error other than an auth or not found. """
2638
pass
2739

2840
def api_call(func_to_decorate):
@@ -57,6 +69,7 @@ class Intercom(object):
5769
@classmethod
5870
@api_call
5971
def _call(cls, method, url, params=None):
72+
""" Construct an API request, send it to the API, and parse the response. """
6073
req_params = {}
6174
headers = { 'User-Agent': 'python-intercom/0.1', 'Accept': 'application/json' }
6275
if method in ('POST', 'PUT'):
@@ -80,15 +93,27 @@ def _create_or_update_user(cls, method, **kwargs):
8093

8194
@classmethod
8295
def get_users(cls):
83-
""" Return a dict for the user represented by the specified
84-
email or user_id. """
96+
""" Return a dict for the user represented by the specified email or user_id.
97+
98+
>>> result = Intercom.get_users()
99+
>>> type(result)
100+
<type 'dict'>
101+
>>> len(result['users'])
102+
3
103+
104+
"""
85105
user_dict = Intercom._call('GET', '%susers' % (Intercom.api_endpoint))
86106
return user_dict
87107

88108
@classmethod
89109
def get_user(cls, email=None, user_id=None):
90-
""" Return a dict for the user represented by the specified
91-
email or user_id. """
110+
""" Return a dict for the user represented by the specified email or user_id.
111+
112+
>>> user = Intercom.get_user(user_id='123')
113+
>>> user['name']
114+
u'Bob'
115+
116+
"""
92117

93118
params = { 'email': email, 'user_id': user_id }
94119
user_dict = Intercom._call('GET', '%susers' % (Intercom.api_endpoint), params=params)
@@ -97,23 +122,52 @@ def get_user(cls, email=None, user_id=None):
97122
@classmethod
98123
def create_user(cls, user_id=None, email=None, name=None, created_at=None,
99124
custom_data=None, last_seen_ip=None, last_seen_user_agent=None):
100-
""" Create a user. """
125+
""" Create a user from the available parameters.
126+
127+
>>> from datetime import datetime
128+
>>> import time
129+
>>> now = time.mktime(datetime.now().timetuple())
130+
>>> user = Intercom.create_user(user_id='987', email='joe@example.com',
131+
... name='Joe Example', created_at=now, last_seen_ip='10.10.10.10',
132+
... custom_data={ 'job_type': 'smuggler'})
133+
>>> user['name']
134+
u'Joe Example'
135+
>>> user['custom_data']['job_type']
136+
u'smuggler'
137+
138+
"""
101139
return Intercom._create_or_update_user('POST', user_id=user_id, email=email,
102140
name=name, created_at=created_at, custom_data=custom_data,
103141
last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent)
104142

105143
@classmethod
106144
def update_user(cls, user_id=None, email=None, name=None, created_at=None,
107145
custom_data=None, last_seen_ip=None, last_seen_user_agent=None):
108-
""" Update a user. """
146+
""" Update a user with the available parameters.
147+
148+
>>> user = Intercom.get_user(user_id='123')
149+
>>> user['name']
150+
u'Bob'
151+
>>> user = Intercom.update_user(user_id='123', name='Han')
152+
>>> user['name']
153+
u'Han'
154+
155+
"""
109156
return Intercom._create_or_update_user('PUT', user_id=user_id, email=email,
110157
name=name, created_at=created_at, custom_data=custom_data,
111158
last_seen_ip=last_seen_ip, last_seen_user_agent=last_seen_user_agent)
112159

113160
@classmethod
114161
def create_impression(cls, user_id=None, email=None, user_ip=None,
115162
user_agent=None, location=None):
116-
""" Create an impression. """
163+
""" Create an impression.
164+
165+
>>> result = Intercom.create_impression(email="somebody@example.com",
166+
... user_agent="MyApp/1.0", user_ip="2.3.4.5")
167+
>>> result['unread_messages']
168+
1
169+
170+
"""
117171
params = { 'email': email, 'user_id': user_id, 'user_ip': user_ip,
118172
'user_agent': user_agent, 'location': location }
119173
user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/impressions',
@@ -122,14 +176,38 @@ def create_impression(cls, user_id=None, email=None, user_ip=None,
122176

123177
@classmethod
124178
def get_message_threads(cls, user_id=None, email=None, thread_id=None):
179+
""" If a thread_id is specified, this returns a specific MessageThread
180+
(if it can find one), otherwise it returns all MessageThreads for the
181+
particular user.
182+
183+
>>> message_threads = Intercom.get_message_threads(email="somebody@example.com")
184+
>>> type(message_threads)
185+
<type 'list'>
186+
>>> message_thread = Intercom.get_message_threads(email="somebody@example.com",
187+
... thread_id=5591)
188+
>>> type(message_thread)
189+
<type 'dict'>
190+
191+
"""
125192
params = { 'email': email, 'user_id': user_id, 'thread_id': thread_id }
126193
msg_dict = Intercom._call('GET', Intercom.api_endpoint + 'users/message_threads',
127194
params=params)
128195
return msg_dict
129196

130197
@classmethod
131198
def create_message_thread(cls, user_id=None, email=None, body=None):
132-
""" Create an impression. """
199+
""" Create a MessageThread.
200+
201+
>>> message_thread = Intercom.create_message_thread(email="somebody@example.com",
202+
... body="Uh, everything's under control. Situation normal.")
203+
>>> message_thread['thread_id']
204+
5591
205+
>>> len(message_thread['messages'])
206+
1
207+
>>> message_thread['messages'][0]['html']
208+
u"<p>Uh, everything's under control. Situation normal.</p>"
209+
210+
"""
133211
params = { 'email': email, 'user_id': user_id, 'body': body }
134212
user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/message_threads',
135213
params=params)
@@ -138,7 +216,18 @@ def create_message_thread(cls, user_id=None, email=None, body=None):
138216
@classmethod
139217
def reply_message_thread(cls, user_id=None, email=None, thread_id=None,
140218
body=None, read=None):
141-
""" Create an impression. """
219+
""" Reply to the specific thread.
220+
221+
>>> message_thread = Intercom.reply_message_thread(email="somebody@example.com",
222+
... thread_id=5591, body="If you're not talking to me you must be talking to someone")
223+
>>> len(message_thread)
224+
6
225+
>>> message_thread['thread_id']
226+
5591
227+
>>> len(message_thread['messages'])
228+
2
229+
230+
"""
142231
params = { 'email': email, 'user_id': user_id, 'thread_id': thread_id,
143232
'body': body, 'read': read }
144233
user_dict = Intercom._call('PUT', Intercom.api_endpoint + 'users/message_threads',

0 commit comments

Comments
 (0)