forked from singer-io/tap-sailthru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.py
483 lines (402 loc) · 17.1 KB
/
streams.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
"""
This module defines the stream classes and their individual sync logic.
"""
import csv
import datetime
import time
from datetime import date, timedelta
from functools import lru_cache
from typing import Any, Iterator
import requests
import singer
from singer import Transformer, metrics
from tap_sailthru.client import SailthruClient, SailthruClientError
from tap_sailthru.transform import (flatten_user_response,
get_start_and_end_date_params,
rfc2822_to_datetime,
transform_keys_to_snake_case)
LOGGER = singer.get_logger()
# pylint: disable=missing-class-docstring
class SailthruJobTimeoutError(Exception):
pass
class BaseStream:
"""
A base class representing singer streams.
:param client: The API client used extract records from the external source
"""
object_type = None
tap_stream_id = None
replication_method = None
replication_key = None
key_properties = []
valid_replication_keys = []
params = {}
parent = None
date_keys = []
def __init__(self, client: SailthruClient):
self.client = client
def get_records(self, bookmark_datetime: datetime = None, is_parent: bool = False) -> list:
"""
Returns a list of records for that stream.
:param bookmark_datetime: The datetime object representing the
bookmark date
:param is_parent: If true, may change the type of data
that is returned for a child stream to consume
:return: list of records
"""
raise NotImplementedError("Child classes of BaseStream require "
"`get_records` implementation")
def get_parent_data(self, bookmark_datetime: datetime = None) -> list:
"""
Returns a list of records from the parent stream.
:param bookmark_datetime: The datetime object representing the
bookmark date
:return: A list of records
"""
# pylint: disable=not-callable
parent = self.parent(self.client)
return parent.get_records(bookmark_datetime, is_parent=True)
def post_job(self, parameter: Any = None) -> dict:
"""
Creates a data export background job. More details:
https://getstarted.sailthru.com/developers/api/job/
:param parameter: Any parameter type to be passed to the logger
:return: The API response as a dictionary
"""
job_name = self.params.get('job')
if parameter:
# pylint: disable=logging-fstring-interpolation
LOGGER.info(f'Starting background job for {job_name},'
f'parameter={parameter}')
else:
# pylint: disable=logging-fstring-interpolation
LOGGER.info(f'Starting background job for {job_name}')
return self.client.create_job(self.params)
def get_job_url(self, job_id: str, timeout: int = 600) -> str:
"""
Polls the /job endpoint and checks to see if export job is completed.
Returns the export URL when job is ready.
:param job_id: the job_id to poll
:param timeout: the default timeout (seconds) before halting request
:return: the export URL
"""
status = ''
job_start_time = singer.utils.now()
while status != 'completed':
response = self.client.get_job({'job_id': job_id})
status = response.get('status')
# pylint: disable=logging-fstring-interpolation
LOGGER.info(f'Job report status: {status}')
now = singer.utils.now()
if (now - job_start_time).seconds > timeout:
# pylint: disable=logging-fstring-interpolation
LOGGER.critical(f'Request with job_id {job_id}'
f' exceeded {timeout} second timeout'
f'latest_status: {status}')
raise SailthruJobTimeoutError
time.sleep(1)
return response.get('export_url')
@staticmethod
def process_job_csv(export_url: str,
chunk_size: int = 1024,
parent_params: dict = None) -> Iterator[dict]:
"""
Fetches CSV from URL and streams each line.
:param export_url: The URL from which to fetch the CSV data from
:param chunk_size: The chunk size to read per line
:param parent_params: A dictionary with "parent" parameters to append
to each record
:return: A generator of a dictionary
"""
with requests.get(export_url, stream=True) as req:
reader = csv.DictReader(line.decode('utf-8') for line
in req.iter_lines(chunk_size=chunk_size))
for row in reader:
if parent_params:
row.update(parent_params)
yield row
def date_records_to_datetime(self, record):
for key in self.date_keys:
if key in record:
if record[key]:
record[key] = rfc2822_to_datetime(record[key]).isoformat()
# pylint: disable=abstract-method
class IncrementalStream(BaseStream):
"""
A child class of a base stream used to represent streams that use the
INCREMENTAL replication method.
:param client: The API client used extract records from the external source
"""
replication_method = 'INCREMENTAL'
# pylint: disable=too-many-arguments
def sync(self,
state: dict,
stream_schema: dict,
stream_metadata: dict,
config: dict,
transformer: Transformer) -> dict:
"""
The sync logic for an incremental stream.
:param state: A dictionary representing singer state
:param stream_schema: A dictionary containing the stream schema
:param stream_metadata: A dictionnary containing stream metadata
:param config: A dictionary containing tap config data
:param transformer: A singer Transformer object
:return: State data in the form of a dictionary
"""
start_date = singer.get_bookmark(state,
self.tap_stream_id,
self.replication_key,
config['start_date'])
bookmark_datetime = singer.utils.strptime_to_utc(start_date)
max_datetime = bookmark_datetime
with metrics.record_counter(self.tap_stream_id) as counter:
for record in self.get_records(bookmark_datetime):
self.date_records_to_datetime(record)
transform_keys_to_snake_case(record)
record_datetime = singer.utils.strptime_to_utc(record[self.replication_key])
if record_datetime >= bookmark_datetime:
transformed_record = transformer.transform(record,
stream_schema,
stream_metadata)
singer.write_record(self.tap_stream_id, transformed_record)
counter.increment()
max_datetime = max(record_datetime, max_datetime)
bookmark_date = singer.utils.strftime(max_datetime)
state = singer.write_bookmark(state,
self.tap_stream_id,
self.replication_key,
bookmark_date)
return state
# pylint: disable=abstract-method
class FullTableStream(BaseStream):
"""
A child class of a base stream used to represent streams that use the
FULL_TABLE replication method.
:param client: The API client used extract records from the external source
"""
replication_method = 'FULL_TABLE'
# pylint: disable=too-many-arguments
def sync(self,
state: dict,
stream_schema: dict,
stream_metadata: dict,
config: dict,
transformer: Transformer) -> dict:
"""
The sync logic for an full table stream.
:param state: A dictionary representing singer state
:param stream_schema: A dictionary containing the stream schema
:param stream_metadata: A dictionnary containing stream metadata
:param config: A dictionary containing tap config data
:param transformer: A singer Transformer object
:return: State data in the form of a dictionary
"""
with metrics.record_counter(self.tap_stream_id) as counter:
for record in self.get_records():
transform_keys_to_snake_case(record)
transformed_record = transformer.transform(record, stream_schema, stream_metadata)
singer.write_record(self.tap_stream_id, transformed_record)
counter.increment()
return state
class AdTargeterPlans(FullTableStream):
"""
Gets records for Sailthru Ad Targeter Plans.
Docs: https://getstarted.sailthru.com/developers/api/ad-plan/
"""
tap_stream_id = 'ad_targeter_plans'
key_properties = ['plan_id']
def get_records(self, bookmark_datetime=None, is_parent=False):
response = self.client.get_ad_targeter_plans()
if not response.get('ad_plans'):
LOGGER.critical('response is empty for ad_plans')
raise SailthruClientError
yield from response['ad_plans']
class Blasts(IncrementalStream):
"""
Gets records for blasts (aka Sailtrhu campaigns).
Docs: https://getstarted.sailthru.com/developers/api/blast/
This endpoint does not have a method of retrieving all blasts available.
You can only retrieve blast information by specifying a blast_id or you
can get all blasts by status type (sent, sending, scheduled, unscheduled).
"""
tap_stream_id = 'blasts'
key_properties = ['blast_id']
replication_key = 'modify_time'
valid_replication_keys = ['modify_time']
params = {
'statuses': ['sent', 'sending', 'unscheduled', 'scheduled'],
}
date_keys = ['start_time', 'modify_time', 'schedule_time']
def get_records(self, bookmark_datetime=None, is_parent=False):
# Will just return a list of blast_id if being called
# by child stream
if is_parent:
for status in self.params['statuses']:
response = self.client.get_blasts({'status': status})
yield from (blast.get('blast_id') for blast in response.get('blasts'))
else:
for status in self.params['statuses']:
response = self.client.get_blasts({'status': status})
# Add the blast status to each blast record
yield from (dict(item, status=status) for item in response.get('blasts'))
class BlastQuery(FullTableStream):
"""
Triggers asynchronous data export job for blasts and then fetches the
records when the job is done.
Docs: https://getstarted.sailthru.com/developers/api/job/#blast-query
"""
tap_stream_id = 'blast_query'
key_properties = ['profile_id', 'blast_id']
params = {
'job': 'blast_query',
'blast_id': '{blast_id}',
}
parent = Blasts
date_keys = ['send_time',
'open_time',
'click_time',
'purchase_time',
'first_ten_clicks_time']
def get_records(self, bookmark_datetime=None, is_parent=False):
for blast_id in self.get_parent_data():
self.params['blast_id'] = blast_id
response = self.post_job(parameter=blast_id)
if response.get("error"):
# https://getstarted.sailthru.com/developers/api/job/#Error_Codes
# Error code 99 = You may not export a blast that has been sent
# pylint: disable=logging-fstring-interpolation
LOGGER.info(f"Skipping blast_id: {blast_id}")
continue
export_url = self.get_job_url(job_id=response['job_id'])
# Add blast id to each record
yield from self.process_job_csv(export_url=export_url,
parent_params={'blast_id': blast_id})
class BlastRepeats(IncrementalStream):
"""
Gets all recurring campaigns.
Docs: https://getstarted.sailthru.com/developers/api/blast_repeat/
"""
tap_stream_id = 'blast_repeats'
key_properties = ['repeat_id']
replication_key = 'modify_time'
valid_replication_keys = ['modify_time']
date_keys = ['create_time',
'modify_time',
'start_date',
'end_date',
'error_time']
def get_records(self, bookmark_datetime=None, is_parent=False):
response = self.client.get_blast_repeats()
if not response.get('repeats'):
LOGGER.critical("response is empty for blast_repeats")
raise SailthruClientError
yield from response.get('repeats')
class Lists(FullTableStream):
"""
Retrieves info for Sailthru lists.
Docs: https://getstarted.sailthru.com/developers/api/list/
"""
tap_stream_id = 'lists'
key_properties = ['list_id']
date_keys = ['create_time']
# pylint: disable=missing-function-docstring
@lru_cache
def get_lists(self):
return self.client.get_lists()
def get_records(self, bookmark_datetime=None, is_parent=False):
response = self.get_lists()
if not response.get('lists'):
LOGGER.critical("response is empty for lists")
raise SailthruClientError
# Will just return list names if called by child stream
if is_parent:
for record in response['lists']:
yield record['name']
else:
yield from response['lists']
class BlastSaveList(FullTableStream):
"""
Triggers asynchronous data export job for blast_save_query and then
fetches the records when the job is done.
Docs: https://getstarted.sailthru.com/developers/api/job/#blast-save-list
"""
tap_stream_id = 'blast_save_list'
key_properties = ['profile_id']
params = {
'job': 'export_list_data',
'list': '{list_name}',
}
parent = Lists
date_keys = ['profile_created_date',
'optout_time',
'first_purchase_time',
'last_purchase_time',
'profile_created_date']
def get_records(self, bookmark_datetime=None, is_parent=False):
for list_name in self.get_parent_data():
self.params['list'] = list_name
response = self.post_job(parameter=self.params['list'])
export_url = self.get_job_url(job_id=response['job_id'])
yield from self.process_job_csv(export_url=export_url)
class Users(FullTableStream):
"""
Retrieve user profile data.
Docs: https://getstarted.sailthru.com/developers/api/user/
"""
tap_stream_id = 'users'
key_properties = ['profile_id']
params = {
'id': '{profile_id}',
'key': 'sid',
}
parent = BlastSaveList
def get_records(self,
bookmark_datetime: datetime = None,
is_parent: bool = None):
for record in self.get_parent_data():
if not record.get('Profile Id'):
LOGGER.critical('no Profile Id for record')
continue
profile_id = record['Profile Id']
response = self.client.get_user({'id': profile_id})
yield flatten_user_response(response)
class PurchaseLog(IncrementalStream):
"""
Triggers asynchronous data export job for export_purchase_log and then
fetches the records when the job is done.
Docs: https://getstarted.sailthru.com/developers/api/job/#export-purchase-log
"""
tap_stream_id = 'purchase_log'
key_properties = ['date', 'email_hash', 'extid', 'message_id', 'price', 'channel']
replication_key = 'date'
valid_replication_keys = ['date']
params = {
'job': 'export_purchase_log',
'start_date': '{purchase_log_start_date}',
'end_date': '{purchase_log_end_date}',
}
date_keys = ['date']
def get_records(self, bookmark_datetime=None, is_parent=False):
start_datetime, end_datetime = get_start_and_end_date_params(bookmark_datetime)
now = singer.utils.now()
# Generate a report for each day up until the today's date
while start_datetime.date() <= now.date():
job_date = start_datetime.strftime('%Y%m%d')
self.params['start_date'] = job_date
self.params['end_date'] = job_date
response = self.post_job(parameter=(self.params['start_date'],
self.params['end_date']))
export_url = self.get_job_url(job_id=response['job_id'])
yield from self.process_job_csv(export_url=export_url)
start_datetime += timedelta(days=1)
STREAMS = {
'ad_targeter_plans': AdTargeterPlans,
'blasts': Blasts,
'blast_query': BlastQuery,
'blast_repeats': BlastRepeats,
'lists': Lists,
'blast_save_list': BlastSaveList,
'users': Users,
'purchase_log': PurchaseLog,
}