-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_sage_entity.py
361 lines (260 loc) · 13.6 KB
/
grab_sage_entity.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
import logging
logger = logging.getLogger('Sage Logger')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('sage.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
from xml.dom.minidom import Document, parseString
from dataclasses import dataclass
from datetime import datetime as dt
from datetime import timezone as tz
import time
import os
import requests
import xml.etree.ElementTree as ET
from azure.storage.filedatalake import DataLakeServiceClient
@dataclass
class SageIntacct:
'''Class representing a Sage Intacct session connection variables'''
control_id: str = str(time.time()).replace('.', '')
company_id: str = os.getenv("SAGE_COMPANY_ID")
user_id: str = os.getenv("SAGE_USER_ID")
user_password: str = os.getenv("SAGE_USER_PASSWORD")
sender_id: str = os.getenv("SAGE_SENDER_ID")
sender_password: str = os.getenv("SAGE_SENDER_PASSWORD")
url: str = "https://api.intacct.com/ia/xml/xmlgw.phtml"
@dataclass
class AzureDataLake:
'''Class representing a filesystem client connection credentials to ADLS Gen2 stored in environment variables'''
storage_account_name: str = os.getenv('AZURE_STORAGE_ACCT_NAME')
storage_account_key: str = os.getenv('AZURE_STORAGE_ACCT_KEY')
filesystem: str = os.getenv('AZURE_STORAGE_FILESYSTEM')
@dataclass
class SageResult:
'''Class holding response data needed to iterate through scans'''
entity: str
result_id: str
this_result_count: int
number_remaining: int
total_count: int
def generate_xml_doc(SageSesh, doc_type: str, session_id: str = None, entity: str = None, columns: str = None, where_query: str = None, rows_per_page: str = None, result_id: str = None):
'''Generates xml for the request object'''
try:
new_xml_doc = Document()
request = new_xml_doc.createElement("request")
new_xml_doc.appendChild(request)
# request.control
control = new_xml_doc.createElement("control")
request.appendChild(control)
senderid = new_xml_doc.createElement('senderid')
control.appendChild(senderid).appendChild(new_xml_doc.createTextNode(SageSesh.sender_id))
senderpassword = new_xml_doc.createElement('password')
control.appendChild(senderpassword).appendChild(new_xml_doc.createTextNode(SageSesh.sender_password))
controlid = new_xml_doc.createElement('controlid')
control.appendChild(controlid).appendChild(new_xml_doc.createTextNode(SageSesh.control_id))
uniqueid = new_xml_doc.createElement('uniqueid')
control.appendChild(uniqueid).appendChild(new_xml_doc.createTextNode("false"))
dtdversion = new_xml_doc.createElement('dtdversion')
control.appendChild(dtdversion).appendChild(new_xml_doc.createTextNode("3.0"))
includewhitespace = new_xml_doc.createElement('includewhitespace')
control.appendChild(includewhitespace).appendChild(new_xml_doc.createTextNode("false"))
# request.operation
operation = new_xml_doc.createElement('operation')
request.appendChild(operation)
# request.operation.authentication
authentication = new_xml_doc.createElement('authentication')
operation.appendChild(authentication)
if doc_type == 'Auth':
login = new_xml_doc.createElement('login')
authentication.appendChild(login)
userid = new_xml_doc.createElement('userid')
login.appendChild(userid).appendChild(new_xml_doc.createTextNode(SageSesh.user_id))
companyid = new_xml_doc.createElement('companyid')
login.appendChild(companyid).appendChild(new_xml_doc.createTextNode(SageSesh.company_id))
password = new_xml_doc.createElement('password')
login.appendChild(password).appendChild(new_xml_doc.createTextNode(SageSesh.user_password))
elif doc_type == 'Entity' or doc_type == 'NextPage':
sessionid = new_xml_doc.createElement('sessionid')
authentication.appendChild(sessionid).appendChild(new_xml_doc.createTextNode(session_id))
# request.operation.content
content = new_xml_doc.createElement('content')
operation.appendChild(content)
function = new_xml_doc.createElement('function')
content.appendChild(function).setAttributeNode(new_xml_doc.createAttribute('controlid'))
function.attributes["controlid"].value = SageSesh.control_id
if doc_type == 'Auth':
getsessionid = new_xml_doc.createElement('getAPISession')
function.appendChild(getsessionid)
elif doc_type == 'Entity':
readbyquery = new_xml_doc.createElement('readByQuery')
function.appendChild(readbyquery)
obj = new_xml_doc.createElement('object')
readbyquery.appendChild(obj).appendChild(new_xml_doc.createTextNode("" + entity + ""))
fields = new_xml_doc.createElement('fields')
readbyquery.appendChild(fields).appendChild(new_xml_doc.createTextNode("" + columns + ""))
query = new_xml_doc.createElement('query')
readbyquery.appendChild(query).appendChild(new_xml_doc.createTextNode("" + where_query + ""))
pagesize = new_xml_doc.createElement('pagesize')
readbyquery.appendChild(pagesize).appendChild(new_xml_doc.createTextNode("" + rows_per_page + ""))
elif doc_type == 'NextPage':
readmore = new_xml_doc.createElement('readMore')
function.appendChild(readmore)
result = new_xml_doc.createElement('resultId')
readmore.appendChild(result).appendChild(new_xml_doc.createTextNode(result_id))
except TypeError:
logger.error(f"There was invalid variables passed when creating the {doc_type} XML document.")
pretty_request = request.toprettyxml()
logger.info(f"The request for this is: \n {pretty_request}")
return pretty_request
def send_request(payload: str, SageSesh):
'''Sends an xml request to the sage intacct api endpoint'''
header = {'Content-type': 'application/xml'}
num_tries = 3
while True:
num_tries -= 1
try:
response = requests.post(SageSesh.url, data=payload, headers=header,timeout=600)
break
except ConnectionError:
if num_tries == 0:
logger.warning("The connection broke during the request. Erroring out.")
return
# response = requests.post(SageSesh.url, data=payload, headers=header)
# response_text = response.text
# parsed_xml = parseString(response_text)
# xml_pretty = parsed_xml.toprettyxml()
# # logger.info(f"The response was: \n {xml_pretty}")
return response
def parse_session_id(response_object):
'''Parses the sessionid from the xml response'''
root = ET.fromstring(response_object.content)
for child in root.iter('sessionid'):
session = (child.text)
for child in root.iter('sessiontimeout'):
timeout = (child.text)
logger.info(f'The sesion id will be {session} which times out at {timeout}')
return session, timeout
def get_new_sesison(SageSesh):
'''Gets a fresh session token for authentication'''
request_doc_string = generate_xml_doc(SageSesh, 'Auth')
response = send_request(request_doc_string, SageSesh)
result = parse_session_id(response)
session_id = result[0]
time_as_string = result[1]
if ":" == time_as_string[-3:-2]:
time_as_string = time_as_string[:-3]+time_as_string[-2:]
timeout = dt.strptime(time_as_string, "%Y-%m-%dT%H:%M:%S%z")
return session_id, timeout
def get_entity(SageSesh, session_id: str, entity: str, query: str):
'''Gets a single entity for a specified amount of time (typically 1 month)'''
request_doc = generate_xml_doc(SageSesh
, 'Entity'
, session_id=session_id
, entity=entity
, columns='*'
, where_query=query
, rows_per_page='1000')
response = send_request(request_doc, SageSesh)
return response.text
def get_next_page(SageSesh, session_id: str, result_id: str):
'''Gets the next page using the result_id'''
request_doc = generate_xml_doc(SageSesh
, 'NextPage'
, session_id=session_id
, result_id=result_id)
response = send_request(request_doc, SageSesh)
return response.text
def initialize_datalake_client(storage_account_name, storage_account_key, file_system='landing-zone'):
"""Creates a service client for the Azure data lake"""
service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
"https", storage_account_name), credential=storage_account_key)
if service_client:
logger.info(f"Successfully connected to {storage_account_name} Azure Data Lake")
# create the filesystem
filesystem_client = service_client.get_file_system_client(file_system=file_system)
return filesystem_client
else:
logger.warning("Could not connect to Azure Data Lake")
return False
def upload_to_datalake(filesystem_client, file_name, file_content):
"""Uploads a file to data lake in partitions"""
logger.info("Creating a file named '{}'.".format(file_name))
file_client = filesystem_client.get_file_client(file_name)
file_client.create_file()
# append data to the file
# the data remain uncommitted until flush is performed
logger.info("Uploading data to '{}'.".format(file_name))
file_client.upload_data(data=file_content, overwrite=True)
# data is only committed when flush is called
# file_client.flush_data(len(file_content))
# read the data back
# [START read_file]
download = file_client.download_file()
downloaded_bytes = download.readall()
# [END read_file]
# verify the downloaded content
if len(file_content) == len(downloaded_bytes):
logger.info(f"{file_name} was saved successfully.")
return True
else:
logger.error(f"Something went wrong saving {file_name}.")
def save_entity(AzureSesh, file_name, content_to_save):
'''Saves the entity XML response to the Azure Data Lake'''
parsed_xml = parseString(content_to_save)
xml_pretty = parsed_xml.toprettyxml()
datalake_client = initialize_datalake_client(AzureSesh.storage_account_name, AzureSesh.storage_account_key, AzureSesh.filesystem)
upload = upload_to_datalake(datalake_client, file_name=file_name, file_content=xml_pretty)
if upload:
logger.info(f"Successfully saved {file_name}.")
def check_for_next_entity(response_to_check):
'''Checks the response for next entity metadata'''
root = ET.fromstring(response_to_check)
for a in root.iter('data'):
result = SageResult(entity=a.get("listtype"), result_id=a.get("resultId"), this_result_count=int(a.get("count")), number_remaining=int(a.get("numremaining")), total_count=int(a.get("totalcount")))
logger.info(f"There are {result.number_remaining} records remaining for {result.entity}. Continuing...")
return result
def main(entity_name: str, query: str, file_name_prefix: str = 'adhoc'):
# Generate the session dataclass from environment variables
SageSesh = SageIntacct()
AzureSesh = AzureDataLake()
logger.info(f"The {SageSesh.sender_id} sender_id is being used to save to the {AzureSesh.storage_account_name} storage account.")
if SageSesh.sender_id is None:
logger.warning("Sage session is missing environmental variables.")
long_run_start_time = dt.now()
logger.info(f'Starting function at {long_run_start_time} for {entity_name}.')
sesh = get_new_sesison(SageSesh)
session_id = sesh[0]
timeout = sesh[1]
entity = get_entity(SageSesh, session_id, entity_name, query)
save_entity(AzureSesh, f'Sage_Intacct/data_download/{entity_name}/{file_name_prefix}_{entity_name}_0.xml', entity)
next_entity = check_for_next_entity(entity)
counter = 1
while next_entity.number_remaining != 0:
current_time = dt.now().replace(tzinfo=tz.utc)
if timeout <= current_time:
logging.info('Previous token expiring. Getting a fresh session token.')
sesh = get_new_sesison(SageSesh)
session_id = sesh[0]
timeout = sesh[1]
next_entity_page = get_next_page(SageSesh, session_id, next_entity.result_id)
save_entity(AzureSesh, f'Sage_Intacct/data_download/{entity_name}/{file_name_prefix}_{entity_name}_{counter}.xml', next_entity_page)
next_entity = check_for_next_entity(next_entity_page)
counter += 1
logger.info(f"Just finished page {counter-1} for {entity_name}.")
logger.info(f"There are {next_entity.number_remaining} {entity_name} records remaining out of {next_entity.total_count}.")
long_run_end_time = dt.now() - long_run_start_time
logger.info(f'Finished processing {entity_name}. It took {counter-1} files and it took {long_run_end_time}')
return next_entity.total_count, next_entity.number_remaining, counter
if __name__ == '__main__':
# main()
main('CUSTOMER', 'WHENMODIFIED >= 06/01/2022 AND WHENMODIFIED <= 07/10/2022')