-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path__init__.py
More file actions
390 lines (336 loc) · 16.8 KB
/
__init__.py
File metadata and controls
390 lines (336 loc) · 16.8 KB
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
import re
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
from tap_salesforce.salesforce.exceptions import (
TapSalesforceException,
TapSalesforceQuotaExceededException)
from tap_salesforce.salesforce.credentials import SalesforceAuth
LOGGER = singer.get_logger()
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',
'base64'
])
NUMBER_TYPES = set([
'double',
'currency',
'percent'
])
DATE_TYPES = set([
'datetime',
'date'
])
BINARY_TYPES = set([
'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(['AssetTokenEvent',
'AttachedContentNote',
'EventWhoRelation',
'QuoteTemplateRichTextData',
'TaskWhoRelation',
'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',
'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):
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
def __init__(self,
credentials=None,
token=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,
pk_chunking={}):
self.api_type = api_type.upper() if api_type else None
self.session = requests.Session()
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.data_url = "{}/services/data/v53.0/{}"
self.pk_chunking = pk_chunking
self.auth = SalesforceAuth.from_credentials(credentials, is_sandbox=self.is_sandbox)
# validate start_date
singer_utils.strptime(default_start_date)
# 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)
def login(self):
self.auth.login()
@property
def instance_url(self):
return self.auth.instance_url
# pylint: disable=too-many-arguments
@backoff.on_exception(backoff.expo,
requests.exceptions.ConnectionError,
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):
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)
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)
else:
raise TapSalesforceException("Unsupported HTTP method")
resp.raise_for_status()
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 describe(self, sobject=None):
"""Describes all objects or a specific object"""
headers = self.auth.rest_headers
instance_url = self.auth.instance_url
if sobject is None:
endpoint = "sobjects"
endpoint_tag = "sobjects"
url = self.data_url.format(instance_url, endpoint)
else:
endpoint = "sobjects/{}/describe".format(sobject)
endpoint_tag = sobject
url = self.data_url.format(instance_url, 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()
# pylint: disable=no-self-use
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):
catalog_metadata = metadata.to_map(catalog_entry['metadata'])
replication_key = catalog_metadata.get((), {}).get('replication-key')
return (singer.get_bookmark(state,
catalog_entry['tap_stream_id'],
replication_key) or self.default_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))