Skip to content

Commit 5ad0c64

Browse files
authored
Merge pull request #7 from MTrab:Better-logging
Adding better connection state logging
2 parents ff1d72f + ab45acc commit 5ad0c64

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

pydanfossally/danfossallyapi.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
import requests
1010

11+
from .exceptions import (
12+
InternalServerError,
13+
NotFoundError,
14+
UnauthorizedError,
15+
UnexpectedError,
16+
)
17+
1118
API_HOST = "https://api.danfoss.com"
1219

1320
_LOGGER = logging.getLogger(__name__)
@@ -38,16 +45,20 @@ def _call(
3845
else:
3946
req = requests.get(API_HOST + path, headers=headers_data, timeout=10)
4047

41-
if not req.ok:
42-
return False
43-
except TimeoutError:
44-
_LOGGER.warning("Timeout communication with Danfoss Ally API")
48+
req.raise_for_status()
49+
except requests.exceptions.HTTPError as err:
50+
code = err.response.status_code
51+
if code == 401:
52+
raise UnauthorizedError
53+
if code == 404:
54+
raise NotFoundError
55+
if code == 500:
56+
raise InternalServerError
4557
return False
58+
except TimeoutError:
59+
raise TimeoutError
4660
except:
47-
_LOGGER.warning(
48-
"Unexpected error occured in communications with Danfoss Ally API!"
49-
)
50-
return False
61+
raise UnexpectedError
5162

5263
json = req.json()
5364
print("JSON: ", json)

pydanfossally/exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Exceptions for Danfoss Ally."""
2+
3+
from http.client import HTTPException
4+
5+
6+
class NotFoundError(HTTPException):
7+
...
8+
9+
10+
class InternalServerError(HTTPException):
11+
...
12+
13+
14+
class UnauthorizedError(HTTPException):
15+
...
16+
17+
18+
class UnexpectedError(Exception):
19+
...

0 commit comments

Comments
 (0)