3
3
#
4
4
# License: http://jkeyes.mit-license.org/
5
5
#
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
+ """
6
15
7
16
import functools
8
17
import json
9
18
import requests
10
- import sys
11
19
12
20
DEFAULT_TIMEOUT = 10 # seconds
13
21
14
22
class IntercomError (StandardError ):
23
+ """ Base error. """
15
24
def __init__ (self , message , result = None ):
16
25
super (IntercomError , self ).__init__ (message )
17
26
self .result = result
18
27
19
28
class AuthenticationError (IntercomError ):
29
+ """ Raised when a request cannot be authenticated by the API. """
20
30
pass
21
31
22
32
class ResourceNotFound (IntercomError ):
33
+ """ Raised when a resource cannot be found e.g. a non-existant User. """
23
34
pass
24
35
25
36
class ServerError (IntercomError ):
37
+ """ Raised when the API returns an error other than an auth or not found. """
26
38
pass
27
39
28
40
def api_call (func_to_decorate ):
@@ -57,6 +69,7 @@ class Intercom(object):
57
69
@classmethod
58
70
@api_call
59
71
def _call (cls , method , url , params = None ):
72
+ """ Construct an API request, send it to the API, and parse the response. """
60
73
req_params = {}
61
74
headers = { 'User-Agent' : 'python-intercom/0.1' , 'Accept' : 'application/json' }
62
75
if method in ('POST' , 'PUT' ):
@@ -80,15 +93,27 @@ def _create_or_update_user(cls, method, **kwargs):
80
93
81
94
@classmethod
82
95
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
+ """
85
105
user_dict = Intercom ._call ('GET' , '%susers' % (Intercom .api_endpoint ))
86
106
return user_dict
87
107
88
108
@classmethod
89
109
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
+ """
92
117
93
118
params = { 'email' : email , 'user_id' : user_id }
94
119
user_dict = Intercom ._call ('GET' , '%susers' % (Intercom .api_endpoint ), params = params )
@@ -97,23 +122,52 @@ def get_user(cls, email=None, user_id=None):
97
122
@classmethod
98
123
def create_user (cls , user_id = None , email = None , name = None , created_at = None ,
99
124
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
+ """
101
139
return Intercom ._create_or_update_user ('POST' , user_id = user_id , email = email ,
102
140
name = name , created_at = created_at , custom_data = custom_data ,
103
141
last_seen_ip = last_seen_ip , last_seen_user_agent = last_seen_user_agent )
104
142
105
143
@classmethod
106
144
def update_user (cls , user_id = None , email = None , name = None , created_at = None ,
107
145
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
+ """
109
156
return Intercom ._create_or_update_user ('PUT' , user_id = user_id , email = email ,
110
157
name = name , created_at = created_at , custom_data = custom_data ,
111
158
last_seen_ip = last_seen_ip , last_seen_user_agent = last_seen_user_agent )
112
159
113
160
@classmethod
114
161
def create_impression (cls , user_id = None , email = None , user_ip = None ,
115
162
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
+ """
117
171
params = { 'email' : email , 'user_id' : user_id , 'user_ip' : user_ip ,
118
172
'user_agent' : user_agent , 'location' : location }
119
173
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,
122
176
123
177
@classmethod
124
178
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
+ """
125
192
params = { 'email' : email , 'user_id' : user_id , 'thread_id' : thread_id }
126
193
msg_dict = Intercom ._call ('GET' , Intercom .api_endpoint + 'users/message_threads' ,
127
194
params = params )
128
195
return msg_dict
129
196
130
197
@classmethod
131
198
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
+ """
133
211
params = { 'email' : email , 'user_id' : user_id , 'body' : body }
134
212
user_dict = Intercom ._call ('POST' , Intercom .api_endpoint + 'users/message_threads' ,
135
213
params = params )
@@ -138,7 +216,18 @@ def create_message_thread(cls, user_id=None, email=None, body=None):
138
216
@classmethod
139
217
def reply_message_thread (cls , user_id = None , email = None , thread_id = None ,
140
218
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
+ """
142
231
params = { 'email' : email , 'user_id' : user_id , 'thread_id' : thread_id ,
143
232
'body' : body , 'read' : read }
144
233
user_dict = Intercom ._call ('PUT' , Intercom .api_endpoint + 'users/message_threads' ,
0 commit comments