Skip to content

Commit

Permalink
feat(method): add get_max_power
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Sep 19, 2021
1 parent 03570d6 commit b5e9133
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
23 changes: 22 additions & 1 deletion datadis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Literal
from datadis.types import (
ConsumptionData, ContractDetail, Supplie, dict_to_typed)
ConsumptionData, ContractDetail, MaxPower, Supplie, dict_to_typed)
import requests

_HOST = 'https://datadis.es'
Expand All @@ -9,6 +9,7 @@
'get_supplies': f'{_HOST}/api-private/api/get-supplies',
'get_contract_detail': f'{_HOST}/api-private/api/get-contract-detail',
'get_consumption_data': f'{_HOST}/api-private/api/get-consumption-data',
'get_max_power': f'{_HOST}/api-private/api/get-max-power',
}


Expand Down Expand Up @@ -105,3 +106,23 @@ def get_consumption_data(token: str, cups: str,
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


def get_max_power(token: str, cups: str,
distrubutor_code: str, start_date: str, end_date: str,
measurement_type: Literal[0, 1]
) -> List[MaxPower]:
headers = {'Authorization': f'Bearer {token}'}

r = requests.get(_ENDPOINTS['get_max_power']
+ f'?cups={cups}&distributorCode={distrubutor_code}'
+ f'&start_date={start_date}&end_date={end_date}',
headers=headers)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, MaxPower))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')
9 changes: 8 additions & 1 deletion datadis/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ class ConsumptionData(TypedDict):
obtainMethod: str


T = TypeVar('T', Supplie, ConsumptionData, ContractDetail)
class MaxPower(TypedDict):
cups: str
date: str
time: str
maxPower: float


T = TypeVar('T', Supplie, ConsumptionData, ContractDetail, MaxPower)


def dict_to_typed(data: Mapping[str, Any], typed: Type[T]) -> T:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_datadis.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def text(self):
"consumptionKWh": 0.194,
"obtainMethod": "Real"
}])
elif args[0].startswith(_ENDPOINTS['get_max_power']):
return MockResponse([{
"cups": "1234ABC",
"date": "2021/08/21",
"time": "13:30",
"maxPower": 3.788
}])
else:
return MockResponse([{"cups": "of rice"}], 'token_ok', 200)

Expand Down

0 comments on commit b5e9133

Please sign in to comment.