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 API exception #113

Merged
Merged
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions odins_spear/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ class OSFileNotFound(OSError):

def __str__(self) -> str:
return f"File can not be found, please check path and file name."


class OSLicenseNonExistent(OSError):
""" Raised when the Specified Entity doesn't exist due to licensing.

class OSApiResponseError(OSError):
""" Raised when Odin Api returns an error code.
"""
def __init__(self, response):
response = response.json()
self.response = f"{response['details']} {response['status']}: {response['error']}"

def __str__(self) -> str:
return f"Specified Entity doesn't have the correct License."
return self.response
10 changes: 3 additions & 7 deletions odins_spear/methods/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,9 @@ def user_call_center(self, user_id: str):
params = {
"userId": user_id
}
try:
import requests.exceptions
response = self.requester.get(endpoint, params=params)
except requests.exceptions.RequestException:
raise OSLicenseNonExistent
else:
return response

return self.requester.get(endpoint, params=params)



def group_call_center_bounced_calls(self, service_user_id: str):
Expand Down
19 changes: 15 additions & 4 deletions odins_spear/requester.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import json
from .exceptions import OSApiResponseError

from ratelimit import limits, sleep_and_retry
class Requester():
Expand Down Expand Up @@ -70,8 +71,12 @@ def _request(self, method, endpoint, data=None, params=None):
self.logger._log_request(endpoint=endpoint, response_code=response.status_code)

# flags errors if any returned from the API
response.raise_for_status()
return response.json()
try:
response.raise_for_status()
except requests.exceptions.RequestException:
raise OSApiResponseError(response)
else:
return response.json()


@sleep_and_retry
Expand All @@ -92,6 +97,12 @@ def _rate_limited_request(self, method, endpoint, data=None, params=None):
self.logger._log_request(endpoint=endpoint, response_code=response.status_code)

# flags errors if any returned from the API
response.raise_for_status()
return response.json()
try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this to the the _request method. Its only applied to the rate limited version.

response.raise_for_status()
except requests.exceptions.RequestException:
raise OSApiResponseError(response)
else:
return response.json()