mirror of https://github.com/zulip/zulip.git
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:
parent
ff63f0b1d3
commit
87dcd5442a
|
@ -41,7 +41,7 @@ class OutgoingWebhookServiceInterface(object):
|
|||
# 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.
|
||||
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()
|
||||
|
||||
# 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
|
||||
|
||||
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'):
|
||||
response_message = service_handler.process_success(response, event)
|
||||
succeed_with_message(event, response_message)
|
||||
|
||||
# On 50x errors, try retry
|
||||
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:
|
||||
response_message = service_handler.process_failure(response, event)
|
||||
fail_with_message(event, response_message)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
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:
|
||||
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)
|
||||
|
||||
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',
|
||||
'relative_url_path': '',
|
||||
'base_url': event['base_url'],
|
||||
'base_url': self.base_url,
|
||||
'request_kwargs': {}}
|
||||
request_data = {"data": event['command'],
|
||||
"message": event['message']}
|
||||
return rest_operation, request_data
|
||||
"message": event['message'],
|
||||
"token": self.token}
|
||||
return rest_operation, json.dumps(request_data)
|
||||
|
||||
def process_success(self, response, event):
|
||||
# type: (Response, Dict[Text, Any]) -> Optional[str]
|
||||
|
|
|
@ -466,7 +466,6 @@ class OutgoingWebhookWorker(QueueProcessingWorker):
|
|||
services = get_bot_services(event['user_profile_id'])
|
||||
for service in services:
|
||||
dup_event['service_name'] = str(service.name)
|
||||
dup_event['base_url'] = str(service.base_url)
|
||||
service_handler = get_outgoing_webhook_service_handler(service)
|
||||
rest_operation, request_data = service_handler.process_event(dup_event)
|
||||
do_rest_call(rest_operation, request_data, dup_event, service_handler)
|
||||
|
|
Loading…
Reference in New Issue