From 824a1ffc915bc22abac52a8e95172c8c9e7b9bd1 Mon Sep 17 00:00:00 2001 From: Roshan Piyush Date: Tue, 2 Jul 2024 08:21:38 +0530 Subject: [PATCH] Timeout handling for gateway (#259) Timeout handling for gateway --- services/workshop/crapi/shop/views.py | 38 +++++++++++++++------------ 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/services/workshop/crapi/shop/views.py b/services/workshop/crapi/shop/views.py index a7965cea..9fd30a96 100644 --- a/services/workshop/crapi/shop/views.py +++ b/services/workshop/crapi/shop/views.py @@ -137,24 +137,28 @@ def get(self, request, order_id=None, user=None): data["user"] = user_dict data["order"] = order_serializer.data data["amount"] = float(order.product.price) * int(order.quantity) - payment_response = requests.post( - gateway_endpoint, - headers={ - "Authorization": gateway_credential, - "Content-Type": "application/json", - }, - json=data, - verify=False, - ) - if payment_response.status_code == 200: - payment = payment_response.json() - else: - logging.error( - "Payment response error, {}: {}".format( - payment_response.status_code, payment_response.content - ) + try: + payment_response = requests.post( + gateway_endpoint, + headers={ + "Authorization": gateway_credential, + "Content-Type": "application/json", + }, + json=data, + verify=False, + timeout=5, ) - logging.debug("payment response: {}".format(payment)) + if payment_response.status_code == 200: + payment = payment_response.json() + else: + logging.error( + "Payment response error, {}: {}".format( + payment_response.status_code, payment_response.content + ) + ) + logging.debug("payment response: {}".format(payment)) + except Exception as e: + logging.error(e, exc_info=True) except Exception as e: logging.error(e, exc_info=True) response_data = dict(order=order_serializer.data, payment=payment)