Skip to content

Commit

Permalink
feat: fix Get Device By ID (BREAKING) (#58)
Browse files Browse the repository at this point in the history
* feat: fix Get Device By ID
#pr

* Update example.py
  • Loading branch information
GuyKh committed Feb 28, 2024
1 parent bf671d9 commit 95e9889
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
14 changes: 8 additions & 6 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from iec_api.login import IECLoginError
from iec_api.models.exceptions import IECError

ROOT_DIR = os.path.dirname(
os.path.abspath(__file__)
)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config.fileConfig(ROOT_DIR + "/logging.conf", disable_existing_loggers=False)
logger = getLogger(__name__)

Expand Down Expand Up @@ -45,11 +43,15 @@
device = client.get_devices()[0]
print(device)

device_details = client.get_device_by_device_id(device.device_number)
print(device_details)

# Get Remote Readings from the last three days

selected_date: datetime = (datetime.now() - timedelta(days=30))
remote_readings = client.get_remote_reading(device.device_number, int(device.device_code), selected_date,
selected_date)
selected_date: datetime = datetime.now() - timedelta(days=30)
remote_readings = client.get_remote_reading(
device.device_number, int(device.device_code), selected_date, selected_date
)

if remote_readings:
print("Got " + str(len(remote_readings.data)) + " readings for " + selected_date.strftime("%Y-%m-%d"))
Expand Down
4 changes: 2 additions & 2 deletions iec_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
GET_DEFAULT_CONTRACT_URL = GET_CONTRACTS_URL + "?count=1"
GET_LAST_METER_READING_URL = IEC_API_BASE_URL + "Device/LastMeterReading/{contract_id}/{bp_number}"
AUTHENTICATE_URL = IEC_API_BASE_URL + "Authentication/{id}/1/-1?customErrorPage=true"
GET_DEVICES_URL = IEC_API_BASE_URL + "Device/{bp_number}"
GET_DEVICES_BY_CONTRACT_ID_URL = GET_DEVICES_URL + "/{contract_id}"
GET_DEVICES_URL = IEC_API_BASE_URL + "Device/{contract_id}"
GET_DEVICE_BY_DEVICE_ID_URL = GET_DEVICES_URL + "/{device_id}"
GET_DEVICE_TYPE_URL = IEC_API_BASE_URL + "Device/type/{bp_number}/{contract_id}/false"
GET_BILLING_INVOICES = IEC_API_BASE_URL + "billingCollection/invoices/{contract_id}/{bp_number}"
10 changes: 5 additions & 5 deletions iec_api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
GET_CONSUMER_URL,
GET_CONTRACTS_URL,
GET_DEFAULT_CONTRACT_URL,
GET_DEVICE_BY_DEVICE_ID_URL,
GET_DEVICE_TYPE_URL,
GET_DEVICES_BY_CONTRACT_ID_URL,
GET_DEVICES_URL,
GET_ELECTRIC_BILL_URL,
GET_LAST_METER_READING_URL,
Expand Down Expand Up @@ -167,11 +167,11 @@ def get_last_meter_reading(token: JWT, bp_number: str, contract_id: str) -> Mete
)


def get_devices(token: JWT, bp_number: str) -> list[Device]:
def get_devices(token: JWT, contract_id: str) -> list[Device]:
"""Get Device data response from IEC API."""
headers = add_jwt_to_headers(HEADERS_WITH_AUTH, token.id_token)
# sending get request and saving the response as response object
response = _get_url(url=GET_DEVICES_URL.format(bp_number=bp_number), headers=headers)
response = _get_url(url=GET_DEVICES_URL.format(contract_id=contract_id), headers=headers)

if response.status_code != 200:
if len(response.content) > 0:
Expand All @@ -184,10 +184,10 @@ def get_devices(token: JWT, bp_number: str) -> list[Device]:
return [Device.from_dict(device) for device in response.json()]


def get_devices_by_contract_id(token: JWT, bp_number: str, contract_id: str) -> Devices:
def get_device_by_device_id(token: JWT, contract_id: str, device_id: str) -> Devices:
"""Get Device data response from IEC API."""
return _get_response_with_descriptor(
token, GET_DEVICES_BY_CONTRACT_ID_URL.format(bp_number=bp_number, contract_id=contract_id), devices_decoder
token, GET_DEVICE_BY_DEVICE_ID_URL.format(device_id=device_id, contract_id=contract_id), devices_decoder
)


Expand Down
11 changes: 3 additions & 8 deletions iec_api/iec_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,24 @@ def get_devices(self, contract_id: Optional[str] = None) -> Optional[list[Device

return data.get_devices(self._token, contract_id)

def get_devices_by_contract_id(self, bp_number: Optional[str] = None, contract_id: Optional[str] = None) -> Devices:
def get_device_by_device_id(self, device_id: str, contract_id: Optional[str] = None) -> Devices:
"""
Get a list of devices for the user
Args:
self: The instance of the class.
bp_number (str): The BP number of the user.
device_id (str): The Device code.
contract_id (str): The Contract ID of the user.
Returns:
list[Device]: List of devices
"""
self.check_token()

if not bp_number:
bp_number = self._bp_number

assert bp_number, "BP number must be provided"

if not contract_id:
contract_id = self._contract_id

assert contract_id, "Contract ID must be provided"

return data.get_devices_by_contract_id(self._token, bp_number, contract_id)
return data.get_device_by_device_id(self._token, contract_id, device_id)

def get_remote_reading(
self,
Expand Down

0 comments on commit 95e9889

Please sign in to comment.