-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
128 lines (99 loc) · 3.87 KB
/
Copy pathlambda_function.py
File metadata and controls
128 lines (99 loc) · 3.87 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
import json
import os
import urllib
import boto3
import datetime as dt
import filereader as filereader
#import intAccessAPI as AccessAPI
#import intAccountReceivableAPI as AccountReceivableAPI
import intBookingAPI as BookingAPI
#import intCenterAPI as CenterAPI
#import intChildCareAPI as ChildCareAPI
#import intClipcardAPI as ClipcardAPI
#import intCompanyAPI as CompanyAPI
#import intCourseAPI as CourseAPI
#import intCrmAPI as CrmAPI
#import intEmployeeAPI as EmployeeAPI
#import intExtractAPI as ExtractAPI
#import intGiftCardAPI as GiftCardAPI
import intPersonAPI as PersonAPI
#import intProductAPI as ProductAPI
#import intQuestionnaireAPI as QuestionnaireAPI
#import intResourceAPI as ResourceAPI
#import intsocialAPI as socialAPI
#import intStaffBookingAPI as StaffBookingAPI
#import intSubscriptionAPI as SubscriptionAPI
import interfaceTransform as intTrans
import schedulerNotify as notifySchedule
import tempfile as tmpfile
import csv
import instanceConfiguration as insConfig
print('Loading function')
# TODO
# Read type of file that is returned, only process accepted files (JSON, XLSX, CSV)
# Dynamic file reading of contents. Bookings expects bookings etc
# Multiprocessing: Should we take 1 file into a stage, split it up,
# then drop each into the main directory to be processed? Or code multiprocessing.
# Wrap up coding other accepted API classes
# Documentation
s3 = boto3.client('s3')
datestamp = dt.datetime.now().strftime("%Y/%m/%d")
timestamp = dt.datetime.now().strftime("%s")
filename_json = "/tmp/file_{ts}.json".format(ts=timestamp)
filename_csv = "/tmp/file_{ts}.csv".format(ts=timestamp)
def lambda_handler(event, context):
#1 - Get the bucket name
bucket = event['Records'][0]['s3']['bucket']['name']
#2 - Get the file/key name
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
#3 - Parse the file name and store as variables
fileName = key.split("_")
processID = str(fileName[0]).replace('PROD/','')
processID = int(processID)
executionInterface = int(fileName[1])
fexecutionMethod= int(fileName[2])
extractFileName = fileName[3]
executionDate = fileName[4]
file = key.split(".")
filetype = file[1]
#4 - Check
print('File extension: ',filetype.upper())
localFilename = '/tmp/file.csv'
s3.download_file(bucket, key, localFilename)
#5 - Conditional check for files we accept
if filetype == 'json' or filetype =='csv':
print(filetype.upper(), 'accepted. We will proceed..')
try:
#6 - Map the interface & Mehtod ID from file name to get right class
interfaceArray = []
interfaceArray.extend(intTrans.transformInterface(executionInterface,fexecutionMethod))
intValue = interfaceArray[0]
intMethod = interfaceArray[1]
#ex Print out for logging purposes
print('Interface & Method: '+intValue+';'+intMethod)
#7 - Set value of method to be passed:
executionMethod = (intMethod)
print('Will use '+ str(intValue) + ' as interface and the method ' + str(executionMethod)+ ' as determined by filename..')
#8 - Build the endpoint URL
req = insConfig.reqURL(intValue)
print('API Endpoint is: ',req)
#9 - Ship the file contents to the method which will handle the transaction
apiInterface = eval(intValue)
#10 - Fetch the file from S3
response = s3.get_object(Bucket=bucket, Key=key)
#11 - Parse file contents and store as dictionary
#parsedFile = filereader.readFile(response,filetype)
print ()
with open(localFilename) as f:
next(f)
for line in f:
parsedFile = filereader.processLine(line)
apiInterface.processList(parsedFile,executionMethod)
#12 - Failure or Success response
return notifySchedule.executionCompletionNotifier(processID,localFilename)
except Exception as e:
print(e)
raise e
else:
#13 - We don't accept the file supplied so we reject
print('We do not accpt ',filetype,' at this time :(')