Skip to content

Commit

Permalink
feat(method): add get_consumption_data
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Sep 19, 2021
1 parent 4cdd844 commit 048783c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
26 changes: 24 additions & 2 deletions datadis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import List
from datadis.types import ContractDetail, Supplie, dict_to_typed
from typing import List, Literal
from datadis.types import (
ConsumptionData, ContractDetail, Supplie, dict_to_typed)
import requests

_HOST = 'https://datadis.es'
_ENDPOINTS = {
'get_token': f'{_HOST}/nikola-auth/tokens/login',
'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',
}


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


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

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

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


class ConsumptionData(TypedDict):
cups: str
date: str
time: str
consumptionKWh: float
obtainMethod: str


def dict_to_typed(data: Mapping[str, Any], typed: TypedDict) -> TypedDict:
result = typed()
for key, key_type in typed.__annotations__.items():
for key, _ in typed.__annotations__.items():
if key not in data:
raise ValueError(f"Key: {key} is not available in data.")
result[key] = data[key]
Expand Down
21 changes: 19 additions & 2 deletions tests/test_datadis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datadis import get_token, get_supplies, get_contract_detail, _ENDPOINTS
from datadis import (get_consumption_data, get_token,
get_supplies, get_contract_detail, _ENDPOINTS)
from unittest import mock


Expand Down Expand Up @@ -32,7 +33,7 @@ def text(self):
elif args[0].startswith(_ENDPOINTS['get_contract_detail']):
return MockResponse([{
"address": "home",
"cups": "1234ABC",
"cups": "c",
"postalCode": "1024",
"province": "madrid",
"municipality": "madrid",
Expand All @@ -49,6 +50,14 @@ def text(self):
"startDate": "2020/09",
"endDate": "2020/09",
}], status_code=200)
elif args[0].startswith(_ENDPOINTS['get_consumption_data']):
return MockResponse([{
"cups": "1234ABC",
"date": "2021/08/01",
"time": "01:00",
"consumptionKWh": 0.194,
"obtainMethod": "Real"
}])
else:
return MockResponse([{"cups": "of rice"}], 'token_ok', 200)

Expand All @@ -73,3 +82,11 @@ def test_get_contract_detail(mock_get: mock.MagicMock):
contract_detail = get_contract_detail("token", "cupaso", 2)

assert contract_detail is not None


@mock.patch('requests.get', side_effect=mock_requests)
def test_get_consumption_data(mock_get: mock.MagicMock):
contract_detail = get_consumption_data(
"token", "cupaso", '2', '2021/08/01', '2021/08/31', 0)

assert contract_detail is not None

0 comments on commit 048783c

Please sign in to comment.