Skip to content

Commit d84e2c0

Browse files
ZoomeoTooknorMateoGreil
authored andcommitted
🐛 Refactor ComwattClient API endpoints and improve date formatting
- Update API endpoints by removing the '/api' prefix - Change 'box_id' parameter to 'macAddress' in get_box_details method - Modify the default value of 'measure_kind' to "QUANTITY" in get_devices_stats method - Format start and end dates using strftime and replace spaces with '%20' for proper URL encoding in get_networkstats and get_devices_stats methods
1 parent 210472a commit d84e2c0

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

comwatt_client_legacy/client.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import hashlib
2-
import json
32
import requests
43
from datetime import datetime
54

@@ -83,20 +82,20 @@ def get_owner_details(self, owner_id):
8382
8483
"""
8584

86-
url = f'{self.base_url}/api/indepboxes?ownerid={owner_id}'
85+
url = f'{self.base_url}/indepboxes?ownerid={owner_id}'
8786

8887
response = self.session.get(url)
8988
if response.status_code == 200:
9089
return response.json()
9190
else:
9291
raise Exception(f'Error retrieving owner details : {response.status_code}')
9392

94-
def get_box_details(self, box_id):
93+
def get_box_details(self, macAddress):
9594
"""
9695
Retrieves information about the box's details.
9796
9897
Args:
99-
box_id (str): The ID of the box.
98+
macAddress (str): The ID of the box.
10099
101100
Returns:
102101
dict: Information about the box.
@@ -106,8 +105,8 @@ def get_box_details(self, box_id):
106105
107106
"""
108107

109-
url = f'{self.base_url}/api/indepboxes/byMacAddress/{box_id}'
110-
108+
url = f'{self.base_url}/indepboxes/byMacAddress/{macAddress}'
109+
111110
response = self.session.get(url)
112111
if response.status_code == 200:
113112
return response.json()
@@ -129,7 +128,7 @@ def get_products(self):
129128
130129
"""
131130

132-
url = f'{self.base_url}/api/products/1'
131+
url = f'{self.base_url}/products/'
133132

134133
response = self.session.get(url)
135134
if response.status_code == 200:
@@ -152,7 +151,7 @@ def get_devices(self, indepbox_id):
152151
153152
"""
154153

155-
url = f'{self.base_url}/api/devices?indepbox_id={indepbox_id}'
154+
url = f'{self.base_url}/devices?indepbox_id={indepbox_id}'
156155

157156
response = self.session.get(url)
158157
if response.status_code == 200:
@@ -193,21 +192,23 @@ def get_networkstats(self, indepbox_id,
193192
194193
"""
195194

196-
url = (f'{self.base_url}/api/aggregations/networkstats?indepbox_id={indepbox_id}&'
197-
f'level={level}&'
198-
f'measure_kind={measure_kind}&'
199-
f'start={start}&'
200-
f'end={end}&'
201-
f'mm=')
195+
start_str = start.strftime('%Y-%m-%d %H:%M:%S').replace(' ', '%20')
196+
end_str = end.strftime('%Y-%m-%d %H:%M:%S').replace(' ', '%20')
202197

198+
url = (f'{self.base_url}/aggregations/networkstats?indepbox_id={indepbox_id}&'
199+
f'level={level}&'
200+
f'measure_kind={measure_kind}&'
201+
f'start={start_str}&'
202+
f'end={end_str}')
203+
203204
response = self.session.get(url)
204205
if response.status_code == 200:
205206
return response.json()
206207
else:
207208
raise Exception(f'Error retrieving networkstats: {response.status_code}')
208209

209210
def get_devices_stats(self, device_id,
210-
measure_kind="VIRTUAL_QUANTITY",
211+
measure_kind="QUANTITY",
211212
measure_type_id="1",
212213
level="HOUR",
213214
start=datetime.now(),
@@ -231,11 +232,14 @@ def get_devices_stats(self, device_id,
231232
Exception: If an error occurs while retrieving the devices_stats.
232233
"""
233234

234-
url = (f'{self.base_url}/api/aggregations/raw?device_id={device_id}&'
235+
start_str = start.strftime('%Y-%m-%d %H:%M:%S').replace(' ', '%20')
236+
end_str = end.strftime('%Y-%m-%d %H:%M:%S').replace(' ', '%20')
237+
238+
url = (f'{self.base_url}/aggregations/raw?device_id={device_id}&'
235239
f'level={level}&'
236240
f'measure_kind={measure_kind}&'
237-
f'start={start}&'
238-
f'end={end}&'
241+
f'start={start_str}&'
242+
f'end={end_str}&'
239243
f'measure_type_id={measure_type_id}&'
240244
f'mm=')
241245

0 commit comments

Comments
 (0)