Created
July 9, 2020 14:08
-
-
Save louwersj/7417c7294c1e409b26fb850405780d2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import json | |
import re | |
import shlex | |
#TODO (0) at current we fake the input with a local file, this needs to be a true http request based trigger in the future | |
#TODO (1) need to include error handling on all levels. (don on occasion already) | |
#TODO (2) implement decryption function | |
#TODO (3) ensure we will publish a error to error stream when we catch an exception. (could you publishEvent for this) | |
def publishEvent(event) : | |
''' | |
Function publishEvent will publish a new event to an Oracle Cloud Stream. Part of the event definition needs to be | |
the stream to which it needs to be published. | |
:param event: | |
:return: | |
''' | |
print ("hallo") | |
def decodePayload(requestData) : | |
''' | |
Function decodePayload will take the full request data and extract both the payload_message as well as the | |
payload_encoding. Based upon the value of payload_encoding the function will decode the payload_message | |
:param requestData: | |
:return: | |
- payloadMessage (decoded payload) | |
''' | |
payloadMessage = requestData['payload_data']['payload_message'] | |
payloadEncoding = requestData['meta_data']['payload_encoding'] | |
if payloadEncoding == "base64" : | |
payloadMessage = base64.b64decode(payloadMessage) | |
return payloadMessage | |
elif payloadEncoding == "base32" : | |
payloadMessage = base64.b32decode(payloadMessage) | |
return payloadMessage | |
else: | |
print ("unknown value for payload_encoding defined in event JSON") | |
def decryptPayload(requestData) : | |
''' | |
Function decryptPayload is used to decrypt the payload. We provide the entire requestPayload to this function. Based | |
upon the value of payload_encryption we will select the correct encryption / decryption algorithm. The function will | |
return the decrypted value of payload_message | |
:param requestData: | |
:return: | |
''' | |
def oiaBuildPayload(payloadMessage) : | |
''' | |
Function oiaBuildPayload is used to build the JSON case file used as a payload in the request to Oracle Intelligent | |
Advisor. For this it will take the values defined in the original payload_message from the event payload which did | |
trigger the invocation of the invent. This is, after is has been decoded (if required) and after it is decrypted (if | |
required). The oiaBuildPayload function will receive a dict containing all the k/v pairs defined in the original | |
payload message and we can populate the globalInputAttribute(x) variables based upon the key used in the original | |
payload_message. | |
:param payloadMessage: | |
:return: oiaPayLoad | |
''' | |
globalInputAttribute1 = payloadMessage.get("a", "NO_VALUE_ERROR") | |
globalInputAttribute2 = payloadMessage.get("b", "NO_VALUE_ERROR") | |
globalInputAttribute3 = payloadMessage.get("c", "NO_VALUE_ERROR") | |
globalInputAttribute4 = payloadMessage.get("d", "NO_VALUE_ERROR") | |
globalInputAttribute5 = payloadMessage.get("e", "NO_VALUE_ERROR") | |
globalInputAttribute6 = payloadMessage.get("f", "NO_VALUE_ERROR") | |
oiaPayLoad = { | |
'outcomes': [ | |
'global_input_attribute_1', | |
'global_output_attribute_6', | |
'global_output_attribute_7' | |
], | |
'cases': [{ | |
'@id': 1, | |
'global_input_attribute_1': globalInputAttribute1, | |
'global_input_attribute_2': globalInputAttribute2, | |
'global_input_attribute_3': globalInputAttribute3, | |
'global_input_attribute_4': globalInputAttribute4, | |
'global_input_attribute_5': globalInputAttribute5, | |
'global_input_attribute_6': globalInputAttribute6, | |
}] | |
} | |
return oiaPayLoad | |
def oiaPostPayload(oiaPayLoad) : | |
''' | |
:param payloadMessage: | |
:return: | |
''' | |
def mainLogic(requestData) : | |
# check if the payload_message is encoded in case the payload is encoded we provide the entire requestPayload to the | |
# decode function and we expect the decoded value of "payload_message" in return | |
if requestData['meta_data']['payload_encoded'] == True: | |
payloadMessage = decodePayload(requestData) | |
# check if the payload_message is encrypted in case the payload is encrypted we provide the entire requestPayload to | |
# the decrypt function and we expect the decrypted value of "payload_message" in return | |
# if requestData['meta_data']['payload_encrypted'] == True: | |
# convert to UTF-8 | |
try: | |
payloadMessage = payloadMessage.decode("utf-8") | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
raise | |
# remove trailing newline | |
try: | |
payloadMessage = payloadMessage.rstrip("\n") | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
raise | |
# split the string into a list | |
try: | |
payloadMessage = payloadMessage.split('|') | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
raise | |
# split the list into a dict | |
try: | |
payloadMessage = dict(s.split('=') for s in payloadMessage) | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
raise | |
# Call oiaBuildPayload to build the downstream payload | |
oiaBuildPayload(payloadMessage) | |
# post the payload case file to OIA as build by oiaBuildPayload | |
# the loading of the .json file is only for example purposes. Under normal situations this will be the request payload | |
# that is send to the event and we do not read it from disk | |
#TODO ensure a propper function like input start | |
with open('example_0.json') as requestPayload: | |
requestData = json.load(requestPayload) | |
mainLogic(requestData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment