Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1.     def send_success_to_webhook(self, public_id: str, gltf_path: str, gltf_size: int):
  2.         values: object = {'publicId': public_id,
  3.                           'gltfPath': gltf_path,
  4.                           'gltfSize': gltf_size,
  5.                           'success': True}
  6.  
  7.         self.contact_webhook(values)
  8.  
  9.     def send_error_to_webhook(self, public_id: str, error: Exception):
  10.         error_type = WebhookClient.get_error_type(error)
  11.  
  12.         values: object = {'publicId': public_id,
  13.                           'success': False,
  14.                           'errorType': error_type,
  15.                           'errorMessage': str(error)}
  16.         try:
  17.             self.contact_webhook(values)
  18.         except WebhookException as webhook_exception:
  19.             logger.error(str(webhook_exception), exc_info=True)
  20.  
  21.     @staticmethod
  22.     def contact_webhook(values: object):
  23.         try:
  24.             data: str = json.dumps(values)
  25.             signature_header: str = WebhookClient.calculate_hash(str(data))
  26.             headers = {'Content-Type': 'application/json', SIGNATURE_HEADER: signature_header}
  27.             response = requests.post(WEBHOOK_URL, data=data, headers=headers)
  28.  
  29.             if response.ok:
  30.                 logger.info(
  31.                     'Contact webhook SUCCEED, url: [{}], requestBody: [{}], responseCode: [{}], responseBody: [{}]'
  32.                         .format(WEBHOOK_URL, str(values), str(response.status_code), str(response.content)))
  33.             else:
  34.                 logger.error(
  35.                     'Contact webhook FAILED, url: [{}], requestBody: [{}], responseCode: [{}], responseBody: [{}]'
  36.                         .format(WEBHOOK_URL, str(values), str(response.status_code), str(response.content)))
  37.         except Exception as exception:
  38.             raise WebhookException('Contact webhook FAILED, url: [{}], body: [{}]'.format(WEBHOOK_URL, str(values)),
  39.                                    exception) from exception
  40.  
  41.     @staticmethod
  42.     def calculate_hash(data: str):
  43.         timestamp: str = str(int(time.time()))
  44.         signature_data: str = re.sub('\s+', '', timestamp + '.' + data)
  45.         return timestamp + ',' + hmac.new(WEBHOOK_SECRET.encode('utf-8'), signature_data.encode('utf-8'),
  46.                                           'sha256').hexdigest()
  47.  
  48.     @staticmethod
  49.     def get_error_type(error: Exception):
  50.         if isinstance(error, AbstractException):
  51.             return error.get_error()
  52.         else:
  53.             return ErrorCode.ERROR_UNKNOWN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement