Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add raise_for_status parameter to HTTP wrapper methods #500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions endpoints/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down