This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
forked from singer-io/tap-exacttarget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_extensions.py
347 lines (265 loc) · 12.5 KB
/
data_extensions.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
import hashlib
import FuelSDK
import copy
import singer
from funcy import set_in, update_in, merge
from urllib.parse import urlencode
import requests
import backoff
from tap_exacttarget.client import request
from tap_exacttarget.dao import (DataAccessObject)
from tap_exacttarget.state import incorporate, save_state, \
get_last_record_value_for_table
from tap_exacttarget.util import sudsobj_to_dict
LOGGER = singer.get_logger() # noqa
def _merge_in(collection, path, new_item):
return update_in(collection, path, lambda x: merge(x, [new_item]))
def _convert_extension_datatype(datatype):
if datatype == 'Boolean':
return 'boolean'
elif datatype == 'Decimal':
return 'number'
elif datatype == 'Number':
return 'integer'
return 'string'
def _get_tap_stream_id(extension):
extension_name = extension.CustomerKey
return 'data_extension.{}'.format(extension_name)
def _get_extension_name_from_tap_stream_id(tap_stream_id):
return tap_stream_id.split('.')[1]
class DataExtensionDataAccessObject(DataAccessObject):
@classmethod
def matches_catalog(cls, catalog):
return 'data_extension.' in catalog.get('stream')
def _get_extensions(self):
result = request(
'DataExtension',
FuelSDK.ET_DataExtension,
self.auth_stub,
props=['CustomerKey', 'Name'],
batch_size=int(self.config.get('batch_size', 2500))
)
to_return = {}
for extension in result:
extension_name = str(extension.Name)
customer_key = str(extension.CustomerKey)
to_return[customer_key] = {
'tap_stream_id': 'data_extension.{}'.format(customer_key),
'stream': 'data_extension.{}'.format(extension_name),
'replication_method': self.get_replication_method(),
'key_properties': ['_CustomObjectKey'],
'schema': {
'type': 'object',
'properties': {
'_CustomObjectKey': {
'type': ['null', 'string'],
'description': ('Hidden auto-incrementing primary '
'key for data extension rows.'),
},
'CategoryID': {
'type': ['null', 'integer'],
'description': ('Specifies the identifier of the '
'folder. (Taken from the parent '
'data extension.)')
}
}
},
'metadata': [{'breadcrumb': (), 'metadata': {
'inclusion':'available'}},
{'breadcrumb': ('properties', '_CustomObjectKey'),
'metadata': {'inclusion':'available'}},
{'breadcrumb': ('properties', 'CategoryID'),
'metadata': {'inclusion':'available'}}]
}
return to_return
def _get_fields(self, extensions):
to_return = extensions.copy()
result = request(
'DataExtensionField',
FuelSDK.ET_DataExtension_Column,
self.auth_stub)
for field in result:
extension_id = field.DataExtension.CustomerKey
field = sudsobj_to_dict(field)
field_name = field['Name']
if field.get('IsPrimaryKey'):
to_return = _merge_in(
to_return,
[extension_id, 'key_properties'],
field_name)
field_schema = {
'type': [
'null',
_convert_extension_datatype(str(field.get('FieldType')))
],
'description': str(field.get('Description')),
}
to_return = set_in(
to_return,
[extension_id, 'schema', 'properties', field_name],
field_schema)
# These fields are defaulted into the schema, do not add to metadata again.
if field_name not in {'_CustomObjectKey', 'CategoryID'}:
to_return[extension_id]['metadata'].append({
'breadcrumb': ('properties', field_name),
'metadata': {'inclusion': 'available'}
})
return to_return
def generate_catalog(self):
# get all the data extensions by requesting all the fields
extensions_catalog = self._get_extensions()
extensions_catalog_with_fields = self._get_fields(extensions_catalog)
return extensions_catalog_with_fields.values()
def parse_object(self, obj):
properties = obj.get('Properties', {}).get('Property', {})
to_return = {}
for prop in properties:
to_return[prop['Name']] = prop['Value']
return to_return
def filter_keys_and_parse(self, obj):
row = obj
to_return = {}
obj_schema = self.catalog['schema']['properties']
keys_match = {key.lower(): key for key in obj_schema.keys()}
for k, v in row.items():
field_schema = obj_schema.get(keys_match[k], {})
to_return[keys_match[k]] = v
# sometimes data extension fields have type integer or
# number, but come back as strings from the API. we need
# to explicitly cast them.
if v is None:
pass
elif 'integer' in field_schema.get('type'):
to_return[keys_match[k]] = int(v) if v else 0
elif 'number' in field_schema.get('type'):
to_return[keys_match[k]] = float(v) if v else 0.0
elif ('boolean' in field_schema.get('type') and
isinstance(to_return[keys_match[k]], str)):
# Extension bools can come through as a number of values, see:
# https://help.salesforce.com/articleView?id=mc_es_data_extension_data_types.htm&type=5
# In practice, looks like they come through as either "True"
# or "False", but for completeness I have included the other
# possible values.
if str(to_return[keys_match[k]]).lower() in [1, "1", "y", "yes", "true"]:
to_return[keys_match[k]] = True
elif str(to_return[keys_match[k]]).lower() in [0, "0", "n", "no", "false"]:
to_return[keys_match[k]] = False
else:
to_return[keys_match[k]] = None
if v and keys_match[k] in self.config.get('sensitive_fields', '').replace(" ", "").split(','):
to_return[keys_match[k]] = hashlib.md5(v.encode('utf-8')).hexdigest()
return to_return
def sync_data(self):
LOGGER.info('...............START SYNC.')
tap_stream_id = self.catalog.get('tap_stream_id')
table = self.catalog.get('stream')
(_, customer_key) = tap_stream_id.split('.', 1)
keys = self.get_catalog_keys()
keys.remove('CategoryID')
replication_key = None
start = get_last_record_value_for_table(
self.state,
table,
self.config.get('start_date'),
self.config.get('offset_start_date', None),
self.is_full_table_mode()
)
if start is None:
start = self.config.get('start_date')
for key in ['ModifiedDate', 'JoinDate', 'CreatedDate']:
if key in keys:
LOGGER.info(f'replication key = {key}')
replication_key = key
parent_result = None
parent_extension = None
parent_result = request(
'DataExtension',
FuelSDK.ET_DataExtension,
self.auth_stub,
search_filter={
'Property': 'CustomerKey',
'SimpleOperator': 'equals',
'Value': customer_key,
},
props=['CustomerKey', 'CategoryID'])
parent_extension = next(parent_result)
parent_category_id = parent_extension.CategoryID
bookmark = self.state.get('bookmarks', {}).get(table, {})
page = 1 if bookmark and bookmark.get('is_completed', False) else (bookmark.get('page') + 1 if bookmark.get('page') else 1)
# restart from previous run if incremental mode and have the bookmark is completed to false
if self.get_replication_method() == 'INCREMENTAL' and not bookmark.get('is_completed', False) and bookmark.get('page'):
LOGGER.info(f'...............Previous run was incompleted: {page}')
start = bookmark.get('previous_start_date', start)
if self.get_replication_method() == 'FULL_TABLE':
#if full table restart from the beginning
page = 1
LOGGER.info(f'...............Start from: {page}')
keys.remove('_CustomObjectKey')
response = self.request_data_extension_via_rest_api(page, customer_key, keys, replication_key, start)
resp = response.json()
self.write_records_from_rest_api(resp.get('items', []), table, replication_key, parent_category_id)
# save state after each upload to unable rerun
self.save_last_page_sync(table, page, start)
# if more results
while resp.get('links', []) and resp.get('links').get('next'):
page += 1
response = self.request_data_extension_via_rest_api(page, customer_key, keys, replication_key, start)
resp = response.json()
self.write_records_from_rest_api(resp.get('items', []), table, replication_key, parent_category_id)
# save state after each upload to unable rerun
self.save_last_page_sync(table, page, start)
self.state['bookmarks'][table]['is_completed'] = True
LOGGER.info('...............END SYNC.')
@backoff.on_exception(backoff.expo, (requests.exceptions.HTTPError), max_tries=5)
def request_data_extension_via_rest_api(self, page, customer_key, keys, replication_key, start_date):
LOGGER.info(f'...............REST request for data extension. parameters: page: {page}, pagesize: 2500, data_extension: {customer_key}')
params = urlencode({
"$page": page,
"$pagesize": 2500,
"$fields": ','.join(keys)
}, safe=',')
if replication_key:
params += f"&$filter={replication_key}%20gt%20'{start_date}'"
LOGGER.info(f"'...............params: {params}")
et_subdomain = self.config.get('tenant_subdomain')
access_token = self.auth_stub.authToken
response = requests.get(f'https://{et_subdomain}.rest.marketingcloudapis.com/data/v1/customobjectdata/key/{customer_key}/rowset?{params}', headers={
'Authorization': f'Bearer {access_token}'
})
resp = response.json()
if response.status_code != 200:
message_error = resp.get('message')
error_code = resp.get('errorcode')
LOGGER.error(f'RequestApiError : {message_error}, errorcode: {error_code}')
if response.status_code == 401:
# if not authorized try to refresh token
self.auth_stub.refresh_token_with_oAuth2(True)
response.raise_for_status()
return response
def write_records_from_rest_api(self, items, table, replication_key, parent_category_id):
catalog_copy = copy.deepcopy(self.catalog)
for row in items:
raw_data = dict()
raw_data.update(row.get('keys', {}))
raw_data.update(row.get('values', {}))
row = self.filter_keys_and_parse(raw_data)
row['_CustomObjectKey'] = None
row['CategoryID'] = parent_category_id
self.write_records_with_transform(row, catalog_copy, table)
self.state = incorporate(self.state,
table,
replication_key,
row.get(replication_key))
LOGGER.info(f'...............Write {len(items)} records')
def save_last_page_sync(self, table, page, start):
new_state = self.state.copy()
if 'bookmarks' not in new_state:
new_state['bookmarks'] = {}
new_state['bookmarks'][table] = {
**new_state['bookmarks'].get(table, {}),
'previous_start_date': start,
'is_completed': False,
'page': page
}
self.state = new_state
save_state(self.state)