Skip to content

Commit

Permalink
Merge pull request #7 from MTrab:Better-logging
Browse files Browse the repository at this point in the history
Adding better connection state logging
  • Loading branch information
MTrab authored Oct 10, 2022
2 parents ff1d72f + ab45acc commit 5ad0c64
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
27 changes: 19 additions & 8 deletions pydanfossally/danfossallyapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

import requests

from .exceptions import (
InternalServerError,
NotFoundError,
UnauthorizedError,
UnexpectedError,
)

API_HOST = "https://api.danfoss.com"

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions pydanfossally/exceptions.py
Original file line number Diff line number Diff line change
@@ -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):
...

0 comments on commit 5ad0c64

Please sign in to comment.