-
Notifications
You must be signed in to change notification settings - Fork 70
/
__init__.py
479 lines (414 loc) · 21.6 KB
/
__init__.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
import datetime
import re
import threading
import time
import backoff
import requests
from requests.exceptions import RequestException
import singer
import singer.utils as singer_utils
from singer import metadata, metrics
from tap_salesforce.salesforce.bulk import Bulk
from tap_salesforce.salesforce.rest import Rest, API_VERSION
from tap_salesforce.salesforce.exceptions import (
TapSalesforceException,
TapSalesforceQuotaExceededException)
LOGGER = singer.get_logger()
# The minimum expiration setting for SF Refresh Tokens is 15 minutes
REFRESH_TOKEN_EXPIRATION_PERIOD = 900
BULK_API_TYPE = "BULK"
REST_API_TYPE = "REST"
STRING_TYPES = set([
'id',
'string',
'picklist',
'textarea',
'phone',
'url',
'reference',
'multipicklist',
'combobox',
'encryptedstring',
'email',
'complexvalue', # TODO: Unverified
'masterrecord',
'datacategorygroupreference'
])
NUMBER_TYPES = set([
'double',
'currency',
'percent'
])
DATE_TYPES = set([
'datetime',
'date'
])
BINARY_TYPES = set([
'base64',
'byte'
])
LOOSE_TYPES = set([
'anyType',
# A calculated field's type can be any of the supported
# formula data types (see https://developer.salesforce.com/docs/#i1435527)
'calculated'
])
# The following objects are not supported by the bulk API.
UNSUPPORTED_BULK_API_SALESFORCE_OBJECTS = set(['FieldSecurityClassification',
'WorkStepStatus',
'ShiftStatus',
'WorkOrderStatus',
'WorkOrderLineItemStatus',
'ServiceAppointmentStatus',
'SolutionStatus',
'ContractStatus',
'RecentlyViewed',
'DeclinedEventRelation',
'AcceptedEventRelation',
'TaskStatus',
'PartnerRole',
'TaskPriority',
'CaseStatus',
'UndecidedEventRelation',
'OrderStatus'])
# The following objects have certain WHERE clause restrictions so we exclude them.
QUERY_RESTRICTED_SALESFORCE_OBJECTS = set(['Announcement',
'ContentDocumentLink',
'CollaborationGroupRecord',
'Vote',
'IdeaComment',
'FieldDefinition',
'PlatformAction',
'UserEntityAccess',
'RelationshipInfo',
'ContentFolderMember',
'ContentFolderItem',
'SearchLayout',
'SiteDetail',
'EntityParticle',
'OwnerChangeOptionInfo',
'DataStatistics',
'UserFieldAccess',
'PicklistValueInfo',
'RelationshipDomain',
'FlexQueueItem',
'NetworkUserHistoryRecent',
'FieldHistoryArchive',
'RecordActionHistory',
'FlowVersionView',
'FlowVariableView',
'AppTabMember',
'ColorDefinition',
'DatacloudDandBCompany', # Not filterable without a criteria.
'DatacloudAddress', # Transient queries are not implemented
'FlowTestView', # A filter on a reified column is required [FlowDefinitionViewId,DurableId]
'RelatedListColumnDefinition', # A filter on a reified column is required [RelatedListDefinitionId,DurableId],
'RelatedListDefinition', # A filter on a reified column is required [ParentEntityDefinitionId,DurableId],
'ApexTypeImplementor', # A filter on a reified column is required [InterfaceName,DurableId]
'IconDefinition',])
# The following objects are not supported by the query method being used.
QUERY_INCOMPATIBLE_SALESFORCE_OBJECTS = set(['DataType',
'ListViewChartInstance',
'FeedLike',
'OutgoingEmail',
'OutgoingEmailRelation',
'FeedSignal',
'ActivityHistory',
'EmailStatus',
'UserRecordAccess',
'Name',
'AggregateResult',
'OpenActivity',
'ProcessInstanceHistory',
'OwnedContentDocument',
'FolderedContentDocument',
'FeedTrackedChange',
'CombinedAttachment',
'AttachedContentDocument',
'ContentBody',
'NoteAndAttachment',
'LookedUpFromActivity',
'AttachedContentNote',
'QuoteTemplateRichTextData'])
def log_backoff_attempt(details):
LOGGER.info("ConnectionError detected, triggering backoff: %d try", details.get("tries"))
def field_to_property_schema(field, mdata): # pylint:disable=too-many-branches
property_schema = {}
field_name = field['name']
sf_type = field['type']
if sf_type in STRING_TYPES:
property_schema['type'] = "string"
elif sf_type in DATE_TYPES:
date_type = {"type": "string", "format": "date-time"}
string_type = {"type": ["string", "null"]}
property_schema["anyOf"] = [date_type, string_type]
elif sf_type == "boolean":
property_schema['type'] = "boolean"
elif sf_type in NUMBER_TYPES:
property_schema['type'] = "number"
elif sf_type == "address":
property_schema['type'] = "object"
property_schema['properties'] = {
"street": {"type": ["null", "string"]},
"state": {"type": ["null", "string"]},
"postalCode": {"type": ["null", "string"]},
"city": {"type": ["null", "string"]},
"country": {"type": ["null", "string"]},
"longitude": {"type": ["null", "number"]},
"latitude": {"type": ["null", "number"]},
"geocodeAccuracy": {"type": ["null", "string"]}
}
elif sf_type in ("int", "long"):
property_schema['type'] = "integer"
elif sf_type == "time":
property_schema['type'] = "string"
elif sf_type in LOOSE_TYPES:
return property_schema, mdata # No type = all types
elif sf_type in BINARY_TYPES:
mdata = metadata.write(mdata, ('properties', field_name), "inclusion", "unsupported")
mdata = metadata.write(mdata, ('properties', field_name),
"unsupported-description", "binary data")
return property_schema, mdata
elif sf_type == 'location':
# geo coordinates are numbers or objects divided into two fields for lat/long
property_schema['type'] = ["number", "object", "null"]
property_schema['properties'] = {
"longitude": {"type": ["null", "number"]},
"latitude": {"type": ["null", "number"]}
}
elif sf_type == 'json':
property_schema['type'] = "string"
else:
raise TapSalesforceException("Found unsupported type: {}".format(sf_type))
# The nillable field cannot be trusted
if field_name != 'Id' and sf_type != 'location' and sf_type not in DATE_TYPES:
property_schema['type'] = ["null", property_schema['type']]
return property_schema, mdata
class Salesforce():
# pylint: disable=too-many-instance-attributes,too-many-arguments,too-many-positional-arguments
def __init__(self,
refresh_token=None,
token=None,
sf_client_id=None,
sf_client_secret=None,
quota_percent_per_run=None,
quota_percent_total=None,
is_sandbox=None,
select_fields_by_default=None,
default_start_date=None,
api_type=None,
lookback_window=None):
self.api_type = api_type.upper() if api_type else None
self.refresh_token = refresh_token
self.token = token
self.sf_client_id = sf_client_id
self.sf_client_secret = sf_client_secret
self.session = requests.Session()
self.access_token = None
self.instance_url = None
if isinstance(quota_percent_per_run, str) and quota_percent_per_run.strip() == '':
quota_percent_per_run = None
if isinstance(quota_percent_total, str) and quota_percent_total.strip() == '':
quota_percent_total = None
self.quota_percent_per_run = float(
quota_percent_per_run) if quota_percent_per_run is not None else 25
self.quota_percent_total = float(
quota_percent_total) if quota_percent_total is not None else 80
self.is_sandbox = is_sandbox is True or (isinstance(is_sandbox, str) and is_sandbox.lower() == 'true')
self.select_fields_by_default = select_fields_by_default is True or (isinstance(select_fields_by_default, str) and select_fields_by_default.lower() == 'true')
self.default_start_date = default_start_date
self.rest_requests_attempted = 0
self.jobs_completed = 0
self.login_timer = None
self.data_url = "{}/services/data/v{}.0/{}"
self.pk_chunking = False
self.lookback_window = lookback_window
# validate start_date
singer_utils.strptime_to_utc(default_start_date)
def _get_standard_headers(self):
return {"Authorization": "Bearer {}".format(self.access_token)}
# pylint: disable=anomalous-backslash-in-string,line-too-long
def check_rest_quota_usage(self, headers):
match = re.search('^api-usage=(\d+)/(\d+)$', headers.get('Sforce-Limit-Info'))
if match is None:
return
remaining, allotted = map(int, match.groups())
LOGGER.info("Used %s of %s daily REST API quota", remaining, allotted)
percent_used_from_total = (remaining / allotted) * 100
max_requests_for_run = int((self.quota_percent_per_run * allotted) / 100)
if percent_used_from_total > self.quota_percent_total:
total_message = ("Salesforce has reported {}/{} ({:3.2f}%) total REST quota " +
"used across all Salesforce Applications. Terminating " +
"replication to not continue past configured percentage " +
"of {}% total quota.").format(remaining,
allotted,
percent_used_from_total,
self.quota_percent_total)
raise TapSalesforceQuotaExceededException(total_message)
elif self.rest_requests_attempted > max_requests_for_run:
partial_message = ("This replication job has made {} REST requests ({:3.2f}% of " +
"total quota). Terminating replication due to allotted " +
"quota of {}% per replication.").format(self.rest_requests_attempted,
(self.rest_requests_attempted / allotted) * 100,
self.quota_percent_per_run)
raise TapSalesforceQuotaExceededException(partial_message)
# pylint: disable=too-many-arguments,too-many-positional-arguments
@backoff.on_exception(backoff.expo,
(requests.exceptions.ConnectionError, requests.exceptions.Timeout),
max_tries=10,
factor=2,
on_backoff=log_backoff_attempt)
def _make_request(self, http_method, url, headers=None, body=None, stream=False, params=None):
request_timeout = 5 * 60 # 5 minute request timeout
try:
if http_method == "GET":
LOGGER.info("Making %s request to %s with params: %s", http_method, url, params)
resp = self.session.get(url,
headers=headers,
stream=stream,
params=params,
timeout=request_timeout,)
elif http_method == "POST":
LOGGER.info("Making %s request to %s with body %s", http_method, url, body)
resp = self.session.post(url,
headers=headers,
data=body,
timeout=request_timeout,)
else:
raise TapSalesforceException("Unsupported HTTP method")
except requests.exceptions.ConnectionError as connection_err:
LOGGER.error('Took longer than %s seconds to connect to the server', request_timeout)
raise connection_err
except requests.exceptions.Timeout as timeout_err:
LOGGER.error('Took longer than %s seconds to hear from the server', request_timeout)
raise timeout_err
try:
resp.raise_for_status()
except RequestException as ex:
raise ex
if resp.headers.get('Sforce-Limit-Info') is not None:
self.rest_requests_attempted += 1
self.check_rest_quota_usage(resp.headers)
return resp
def login(self):
if self.is_sandbox:
login_url = 'https://test.salesforce.com/services/oauth2/token'
else:
login_url = 'https://login.salesforce.com/services/oauth2/token'
login_body = {'grant_type': 'refresh_token', 'client_id': self.sf_client_id,
'client_secret': self.sf_client_secret, 'refresh_token': self.refresh_token}
LOGGER.info("Attempting login via OAuth2")
resp = None
try:
resp = self._make_request("POST", login_url, body=login_body, headers={"Content-Type": "application/x-www-form-urlencoded"})
LOGGER.info("OAuth2 login successful")
auth = resp.json()
self.access_token = auth['access_token']
self.instance_url = auth['instance_url']
except Exception as e:
error_message = str(e)
if resp is None and hasattr(e, 'response') and e.response is not None: #pylint:disable=no-member
resp = e.response #pylint:disable=no-member
# NB: requests.models.Response is always falsy here. It is false if status code >= 400
if isinstance(resp, requests.models.Response):
error_message = error_message + ", Response from Salesforce: {}".format(resp.text)
raise Exception(error_message) from e
finally:
LOGGER.info("Starting new login timer")
self.login_timer = threading.Timer(REFRESH_TOKEN_EXPIRATION_PERIOD, self.login)
self.login_timer.daemon = True # The timer should be a daemon thread so the process exits.
self.login_timer.start()
def describe(self, sobject=None):
"""Describes all objects or a specific object"""
headers = self._get_standard_headers()
if sobject is None:
endpoint = "sobjects"
endpoint_tag = "sobjects"
url = self.data_url.format(self.instance_url, API_VERSION, endpoint)
else:
endpoint = "sobjects/{}/describe".format(sobject)
endpoint_tag = sobject
url = self.data_url.format(self.instance_url, API_VERSION, endpoint)
with metrics.http_request_timer("describe") as timer:
timer.tags['endpoint'] = endpoint_tag
resp = self._make_request('GET', url, headers=headers)
return resp.json()
def _get_selected_properties(self, catalog_entry):
mdata = metadata.to_map(catalog_entry['metadata'])
properties = catalog_entry['schema'].get('properties', {})
return [k for k in properties.keys()
if singer.should_sync_field(metadata.get(mdata, ('properties', k), 'inclusion'),
metadata.get(mdata, ('properties', k), 'selected'),
self.select_fields_by_default)]
def get_start_date(self, state, catalog_entry):
"""
return start date if state is not provided
else return bookmark from the state by subtracting lookback if provided
"""
catalog_metadata = metadata.to_map(catalog_entry['metadata'])
replication_key = catalog_metadata.get((), {}).get('replication-key')
# get bookmark value from the state
bookmark_value = singer.get_bookmark(state, catalog_entry['tap_stream_id'], replication_key)
sync_start_date = bookmark_value or self.default_start_date
# if the state contains a bookmark, subtract the lookback window from the bookmark
if bookmark_value and self.lookback_window:
sync_start_date = singer_utils.strftime(singer_utils.strptime_with_tz(sync_start_date) - datetime.timedelta(seconds=self.lookback_window))
return sync_start_date
def _build_query_string(self, catalog_entry, start_date, end_date=None, order_by_clause=True):
selected_properties = self._get_selected_properties(catalog_entry)
query = "SELECT {} FROM {}".format(",".join(selected_properties), catalog_entry['stream'])
catalog_metadata = metadata.to_map(catalog_entry['metadata'])
replication_key = catalog_metadata.get((), {}).get('replication-key')
if replication_key:
where_clause = " WHERE {} >= {} ".format(
replication_key,
start_date)
if end_date:
end_date_clause = " AND {} < {}".format(replication_key, end_date)
else:
end_date_clause = ""
order_by = " ORDER BY {} ASC".format(replication_key)
if order_by_clause:
return query + where_clause + end_date_clause + order_by
return query + where_clause + end_date_clause
else:
return query
def query(self, catalog_entry, state):
if self.api_type == BULK_API_TYPE:
bulk = Bulk(self)
return bulk.query(catalog_entry, state)
elif self.api_type == REST_API_TYPE:
rest = Rest(self)
return rest.query(catalog_entry, state)
else:
raise TapSalesforceException(
"api_type should be REST or BULK was: {}".format(
self.api_type))
def get_blacklisted_objects(self):
if self.api_type == BULK_API_TYPE:
return UNSUPPORTED_BULK_API_SALESFORCE_OBJECTS.union(
QUERY_RESTRICTED_SALESFORCE_OBJECTS).union(QUERY_INCOMPATIBLE_SALESFORCE_OBJECTS)
elif self.api_type == REST_API_TYPE:
return QUERY_RESTRICTED_SALESFORCE_OBJECTS.union(QUERY_INCOMPATIBLE_SALESFORCE_OBJECTS)
else:
raise TapSalesforceException(
"api_type should be REST or BULK was: {}".format(
self.api_type))
# pylint: disable=line-too-long
def get_blacklisted_fields(self):
if self.api_type == BULK_API_TYPE:
return {('EntityDefinition', 'RecordTypesSupported'): "this field is unsupported by the Bulk API."}
elif self.api_type == REST_API_TYPE:
return {}
else:
raise TapSalesforceException(
"api_type should be REST or BULK was: {}".format(
self.api_type))
def get_window_end_date(self, start_date, end_date):
# to update end_date, substract 'half_day_range' (i.e. half of the days between start_date and end_date)
# when the 'half_day_range' is an odd number, we will round down to the nearest integer because of the '//'
half_day_range = (end_date - start_date) // 2
if half_day_range.days == 0:
raise TapSalesforceException(
"Attempting to query by 0 day range, this would cause infinite looping.")
return end_date - half_day_range