Skip to content

Commit

Permalink
🐛 Refactor ComwattClient API endpoints and improve date formatting
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ZoomeoTooknor authored and MateoGreil committed Jan 27, 2025
1 parent 210472a commit d84e2c0
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions comwatt_client_legacy/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import hashlib
import json
import requests
from datetime import datetime

Expand Down Expand Up @@ -83,20 +82,20 @@ def get_owner_details(self, owner_id):
"""

url = f'{self.base_url}/api/indepboxes?ownerid={owner_id}'
url = f'{self.base_url}/indepboxes?ownerid={owner_id}'

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

def get_box_details(self, box_id):
def get_box_details(self, macAddress):
"""
Retrieves information about the box's details.
Args:
box_id (str): The ID of the box.
macAddress (str): The ID of the box.
Returns:
dict: Information about the box.
Expand All @@ -106,8 +105,8 @@ def get_box_details(self, box_id):
"""

url = f'{self.base_url}/api/indepboxes/byMacAddress/{box_id}'

url = f'{self.base_url}/indepboxes/byMacAddress/{macAddress}'
response = self.session.get(url)
if response.status_code == 200:
return response.json()
Expand All @@ -129,7 +128,7 @@ def get_products(self):
"""

url = f'{self.base_url}/api/products/1'
url = f'{self.base_url}/products/'

response = self.session.get(url)
if response.status_code == 200:
Expand All @@ -152,7 +151,7 @@ def get_devices(self, indepbox_id):
"""

url = f'{self.base_url}/api/devices?indepbox_id={indepbox_id}'
url = f'{self.base_url}/devices?indepbox_id={indepbox_id}'

response = self.session.get(url)
if response.status_code == 200:
Expand Down Expand Up @@ -193,21 +192,23 @@ def get_networkstats(self, indepbox_id,
"""

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

url = (f'{self.base_url}/aggregations/networkstats?indepbox_id={indepbox_id}&'
f'level={level}&'
f'measure_kind={measure_kind}&'
f'start={start_str}&'
f'end={end_str}')

response = self.session.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(f'Error retrieving networkstats: {response.status_code}')

def get_devices_stats(self, device_id,
measure_kind="VIRTUAL_QUANTITY",
measure_kind="QUANTITY",
measure_type_id="1",
level="HOUR",
start=datetime.now(),
Expand All @@ -231,11 +232,14 @@ def get_devices_stats(self, device_id,
Exception: If an error occurs while retrieving the devices_stats.
"""

url = (f'{self.base_url}/api/aggregations/raw?device_id={device_id}&'
start_str = start.strftime('%Y-%m-%d %H:%M:%S').replace(' ', '%20')
end_str = end.strftime('%Y-%m-%d %H:%M:%S').replace(' ', '%20')

url = (f'{self.base_url}/aggregations/raw?device_id={device_id}&'
f'level={level}&'
f'measure_kind={measure_kind}&'
f'start={start}&'
f'end={end}&'
f'start={start_str}&'
f'end={end_str}&'
f'measure_type_id={measure_type_id}&'
f'mm=')

Expand Down

0 comments on commit d84e2c0

Please sign in to comment.