Merge pull request #5983 from PostmanSpat/webhook-raw-retry
Added raw config and retry config to webhook
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
This module manages webhook communication
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
from typing import Any, Dict
|
||||
|
||||
from requests import RequestException, post
|
||||
@@ -28,12 +29,9 @@ class Webhook(RPCHandler):
|
||||
super().__init__(rpc, config)
|
||||
|
||||
self._url = self._config['webhook']['url']
|
||||
|
||||
self._format = self._config['webhook'].get('format', 'form')
|
||||
|
||||
if self._format != 'form' and self._format != 'json':
|
||||
raise NotImplementedError('Unknown webhook format `{}`, possible values are '
|
||||
'`form` (default) and `json`'.format(self._format))
|
||||
self._retries = self._config['webhook'].get('retries', 0)
|
||||
self._retry_delay = self._config['webhook'].get('retry_delay', 0.1)
|
||||
|
||||
def cleanup(self) -> None:
|
||||
"""
|
||||
@@ -77,13 +75,30 @@ class Webhook(RPCHandler):
|
||||
def _send_msg(self, payload: dict) -> None:
|
||||
"""do the actual call to the webhook"""
|
||||
|
||||
try:
|
||||
if self._format == 'form':
|
||||
post(self._url, data=payload)
|
||||
elif self._format == 'json':
|
||||
post(self._url, json=payload)
|
||||
else:
|
||||
raise NotImplementedError('Unknown format: {}'.format(self._format))
|
||||
success = False
|
||||
attempts = 0
|
||||
while not success and attempts <= self._retries:
|
||||
if attempts:
|
||||
if self._retry_delay:
|
||||
time.sleep(self._retry_delay)
|
||||
logger.info("Retrying webhook...")
|
||||
|
||||
except RequestException as exc:
|
||||
logger.warning("Could not call webhook url. Exception: %s", exc)
|
||||
attempts += 1
|
||||
|
||||
try:
|
||||
if self._format == 'form':
|
||||
response = post(self._url, data=payload)
|
||||
elif self._format == 'json':
|
||||
response = post(self._url, json=payload)
|
||||
elif self._format == 'raw':
|
||||
response = post(self._url, data=payload['data'],
|
||||
headers={'Content-Type': 'text/plain'})
|
||||
else:
|
||||
raise NotImplementedError('Unknown format: {}'.format(self._format))
|
||||
|
||||
# Throw a RequestException if the post was not successful
|
||||
response.raise_for_status()
|
||||
success = True
|
||||
|
||||
except RequestException as exc:
|
||||
logger.warning("Could not call webhook url. Exception: %s", exc)
|
||||
|
Reference in New Issue
Block a user