Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import os
  2. import logging
  3. from dynCommons import DynCommons
  4.  
  5. __author__ = "BBVA-NEXT"
  6. __version__ = "1.0.0"
  7.  
  8. logger = logging.getLogger()
  9.  
  10.  
  11. def get_key_even_if_not_exist(key, dictionary, substitute_element=None):
  12. """
  13. Gets a value from a key even if doesn't exist in the dictionary.
  14. :param key: String that contains the field to get.
  15. :param dictionary: Dictionary object
  16. :param substitute_element: Any type of variable to substitute the field if doesn't exist
  17. :return:
  18. Returns the value or the substitute element.
  19. """
  20. try:
  21. logger.info('LAMBDA HANDLER|GET KEY EVEN IF NOT EXIST|INIT')
  22. result = (dictionary[key] if dictionary[key] else substitute_element) if dictionary.get(
  23. key) else substitute_element
  24. logger.info('LAMBDA HANDLER|GET KEY EVEN IF NOT EXIST|FINISH|OK')
  25.  
  26. return result
  27. except Exception as e:
  28.  
  29. logger.error('|'.join(['LAMBDA HANDLER|GET KEY EVEN IF NOT EXIST|FINISH|KO', str(e)]))
  30. raise Exception(str(e))
  31.  
  32.  
  33. def refactor_to_dynamo_object(item):
  34. """
  35. Changes the format of the dictionary to another dictionary to save it in DynamoDB.
  36. :param item: JSON object with the next format:
  37. {
  38. 'ENDPOINT_FIELD': environment variable that contains a string with the end point
  39. 'AUTH_FIELD': environment variable that contains a string with the auth
  40. 'PDH_FIELD': environment variable that contains a string with the end p256dh
  41. 'EMAIL_FIELD': environment variable that contains a string with the email
  42. }
  43. :return:
  44. Returns a new JSON with a DynamoDB format.
  45. """
  46. try:
  47. dynamo_object = {}
  48.  
  49. dynamo_object[os.environ['ENDPOINT_FIELD']] = {'S': item[os.environ['ENDPOINT_FIELD']]}
  50. dynamo_object[os.environ['AUTH_FIELD']] = {'S': item[os.environ['AUTH_FIELD']]}
  51. dynamo_object[os.environ['PDH_FIELD']] = {'S': item[os.environ['PDH_FIELD']]}
  52. dynamo_object[os.environ['EMAIL_FIELD']] = {
  53. 'S': get_key_even_if_not_exist(key=os.environ['EMAIL_FIELD'], dictionary=item, substitute_element=" ")}
  54.  
  55. return dynamo_object
  56. except Exception as e:
  57. logger.error('|'.join(['LAMBDA HANDLER|REFACTOR TO DYNAMO OBJECT|FINISH|KO', str(e)]))
  58. return None
  59.  
  60.  
  61. def lambda_handler(event, context):
  62. """
  63. Lambda to save the FCM credentials in dynamoDB.
  64. :params event :
  65. :return: boolean goal.
  66. """
  67. try:
  68. logger.info('LAMBDA HANDLER|SAVE CREDENTIALS FCM|INIT')
  69.  
  70. dyncommons_object = DynCommons()
  71. dyncommons_object.create_resource()
  72. result = dyncommons_object.put_item(os.environ['CREDENTIALS_TABLE'], refactor_to_dynamo_object(event))
  73.  
  74. logger.info('LAMBDA HANDLER|SAVE CREDENTIALS FCM|FIN')
  75. return result
  76. except Exception as e:
  77. logger.error('|'.join(['LAMBDA HANDLER|SAVE CREDENTIALS FCM|FINISH|KO', str(e)]))
  78. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement