This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
/
service.py
283 lines (221 loc) · 8.06 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import re
import logging
import time
import random
from copy import deepcopy
from concurrent.futures import ThreadPoolExecutor
import requests
import bleach
from funcy import retry, log_calls, pluck, chunks
logger = logging.getLogger(__name__)
session = requests.session()
TIMEOUT = 30
RETRY_COUNT = 3
def _timeout(i):
from app import app
if app.config.get('TESTING'):
return 0
return 2 ** i
def wait(wait_range=15):
wait_time = random.choice(range(wait_range))
logger.info('Wait %s seconds.', wait_time)
time.sleep(wait_time)
requests_retry = retry(RETRY_COUNT, errors=requests.RequestException,
timeout=_timeout)
class IntercomError(Exception):
""" Base exception for IntercomClient.
"""
class IntercomValidationError(IntercomError):
""" Incorrect value for one of the request's parameter.
"""
class IntercomClient:
""" Client for making requests to the Intercom's API.
Ref: https://developers.intercom.io/docs
"""
# TODO: Add validation for incoming and outgoing values
# See: https://github.com/nicolaiarocci/cerberus
base_url = 'https://api.intercom.io'
def __init__(self, app_id, api_key, workers_count=10):
self.app_id = app_id
self.api_key = api_key
self.auth = (app_id, api_key)
self.workers_count = workers_count
def get_headers(self, **extra):
default = {'Accept': 'application/json'}
default.update(extra)
return default
def get_executor(self):
return ThreadPoolExecutor(self.workers_count)
@log_calls(logger.debug)
def iter_users(self, per_page=50, order='desc'):
VALID_ORDERS = ('desc', 'asc')
if order not in VALID_ORDERS:
raise IntercomValidationError('order can by %s' % VALID_ORDERS)
@requests_retry
def _request(url):
response = session.get(url,
auth=self.auth,
headers=self.get_headers(),
timeout=TIMEOUT)
response.raise_for_status()
return response.json()
url = '{0}/users?page=1&per_page={1}&order={2}'.format(
self.base_url, per_page, order)
while True:
response = _request(url)
for user_data in response['users']:
yield user_data
if response['pages']['next']:
url = response['pages']['next']
else:
break
@log_calls(logger.debug)
def update_users(self, users_data, prefix=None):
""" Uses Intercom's bulk update.
"""
CHUNK_SIZE = 50 # Intercom's limitation
@requests_retry
def request(chunk_of_users_data):
url = '{0}/bulk/users'.format(self.base_url)
response = session.post(
url,
json={'items': [
{
'method': 'post',
'data_type': 'user',
'data': apply_prefix_for_user_data(ch, prefix)
} for ch in chunk_of_users_data]
},
auth=self.auth,
headers=self.get_headers(),
timeout=TIMEOUT)
# TODO: re-raise custom exception for 429 HTTP error
# for further handling (e.g. retry celery task)
response.raise_for_status()
result = response.json()
try:
status_url = result['links']['self']
logger.debug('Bulk update status: %s', status_url)
except KeyError:
logger.error('Weird response from Intercom: %r', result)
return result
with self.get_executor() as executor:
for _ in executor.map(request, chunks(CHUNK_SIZE, users_data)):
pass
@log_calls(logger.debug)
def update_user(self, user_data, prefix=None):
""" Update a single user
"""
@requests_retry
def request(user_data):
url = '{0}/users'.format(self.base_url)
response = session.post(
url,
json=apply_prefix_for_user_data(user_data, prefix),
auth=self.auth,
headers=self.get_headers(),
timeout=TIMEOUT)
response.raise_for_status()
return response.json()
return request(user_data)
@log_calls(logger.debug)
def create_notes(self, data, force=False):
url = '{0}/notes'.format(self.base_url)
@requests_retry
def request(row):
wait()
response = session.post(
url,
json=dict(user={'user_id': row['user_id']}, body=row['body']),
auth=self.auth,
headers=self.get_headers(),
)
response.raise_for_status()
return response.json()
def iter_data(data):
if force:
return iter(data)
# Filter notes for excluding duplicates
exist_notes = self.get_notes(pluck('user_id', data))
for row in data:
user_id, body = str(row['user_id']), row['body']
if user_id not in exist_notes:
yield row
continue
bodies = map(normalize_note,
pluck('body', exist_notes[user_id]))
if normalize_note(body) not in bodies:
yield row
continue
logger.debug(
'The note with this body already exists: %r', row)
with ThreadPoolExecutor(self.workers_count) as executor:
for _ in executor.map(request, iter_data(data)):
pass
@log_calls(logger.debug)
def get_notes(self, users_ids):
""" Fetch notes for users. Returns a dict like: {user_id: [note, note]}
"""
url = '{0}/notes'.format(self.base_url)
@requests_retry
def request(user_id):
response = session.get(
url,
data={'user_id': user_id},
auth=self.auth,
headers=self.get_headers(),
)
response.raise_for_status()
result = response.json()
result['user_id'] = str(user_id)
return result
result = {}
with ThreadPoolExecutor(self.workers_count) as executor:
for row in executor.map(request, users_ids):
result[row['user_id']] = row['notes']
return result
@log_calls(logger.debug)
def subscribe(self, hook_url, topics):
url = '{0}/subscriptions'.format(self.base_url)
@requests_retry
def request(hook_url, topics):
response = session.post(
url,
json=dict(
service_type='web',
url=hook_url,
topics=topics
),
auth=self.auth,
headers=self.get_headers()
)
response.raise_for_status()
return response.json()
return request(hook_url, topics)
@log_calls(logger.debug)
def unsubscribe(self, subscription_id):
url = '{0}/subscriptions/{1}'.format(self.base_url, subscription_id)
@requests_retry
def request(url):
response = session.delete(
url,
auth=self.auth,
headers=self.get_headers()
)
response.raise_for_status()
return response.json()
return request(url)
def apply_prefix_for_user_data(user_data, prefix=None):
if not prefix or 'custom_attributes' not in user_data:
return user_data
user_data = deepcopy(user_data)
user_data['custom_attributes'] = {
'_'.join((prefix, k)): v
for k, v in user_data['custom_attributes'].items()
}
return user_data
def normalize_note(body):
""" Used for comparing notes.
"""
body = bleach.clean(body, strip=True)
return re.sub('\s', '', body).lower()