in lambdas/api_predict_station_status/index.py [0:0]
def lambda_handler(event, context):
status_code = 400
try:
#print("Received event: " + json.dumps(event, indent=2))
operation = event['httpMethod']
if operation == 'POST':
input = json.loads(event['body'])
day_of_week = datetime.strptime('{}{}{}'.format(input['year'], input['month'], input['day']), '%Y%m%d').date().isoweekday() + 1
test_data = [','.join(map(str, [station_id, input['year'], input['month'], input['day'], input['hour'], day_of_week])) for station_id in input['station_ids']]
#print('Test data: {}'.format(json.dumps(test_data)))
result = batch_predict(test_data, 100, MODEL_ENDPOINT_NAME, 'text/csv')
output = json.dumps(dict(zip(input['station_ids'], result)))
status_code = 200
else:
output = 'Unsupported method: {}'.format(operation)
except Exception as e:
print('ERROR: ', e)
output = '{}'.format(e)
# Construct API response
response = {
'statusCode': status_code,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': True,
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent',
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS,HEAD,PATCH',
'Content-Type': 'application/json'
},
'body': output
}
print('[INFO] Query response: {}'.format(json.dumps(response)))
return response