diff --git a/src/otf_api/api.py b/src/otf_api/api.py index f237098..91b0401 100644 --- a/src/otf_api/api.py +++ b/src/otf_api/api.py @@ -86,15 +86,13 @@ def _do( LOGGER.exception(f"Error making request: {e}") LOGGER.exception(f"Response: {response.text}") raise + except httpx.HTTPStatusError as e: + LOGGER.exception(f"Error making request: {e}") + raise exc.OtfRequestError("Error making request", response=response, request=request) except Exception as e: LOGGER.exception(f"Error making request: {e}") raise - if isinstance(response, dict) and "code" in response and response["code"] != "SUCCESS": - LOGGER.error(f"Error making request: {response}") - LOGGER.error(f"Response: {response.text}") - raise exc.OtfRequestError("Error making request", response=response, request=request) - return response.json() def _classes_request(self, method: str, url: str, params: dict[str, Any] | None = None) -> Any: @@ -272,12 +270,12 @@ def book_class(self, otf_class: str | models.OtfClass) -> models.Booking: try: resp = self._default_request("PUT", f"/member/members/{self.member_uuid}/bookings", json=body) except exc.OtfRequestError as e: - resp = e.response + resp_obj = e.response.json() - if resp["code"] == "ERROR": - if resp["data"]["errorCode"] == "603": + if resp_obj["code"] == "ERROR": + if resp_obj["data"]["errorCode"] == "603": raise exc.AlreadyBookedError(f"Class {class_uuid} is already booked.") - if resp["data"]["errorCode"] == "602": + if resp_obj["data"]["errorCode"] == "602": raise exc.OutsideSchedulingWindowError(f"Class {class_uuid} is outside the scheduling window.") raise diff --git a/src/otf_api/exceptions.py b/src/otf_api/exceptions.py index fd64be0..61a6d25 100644 --- a/src/otf_api/exceptions.py +++ b/src/otf_api/exceptions.py @@ -1,3 +1,6 @@ +from httpx import Request, Response + + class OtfException(Exception): """Base class for all exceptions in this package.""" @@ -5,10 +8,10 @@ class OtfException(Exception): class OtfRequestError(OtfException): """Raised when an error occurs while making a request to the OTF API.""" - response: dict - request: dict + response: Response + request: Request - def __init__(self, message: str, response: dict, request: dict): + def __init__(self, message: str, response: Response, request: Request): super().__init__(message) self.response = response self.request = request