Skip to content

Commit b6d4fd3

Browse files
committed
Don't parse date strings in get_object_storage_network_statistics
This changes the `get_object_storage_network_statistics` method to not accept arbitrary strings that might or might not contain dates. The parsing of those strings expects them to be in one strict format and in the local time zone, which may or may not be the case. Instead, it's simpler to just only accept `datetime.datetime` objects and call `.isoformat()` on them. This has the happy consequence of being able to remove the `dateutil` dependency too.
1 parent 1966aab commit b6d4fd3

File tree

5 files changed

+12
-22
lines changed

5 files changed

+12
-22
lines changed

docs/object_storage-mixin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def get_object_storage_network_statistics(
6666
"""
6767
The network usage of an Object Storage device is metered and can be reviewed using the statistics request.
6868
Object_storage is mandatory and can be a uuid or a ObjectStorage object.
69-
Datetime_from is mandatory and needs to be a string example: 2020-11-03 00:00:00
70-
Datetime_to is optional and needs to be a string example: 2020-11-04 00:00:00
69+
Datetime_from is mandatory and needs to be a Datetime.
70+
Datetime_to is optional and needs to be a Datetime.
7171
Interval is optional and needs to be an integer
7272
Bucket is optional and needs to be a list of bucket name strings
7373
Filename is optional and needs to be a list of filename strings

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ python_requires = >=3.6, <4
1515
setup_requires =
1616
setuptools
1717
install_requires =
18-
python-dateutil
1918
requests
2019
packages =
2120
upcloud_api

test/test_object_storage.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
13
import responses
24
from conftest import Mock
35

@@ -63,12 +65,13 @@ def test_delete_object_storage(self, manager):
6365

6466
@responses.activate
6567
def test_get_object_storage_network_statistics(self, manager):
66-
data = Mock.mock_get(
68+
Mock.mock_get(
6769
'object-storage/06b0e4fc-d74b-455e-a373-60cd6ca84022/stats/network/',
6870
response_file='object-storage_06b0e4fc-d74b-455e-a373-60cd6ca84022_stats_network.json',
6971
)
7072
res = manager.get_object_storage_network_statistics(
71-
'06b0e4fc-d74b-455e-a373-60cd6ca84022', '2020-11-03 00:00:00'
73+
object_storage='06b0e4fc-d74b-455e-a373-60cd6ca84022',
74+
datetime_from=datetime.datetime(2020, 11, 3),
7275
)
7376

7477
assert 'stats' in res

upcloud_api/cloud_manager/object_storage_mixin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import datetime
12
from typing import List, Optional
23

34
from upcloud_api.object_storage import ObjectStorage
4-
from upcloud_api.utils import convert_datetime_string_to_object
55

66

77
class ObjectStorageManager:
@@ -82,8 +82,8 @@ def delete_object_storage(self, object_storage):
8282
def get_object_storage_network_statistics(
8383
self,
8484
object_storage,
85-
datetime_from,
86-
datetime_to=None,
85+
datetime_from: datetime.datetime,
86+
datetime_to: Optional[datetime.datetime] = None,
8787
interval: Optional[int] = None,
8888
bucket: Optional[List[str]] = None,
8989
filename: Optional[List[str]] = None,
@@ -96,11 +96,11 @@ def get_object_storage_network_statistics(
9696
"""
9797
The network usage of an Object Storage device is metered and can be reviewed using the statistics request.
9898
"""
99-
key_dict = {'from': convert_datetime_string_to_object(datetime_from)}
99+
key_dict = {'from': datetime_from.isoformat(timespec='seconds')}
100100
url = f'/object-storage/{object_storage}/stats/network/?'
101101

102102
if datetime_to:
103-
key_dict['to'] = convert_datetime_string_to_object(datetime_to)
103+
key_dict['to'] = datetime_to.isoformat(timespec='seconds')
104104
if interval:
105105
key_dict['interval'] = interval
106106
if bucket:

upcloud_api/utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import itertools
2-
from datetime import datetime
32
from time import sleep
43

5-
from dateutil import tz
6-
74
from upcloud_api.errors import UpCloudAPIError, UpCloudClientError
85

96

@@ -35,12 +32,3 @@ def get_raw_data_from_file(file):
3532
data = file.read()
3633
file.close()
3734
return data
38-
39-
40-
def convert_datetime_string_to_object(datetime_string):
41-
"""
42-
Helper function to convert datetime string to object with local timezone
43-
"""
44-
local_tz = tz.tzlocal()
45-
datetime_object = datetime.strptime(datetime_string, '%Y-%m-%d %H:%M:%S')
46-
return datetime_object.replace(tzinfo=local_tz, microsecond=0).isoformat()

0 commit comments

Comments
 (0)