diff --git a/pydanfossally/danfossallyapi.py b/pydanfossally/danfossallyapi.py index 5205571..108a2e4 100644 --- a/pydanfossally/danfossallyapi.py +++ b/pydanfossally/danfossallyapi.py @@ -8,6 +8,13 @@ import requests +from .exceptions import ( + InternalServerError, + NotFoundError, + UnauthorizedError, + UnexpectedError, +) + API_HOST = "https://api.danfoss.com" _LOGGER = logging.getLogger(__name__) @@ -38,16 +45,20 @@ def _call( else: req = requests.get(API_HOST + path, headers=headers_data, timeout=10) - if not req.ok: - return False - except TimeoutError: - _LOGGER.warning("Timeout communication with Danfoss Ally API") + req.raise_for_status() + except requests.exceptions.HTTPError as err: + code = err.response.status_code + if code == 401: + raise UnauthorizedError + if code == 404: + raise NotFoundError + if code == 500: + raise InternalServerError return False + except TimeoutError: + raise TimeoutError except: - _LOGGER.warning( - "Unexpected error occured in communications with Danfoss Ally API!" - ) - return False + raise UnexpectedError json = req.json() print("JSON: ", json) diff --git a/pydanfossally/exceptions.py b/pydanfossally/exceptions.py new file mode 100644 index 0000000..0d1a245 --- /dev/null +++ b/pydanfossally/exceptions.py @@ -0,0 +1,19 @@ +"""Exceptions for Danfoss Ally.""" + +from http.client import HTTPException + + +class NotFoundError(HTTPException): + ... + + +class InternalServerError(HTTPException): + ... + + +class UnauthorizedError(HTTPException): + ... + + +class UnexpectedError(Exception): + ...