From 00af25cad4d93e22bc2295734608363b2d610162 Mon Sep 17 00:00:00 2001 From: Archana-GS Date: Thu, 2 Jan 2025 11:26:09 +0530 Subject: [PATCH] add raise_for_status parameter to HTTP wrapper methods --- endpoints/base_api.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/endpoints/base_api.py b/endpoints/base_api.py index a4724ab6..e104b27b 100644 --- a/endpoints/base_api.py +++ b/endpoints/base_api.py @@ -11,12 +11,13 @@ class BaseAPI: session_object = requests.Session() base_url = None - def get(self, url, headers=None): + def get(self, url, headers=None, raise_for_status=True): "Get request" headers = headers if headers else {} try: response = self.session_object.get(url=url, headers=headers) - response.raise_for_status() + if raise_for_status: + response.raise_for_status() except HTTPError as http_err: print(f"GET request failed: {http_err}") except ConnectionError: @@ -26,7 +27,7 @@ def get(self, url, headers=None): return response # pylint: disable=too-many-arguments - def post(self, url,params=None, data=None,json=None,headers=None): + def post(self, url,params=None, data=None, json=None, headers=None, raise_for_status=True): "Post request" headers = headers if headers else {} try: @@ -35,7 +36,8 @@ def post(self, url,params=None, data=None,json=None,headers=None): data=data, json=json, headers=headers) - response.raise_for_status() + if raise_for_status: + response.raise_for_status() except HTTPError as http_err: print(f"POST request failed: {http_err}") except ConnectionError: @@ -45,12 +47,13 @@ def post(self, url,params=None, data=None,json=None,headers=None): return response - def delete(self, url,headers=None): + def delete(self, url,headers=None, raise_for_status=True): "Delete request" headers = headers if headers else {} try: response = self.session_object.delete(url, headers=headers) - response.raise_for_status() + if raise_for_status: + response.raise_for_status() except HTTPError as http_err: print(f"DELETE request failed: {http_err}") except ConnectionError: @@ -59,12 +62,13 @@ def delete(self, url,headers=None): print(f"\033[1;31mAn error occurred: {err}\033[1;m") return response - def put(self,url,json=None, headers=None): + def put(self,url,json=None, headers=None, raise_for_status=True): "Put request" headers = headers if headers else {} try: response = self.session_object.put(url, json=json, headers=headers) - response.raise_for_status() + if raise_for_status: + response.raise_for_status() except HTTPError as http_err: print(f"PUT request failed: {http_err}") except ConnectionError: