Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def send_success_to_webhook(self, public_id: str, gltf_path: str, gltf_size: int):
- values: object = {'publicId': public_id,
- 'gltfPath': gltf_path,
- 'gltfSize': gltf_size,
- 'success': True}
- self.contact_webhook(values)
- def send_error_to_webhook(self, public_id: str, error: Exception):
- error_type = WebhookClient.get_error_type(error)
- values: object = {'publicId': public_id,
- 'success': False,
- 'errorType': error_type,
- 'errorMessage': str(error)}
- try:
- self.contact_webhook(values)
- except WebhookException as webhook_exception:
- logger.error(str(webhook_exception), exc_info=True)
- @staticmethod
- def contact_webhook(values: object):
- try:
- data: str = json.dumps(values)
- signature_header: str = WebhookClient.calculate_hash(str(data))
- headers = {'Content-Type': 'application/json', SIGNATURE_HEADER: signature_header}
- response = requests.post(WEBHOOK_URL, data=data, headers=headers)
- if response.ok:
- logger.info(
- 'Contact webhook SUCCEED, url: [{}], requestBody: [{}], responseCode: [{}], responseBody: [{}]'
- .format(WEBHOOK_URL, str(values), str(response.status_code), str(response.content)))
- else:
- logger.error(
- 'Contact webhook FAILED, url: [{}], requestBody: [{}], responseCode: [{}], responseBody: [{}]'
- .format(WEBHOOK_URL, str(values), str(response.status_code), str(response.content)))
- except Exception as exception:
- raise WebhookException('Contact webhook FAILED, url: [{}], body: [{}]'.format(WEBHOOK_URL, str(values)),
- exception) from exception
- @staticmethod
- def calculate_hash(data: str):
- timestamp: str = str(int(time.time()))
- signature_data: str = re.sub('\s+', '', timestamp + '.' + data)
- return timestamp + ',' + hmac.new(WEBHOOK_SECRET.encode('utf-8'), signature_data.encode('utf-8'),
- 'sha256').hexdigest()
- @staticmethod
- def get_error_type(error: Exception):
- if isinstance(error, AbstractException):
- return error.get_error()
- else:
- return ErrorCode.ERROR_UNKNOWN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement