|
@@ -4,8 +4,10 @@ Provides access to the models stored in the database, via the server.
|
4
|
4
|
from __future__ import annotations
|
5
|
5
|
|
6
|
6
|
import datetime
|
|
7
|
+import enum
|
7
|
8
|
import logging
|
8
|
|
-from typing import Any, List, NamedTuple, Optional, Sequence, Tuple
|
|
9
|
+from dataclasses import dataclass
|
|
10
|
+from typing import Any, List, NamedTuple, Optional, Sequence, Tuple, Union
|
9
|
11
|
from urllib.parse import urljoin
|
10
|
12
|
|
11
|
13
|
import requests
|
|
@@ -16,36 +18,75 @@ SERVER_URL = "http://127.0.0.1:5000"
|
16
|
18
|
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
|
17
|
19
|
|
18
|
20
|
|
|
21
|
+class NetworkError(enum.Enum):
|
|
22
|
+ """Represents errors that might occur when communicating with the server."""
|
|
23
|
+
|
|
24
|
+ HttpFailure = "http_failure"
|
|
25
|
+ """Returned when the server returns a non-successful status code."""
|
|
26
|
+
|
|
27
|
+ ConnectionFailure = "connection_failure"
|
|
28
|
+ """Returned when we can't connect to the server at all."""
|
|
29
|
+
|
|
30
|
+ InvalidData = "invalid_data"
|
|
31
|
+
|
|
32
|
+
|
19
|
33
|
class ServerStatus:
|
20
|
34
|
""" Provides helper classes to check whether the server is up. """
|
21
|
35
|
|
22
|
36
|
@classmethod
|
23
|
|
- def is_server_running(cls) -> Tuple[bool, Any]:
|
|
37
|
+ def is_server_running(cls) -> Union[bool, NetworkError]:
|
24
|
38
|
try:
|
25
|
39
|
req = requests.get(urljoin(SERVER_URL, "ping"))
|
26
|
|
-
|
27
|
|
- if req.status_code == 200:
|
28
|
|
- return True, req.content
|
29
|
|
- return False, req.content
|
|
40
|
+ req.raise_for_status()
|
30
|
41
|
|
31
|
42
|
except requests.ConnectionError as ex:
|
32
|
|
- return False, ex
|
|
43
|
+ LOG.exception(ex)
|
|
44
|
+ return NetworkError.ConnectionFailure
|
|
45
|
+
|
|
46
|
+ except requests.HTTPError as ex:
|
|
47
|
+ LOG.exception(ex)
|
|
48
|
+ return NetworkError.HttpFailure
|
|
49
|
+
|
|
50
|
+ return True
|
|
51
|
+
|
|
52
|
+ @dataclass(frozen=True)
|
|
53
|
+ class OpenConsumptions:
|
|
54
|
+ amount: int
|
|
55
|
+ first_timestamp: Optional[datetime.datetime]
|
|
56
|
+ last_timestamp: Optional[datetime.datetime]
|
33
|
57
|
|
34
|
58
|
@classmethod
|
35
|
|
- def unsettled_consumptions(cls) -> dict:
|
36
|
|
- req = requests.get(urljoin(SERVER_URL, "status"))
|
|
59
|
+ def unsettled_consumptions(cls) -> Union[OpenConsumptions, NetworkError]:
|
|
60
|
+ try:
|
|
61
|
+ req = requests.get(urljoin(SERVER_URL, "status"))
|
|
62
|
+ req.raise_for_status()
|
|
63
|
+ data = req.json()
|
37
|
64
|
|
38
|
|
- data = req.json()
|
|
65
|
+ except requests.ConnectionError as e:
|
|
66
|
+ LOG.exception(e)
|
|
67
|
+ return NetworkError.ConnectionFailure
|
39
|
68
|
|
40
|
|
- if data["unsettled"]["amount"]:
|
41
|
|
- data["unsettled"]["first"] = datetime.datetime.strptime(
|
42
|
|
- data["unsettled"]["first"], DATETIME_FORMAT
|
43
|
|
- )
|
44
|
|
- data["unsettled"]["last"] = datetime.datetime.strptime(
|
45
|
|
- data["unsettled"]["last"], DATETIME_FORMAT
|
|
69
|
+ except requests.HTTPError as e:
|
|
70
|
+ LOG.exception(e)
|
|
71
|
+ return NetworkError.HttpFailure
|
|
72
|
+
|
|
73
|
+ except ValueError as e:
|
|
74
|
+ LOG.exception(e)
|
|
75
|
+ return NetworkError.InvalidData
|
|
76
|
+
|
|
77
|
+ amount: int = data["unsettled"]["amount"]
|
|
78
|
+
|
|
79
|
+ if amount == 0:
|
|
80
|
+ return cls.OpenConsumptions(
|
|
81
|
+ amount=0, first_timestamp=None, last_timestamp=None
|
46
|
82
|
)
|
47
|
83
|
|
48
|
|
- return data
|
|
84
|
+ first = datetime.datetime.fromisoformat(data["unsettled"]["first"])
|
|
85
|
+ last = datetime.datetime.fromisoformat(data["unsettled"]["last"])
|
|
86
|
+
|
|
87
|
+ return cls.OpenConsumptions(
|
|
88
|
+ amount=amount, first_timestamp=first, last_timestamp=last
|
|
89
|
+ )
|
49
|
90
|
|
50
|
91
|
|
51
|
92
|
class Person(NamedTuple):
|