Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import logging
- from dynCommons import DynCommons
- __author__ = "BBVA-NEXT"
- __version__ = "1.0.0"
- logger = logging.getLogger()
- def get_key_even_if_not_exist(key, dictionary, substitute_element=None):
- """
- Gets a value from a key even if doesn't exist in the dictionary.
- :param key: String that contains the field to get.
- :param dictionary: Dictionary object
- :param substitute_element: Any type of variable to substitute the field if doesn't exist
- :return:
- Returns the value or the substitute element.
- """
- try:
- logger.info('LAMBDA HANDLER|GET KEY EVEN IF NOT EXIST|INIT')
- result = (dictionary[key] if dictionary[key] else substitute_element) if dictionary.get(
- key) else substitute_element
- logger.info('LAMBDA HANDLER|GET KEY EVEN IF NOT EXIST|FINISH|OK')
- return result
- except Exception as e:
- logger.error('|'.join(['LAMBDA HANDLER|GET KEY EVEN IF NOT EXIST|FINISH|KO', str(e)]))
- raise Exception(str(e))
- def refactor_to_dynamo_object(item):
- """
- Changes the format of the dictionary to another dictionary to save it in DynamoDB.
- :param item: JSON object with the next format:
- {
- 'ENDPOINT_FIELD': environment variable that contains a string with the end point
- 'AUTH_FIELD': environment variable that contains a string with the auth
- 'PDH_FIELD': environment variable that contains a string with the end p256dh
- 'EMAIL_FIELD': environment variable that contains a string with the email
- }
- :return:
- Returns a new JSON with a DynamoDB format.
- """
- try:
- dynamo_object = {}
- dynamo_object[os.environ['ENDPOINT_FIELD']] = {'S': item[os.environ['ENDPOINT_FIELD']]}
- dynamo_object[os.environ['AUTH_FIELD']] = {'S': item[os.environ['AUTH_FIELD']]}
- dynamo_object[os.environ['PDH_FIELD']] = {'S': item[os.environ['PDH_FIELD']]}
- dynamo_object[os.environ['EMAIL_FIELD']] = {
- 'S': get_key_even_if_not_exist(key=os.environ['EMAIL_FIELD'], dictionary=item, substitute_element=" ")}
- return dynamo_object
- except Exception as e:
- logger.error('|'.join(['LAMBDA HANDLER|REFACTOR TO DYNAMO OBJECT|FINISH|KO', str(e)]))
- return None
- def lambda_handler(event, context):
- """
- Lambda to save the FCM credentials in dynamoDB.
- :params event :
- :return: boolean goal.
- """
- try:
- logger.info('LAMBDA HANDLER|SAVE CREDENTIALS FCM|INIT')
- dyncommons_object = DynCommons()
- dyncommons_object.create_resource()
- result = dyncommons_object.put_item(os.environ['CREDENTIALS_TABLE'], refactor_to_dynamo_object(event))
- logger.info('LAMBDA HANDLER|SAVE CREDENTIALS FCM|FIN')
- return result
- except Exception as e:
- logger.error('|'.join(['LAMBDA HANDLER|SAVE CREDENTIALS FCM|FINISH|KO', str(e)]))
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement