diff --git a/odins_spear/exceptions.py b/odins_spear/exceptions.py index 8047999..6c8df1a 100644 --- a/odins_spear/exceptions.py +++ b/odins_spear/exceptions.py @@ -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." \ No newline at end of file + return self.response \ No newline at end of file diff --git a/odins_spear/methods/get.py b/odins_spear/methods/get.py index 3f9dc29..b995fc5 100644 --- a/odins_spear/methods/get.py +++ b/odins_spear/methods/get.py @@ -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): diff --git a/odins_spear/requester.py b/odins_spear/requester.py index e68abd6..48a1dc9 100644 --- a/odins_spear/requester.py +++ b/odins_spear/requester.py @@ -1,5 +1,6 @@ import requests import json +from .exceptions import OSApiResponseError from ratelimit import limits, sleep_and_retry class Requester(): @@ -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 @@ -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: + response.raise_for_status() + except requests.exceptions.RequestException: + raise OSApiResponseError(response) + else: + return response.json() + + \ No newline at end of file