File tree Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Original file line number Diff line number Diff line change 8
8
9
9
import requests
10
10
11
+ from .exceptions import (
12
+ InternalServerError ,
13
+ NotFoundError ,
14
+ UnauthorizedError ,
15
+ UnexpectedError ,
16
+ )
17
+
11
18
API_HOST = "https://api.danfoss.com"
12
19
13
20
_LOGGER = logging .getLogger (__name__ )
@@ -38,16 +45,20 @@ def _call(
38
45
else :
39
46
req = requests .get (API_HOST + path , headers = headers_data , timeout = 10 )
40
47
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
45
57
return False
58
+ except TimeoutError :
59
+ raise TimeoutError
46
60
except :
47
- _LOGGER .warning (
48
- "Unexpected error occured in communications with Danfoss Ally API!"
49
- )
50
- return False
61
+ raise UnexpectedError
51
62
52
63
json = req .json ()
53
64
print ("JSON: " , json )
Original file line number Diff line number Diff line change
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
+ ...
You can’t perform that action at this time.
0 commit comments