mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 15:03:34 +00:00
outgoing webhook system: Minor fixes.
This fixes some error message strings and skips converting request_data into json. From now, conversion would be the responsibility of interface. Also, base_url is now not passed into event structure.
This commit is contained in:
@@ -41,7 +41,7 @@ class OutgoingWebhookServiceInterface(object):
|
|||||||
# It should be in the appropriate form. Ex: certain services require the post data to be in
|
# It should be in the appropriate form. Ex: certain services require the post data to be in
|
||||||
# json form. Hence request_data returned here should be a json.
|
# json form. Hence request_data returned here should be a json.
|
||||||
def process_event(self, event):
|
def process_event(self, event):
|
||||||
# type: (Dict[str, Any]) -> Tuple[Dict[str, Any], Any]
|
# type: (Dict[Text, Any]) -> Tuple[Dict[str, Any], Any]
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
# Given a successful response to the outgoing webhook REST operation, determines if response is required
|
# Given a successful response to the outgoing webhook REST operation, determines if response is required
|
||||||
@@ -125,21 +125,21 @@ def do_rest_call(rest_operation, request_data, event, service_handler, timeout=N
|
|||||||
request_kwargs['timeout'] = timeout
|
request_kwargs['timeout'] = timeout
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.request(http_method, final_url, data=json.dumps(request_data), **request_kwargs)
|
response = requests.request(http_method, final_url, data=request_data, **request_kwargs)
|
||||||
if str(response.status_code).startswith('2'):
|
if str(response.status_code).startswith('2'):
|
||||||
response_message = service_handler.process_success(response, event)
|
response_message = service_handler.process_success(response, event)
|
||||||
succeed_with_message(event, response_message)
|
succeed_with_message(event, response_message)
|
||||||
|
|
||||||
# On 50x errors, try retry
|
# On 50x errors, try retry
|
||||||
elif str(response.status_code).startswith('5'):
|
elif str(response.status_code).startswith('5'):
|
||||||
request_retry(event, "unable to connect with the third party.")
|
request_retry(event, "Internal Server error at third party.")
|
||||||
else:
|
else:
|
||||||
response_message = service_handler.process_failure(response, event)
|
response_message = service_handler.process_failure(response, event)
|
||||||
fail_with_message(event, response_message)
|
fail_with_message(event, response_message)
|
||||||
|
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
logging.info("Trigger event %s on %s timed out. Retrying" % (event["command"], event['service_name']))
|
logging.info("Trigger event %s on %s timed out. Retrying" % (event["command"], event['service_name']))
|
||||||
request_retry(event, 'unable to connect with the third party.')
|
request_retry(event, 'Unable to connect with the third party.')
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
response_message = "An exception occured for message `%s`! See the logs for more information." % (event["command"],)
|
response_message = "An exception occured for message `%s`! See the logs for more information." % (event["command"],)
|
||||||
|
|||||||
@@ -13,14 +13,15 @@ class GenericOutgoingWebhookService(OutgoingWebhookServiceInterface):
|
|||||||
super(GenericOutgoingWebhookService, self).__init__(base_url, token, user_profile, service_name)
|
super(GenericOutgoingWebhookService, self).__init__(base_url, token, user_profile, service_name)
|
||||||
|
|
||||||
def process_event(self, event):
|
def process_event(self, event):
|
||||||
# type: (Dict[str, Any]) -> Tuple[Dict[str, Any], Any]
|
# type: (Dict[Text, Any]) -> Tuple[Dict[str, Any], Any]
|
||||||
rest_operation = {'method': 'POST',
|
rest_operation = {'method': 'POST',
|
||||||
'relative_url_path': '',
|
'relative_url_path': '',
|
||||||
'base_url': event['base_url'],
|
'base_url': self.base_url,
|
||||||
'request_kwargs': {}}
|
'request_kwargs': {}}
|
||||||
request_data = {"data": event['command'],
|
request_data = {"data": event['command'],
|
||||||
"message": event['message']}
|
"message": event['message'],
|
||||||
return rest_operation, request_data
|
"token": self.token}
|
||||||
|
return rest_operation, json.dumps(request_data)
|
||||||
|
|
||||||
def process_success(self, response, event):
|
def process_success(self, response, event):
|
||||||
# type: (Response, Dict[Text, Any]) -> Optional[str]
|
# type: (Response, Dict[Text, Any]) -> Optional[str]
|
||||||
|
|||||||
@@ -466,7 +466,6 @@ class OutgoingWebhookWorker(QueueProcessingWorker):
|
|||||||
services = get_bot_services(event['user_profile_id'])
|
services = get_bot_services(event['user_profile_id'])
|
||||||
for service in services:
|
for service in services:
|
||||||
dup_event['service_name'] = str(service.name)
|
dup_event['service_name'] = str(service.name)
|
||||||
dup_event['base_url'] = str(service.base_url)
|
|
||||||
service_handler = get_outgoing_webhook_service_handler(service)
|
service_handler = get_outgoing_webhook_service_handler(service)
|
||||||
rest_operation, request_data = service_handler.process_event(dup_event)
|
rest_operation, request_data = service_handler.process_event(dup_event)
|
||||||
do_rest_call(rest_operation, request_data, dup_event, service_handler)
|
do_rest_call(rest_operation, request_data, dup_event, service_handler)
|
||||||
|
|||||||
Reference in New Issue
Block a user