/
streams.py
417 lines (340 loc) · 14.9 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
from datetime import timedelta
import singer
from methodtools import lru_cache
from requests.exceptions import RequestException
LOGGER = singer.get_logger()
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def get_date_windows(start_time):
window_start = singer.utils.strptime_to_utc(start_time)
now = singer.utils.now()
while window_start < now:
window_end = window_start + timedelta(days=7)
if window_end > now:
window_end = now
yield singer.utils.strftime(window_start), singer.utils.strftime(window_end)
window_start = window_end
class Stream:
def __init__(self, client):
self.client = client
class CatalogStream(Stream):
object_type = None
tap_stream_id = None
replication_key = None
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = singer.get_bookmark(state, self.tap_stream_id, self.replication_key, config['start_date'])
max_record_value = start_time
for page, _ in self.client.get_catalog(self.object_type, start_time):
for record in page:
transformed_record = transformer.transform(record, stream_schema, stream_metadata)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
if record[self.replication_key] > max_record_value:
max_record_value = transformed_record[self.replication_key]
state = singer.write_bookmark(state, self.tap_stream_id, self.replication_key, max_record_value)
singer.write_state(state)
return state
class FullTableStream(Stream):
tap_stream_id = None
key_properties = []
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
def get_pages_safe(self, state, bookmarked_cursor, start_time):
try:
yield from self.get_pages(bookmarked_cursor, start_time)
except (RuntimeError, RequestException):
# NB> If we get a non-retryable error we should delete the
# pagination cursor bookmark before re-raising the exception.
LOGGER.fatal("Received fatal exception during syncing of stream %s, Clearing cursor bookmark.", self.tap_stream_id)
state = singer.clear_bookmark(state, self.tap_stream_id, 'cursor')
singer.write_state(state)
raise
def get_pages(self, bookmarked_cursor, start_time):
raise NotImplementedError("Child classes of FullTableStreams require `get_pages` implementation")
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = singer.get_bookmark(state, self.tap_stream_id, self.replication_key, config['start_date'])
bookmarked_cursor = singer.get_bookmark(state, self.tap_stream_id, 'cursor')
for page, cursor in self.get_pages_safe(state, bookmarked_cursor, start_time):
for record in page:
transformed_record = transformer.transform(record, stream_schema, stream_metadata)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
singer.write_bookmark(state, self.tap_stream_id, 'cursor', cursor)
singer.write_state(state)
state = singer.clear_bookmark(state, self.tap_stream_id, 'cursor')
singer.write_state(state)
return state
class Items(CatalogStream):
tap_stream_id = 'items'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
object_type = 'ITEM'
class Categories(CatalogStream):
tap_stream_id = 'categories'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
object_type = 'CATEGORY'
class Discounts(CatalogStream):
tap_stream_id = 'discounts'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
object_type = 'DISCOUNT'
class Taxes(CatalogStream):
tap_stream_id = 'taxes'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
object_type = 'TAX'
class Employees(FullTableStream):
tap_stream_id = 'employees'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_employees(bookmarked_cursor)
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = config['start_date']
bookmarked_cursor = singer.get_bookmark(state, self.tap_stream_id, 'cursor')
for page, cursor in self.get_pages_safe(state, bookmarked_cursor, start_time):
for record in page:
if record['updated_at'] >= start_time:
transformed_record = transformer.transform(record, stream_schema, stream_metadata)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
singer.write_bookmark(state, self.tap_stream_id, 'cursor', cursor)
singer.write_state(state)
state = singer.clear_bookmark(state, self.tap_stream_id, 'cursor')
singer.write_state(state)
return state
class ModifierLists(CatalogStream):
tap_stream_id = 'modifier_lists'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
object_type = 'MODIFIER_LIST'
class Locations(FullTableStream):
tap_stream_id = 'locations'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
@lru_cache()
@classmethod
def get_all_location_ids(cls, client):
all_location_ids = list()
for page, _ in client.get_locations():
for location in page:
all_location_ids.append(location['id'])
return all_location_ids
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_locations()
class BankAccounts(FullTableStream):
tap_stream_id = 'bank_accounts'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
object_type = 'BANK ACCOUNTS'
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_bank_accounts()
class Refunds(FullTableStream):
tap_stream_id = 'refunds'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
object_type = 'REFUND'
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_refunds(start_time, bookmarked_cursor)
class Payments(FullTableStream):
tap_stream_id = 'payments'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
object_type = 'PAYMENT'
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_payments(start_time, bookmarked_cursor)
class Orders(Stream):
tap_stream_id = 'orders'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
object_type = 'ORDER'
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = singer.get_bookmark(state, self.tap_stream_id, self.replication_key, config['start_date'])
max_record_value = start_time
all_location_ids = Locations.get_all_location_ids(self.client)
for location_ids_chunk in chunks(all_location_ids, 10):
# orders requests can only take up to 10 location_ids at a time
for page, _ in self.client.get_orders(location_ids_chunk, start_time):
for record in page:
transformed_record = transformer.transform(record, stream_schema, stream_metadata)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
if record[self.replication_key] > max_record_value:
max_record_value = transformed_record[self.replication_key]
state = singer.write_bookmark(state, self.tap_stream_id, self.replication_key, max_record_value)
singer.write_state(state)
return state
class Inventories(FullTableStream):
tap_stream_id = 'inventories'
key_properties = []
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_inventories(start_time, bookmarked_cursor)
class Shifts(FullTableStream):
tap_stream_id = 'shifts'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_shifts(bookmarked_cursor)
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = singer.get_bookmark(state, self.tap_stream_id, self.replication_key, config['start_date'])
sync_start_bookmark = singer.get_bookmark(
state,
self.tap_stream_id,
'sync_start',
singer.utils.strftime(singer.utils.now(),
format_str=singer.utils.DATETIME_PARSE)
)
state = singer.write_bookmark(
state,
self.tap_stream_id,
'sync_start',
sync_start_bookmark,
)
bookmarked_cursor = singer.get_bookmark(state, self.tap_stream_id, 'cursor')
for page, cursor in self.get_pages_safe(state, bookmarked_cursor, start_time):
for record in page:
if record[self.replication_key] >= start_time:
transformed_record = transformer.transform(
record, stream_schema, stream_metadata,
)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
state = singer.write_bookmark(state, self.tap_stream_id, 'cursor', cursor)
singer.write_state(state)
state = singer.clear_bookmark(state, self.tap_stream_id, 'sync_start')
state = singer.clear_bookmark(state, self.tap_stream_id, 'cursor')
state = singer.write_bookmark(
state,
self.tap_stream_id,
self.replication_key,
sync_start_bookmark,
)
singer.write_state(state)
return state
class Roles(FullTableStream):
# Square Docs: you must use Connect V1 to manage employees and employee roles.
tap_stream_id = 'roles'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
def get_pages(self, bookmarked_cursor, start_time):
yield from self.client.get_roles(bookmarked_cursor)
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = config['start_date']
bookmarked_cursor = singer.get_bookmark(state, self.tap_stream_id, 'cursor')
for page, cursor in self.get_pages_safe(state, bookmarked_cursor, start_time):
for record in page:
if record['updated_at'] >= start_time:
transformed_record = transformer.transform(
record, stream_schema, stream_metadata,
)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
singer.write_bookmark(state, self.tap_stream_id, 'cursor', cursor)
singer.write_state(state)
state = singer.clear_bookmark(state, self.tap_stream_id, 'cursor')
singer.write_state(state)
return state
class CashDrawerShifts(FullTableStream):
tap_stream_id = 'cash_drawer_shifts'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
def get_pages(self, bookmarked_cursor, start_time):
for location_id in Locations.get_all_location_ids(self.client):
# Cash Drawer Shifts requests can only take up to 1 location_id at a time
yield from self.client.get_cash_drawer_shifts(location_id, start_time, bookmarked_cursor)
class Settlements(FullTableStream):
tap_stream_id = 'settlements'
key_properties = ['id']
replication_method = 'FULL_TABLE'
valid_replication_keys = []
replication_key = None
def get_pages(self, bookmarked_cursor, start_time):
for location_id in Locations.get_all_location_ids(self.client):
# Settlements requests can only take up to 1 location_id at a time
yield from self.client.get_settlements(location_id, start_time, bookmarked_cursor)
class Customers(Stream):
tap_stream_id = 'customers'
key_properties = ['id']
replication_method = 'INCREMENTAL'
valid_replication_keys = ['updated_at']
replication_key = 'updated_at'
def sync(self, state, stream_schema, stream_metadata, config, transformer):
start_time = singer.get_bookmark(state, self.tap_stream_id, self.replication_key, config['start_date'])
for window_start, window_end in get_date_windows(start_time):
LOGGER.info("Searching for customers from %s to %s", window_start, window_end)
for page, _ in self.client.get_customers(window_start, window_end):
for record in page:
transformed_record = transformer.transform(record, stream_schema, stream_metadata)
singer.write_record(
self.tap_stream_id,
transformed_record,
)
state = singer.write_bookmark(state, self.tap_stream_id, self.replication_key, window_end)
singer.write_state(state)
return state
STREAMS = {
'items': Items,
'categories': Categories,
'discounts': Discounts,
'taxes': Taxes,
'employees': Employees,
'locations': Locations,
'bank_accounts': BankAccounts,
'refunds': Refunds,
'payments': Payments,
'modifier_lists': ModifierLists,
'inventories': Inventories,
'orders': Orders,
'roles': Roles,
'shifts': Shifts,
'cash_drawer_shifts': CashDrawerShifts,
'settlements': Settlements,
'customers': Customers
}