Skip to content

Commit

Permalink
feat: add support for authorized nif
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Oct 12, 2021
1 parent ee7e4fe commit 2cb5157
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
26 changes: 20 additions & 6 deletions datadis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,37 @@ async def get_token(username: str, password: str) -> str:
raise ConnectionError(f'Error: {r.json()["message"]}')


async def get_supplies(token: str) -> List[Supplie]:
async def get_supplies(
token: str, authorized_nif: str = None
) -> List[Supplie]:
"""Search all the supplies
Args:
token (str): Bearer token
authorized_nif (str): NIF of the authorized people. Optional.
Raises:
Exception: If the authentication fails
Returns:
dict: A dictionary with the supplies
"""
return await _request(_ENDPOINTS["get_supplies"], token, None, Supplie)
params = None
if authorized_nif:
params = {"authorizedNif": authorized_nif}
return await _request(_ENDPOINTS["get_supplies"], token, params, Supplie)


async def get_contract_detail(
token: str, cups: str, distrubutor_code: int
token: str, cups: str, distrubutor_code: int, authorized_nif: str = None
) -> List[ContractDetail]:
"""Search the contract detail
Args:
token (str): Bearer token
cups (str): Cups code. Get it from get_supplies
distrubutor_code (int): Distributor code. Get it from get_supplies
authorized_nif (str): NIF of the authorized people. Optional.
Raises:
Exception: [description]
Expand All @@ -74,7 +81,8 @@ async def get_contract_detail(
"""

params = {"cups": cups, "distributorCode": distrubutor_code}

if authorized_nif:
params["authorizedNif"] = authorized_nif
return await _request(
_ENDPOINTS["get_contract_detail"], token, params, ContractDetail
)
Expand All @@ -88,6 +96,7 @@ async def get_consumption_data(
end_date: str,
measurement_type: Literal[0, 1],
point_type: int,
authorized_nif: str = None,
) -> List[ConsumptionData]:
"""Search the consumption data
Expand All @@ -99,6 +108,7 @@ async def get_consumption_data(
measurement_type (str): 0 -> Hourly, 1 -> quarter hourly
pointType (str): Point type code, get it from get-supplies
distrubutor_code (int): Distributor code. Get it from get_supplies
authorized_nif (str): NIF of the authorized people. Optional.
Raises:
Exception: [description]
Expand All @@ -114,7 +124,8 @@ async def get_consumption_data(
"measurementType": measurement_type,
"pointType": point_type,
}

if authorized_nif:
params["authorizedNif"] = authorized_nif
return await _request(
_ENDPOINTS["get_consumption_data"], token, params, ConsumptionData
)
Expand All @@ -126,6 +137,7 @@ async def get_max_power(
distrubutor_code: int,
start_date: str,
end_date: str,
authorized_nif: str = None,
) -> List[MaxPower]:
"""Search the maximum power and the result will appear in kW
Expand All @@ -135,6 +147,7 @@ async def get_max_power(
start_date (str): start date beetween search data. Format: YYYY/MM
end_date (str): end date beetween search data. Format: YYYY/MM
distrubutor_code (int): Distributor code. Get it from get_supplies
authorized_nif (str): NIF of the authorized people. Optional.
Raises:
Exception: [description]
Expand All @@ -148,7 +161,8 @@ async def get_max_power(
"startDate": start_date,
"endDate": end_date,
}

if authorized_nif:
params["authorizedNif"] = authorized_nif
return await _request(_ENDPOINTS["get_max_power"], token, params, MaxPower)


Expand Down
15 changes: 15 additions & 0 deletions tests/test_datadis.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async def test_get_token(mock_post: mock.MagicMock):

mock_post.assert_called_once()
assert token == TOKEN
assert "params" not in mock_post.call_args[1]


@pytest.mark.asyncio
Expand All @@ -69,13 +70,25 @@ async def test_get_supplies(mock_get: mock.MagicMock):
assert supplies[0]["address"] == SUPPLIES_RESPONSE[0]["address"]


@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.get", side_effect=mock_requests)
async def test_get_supplies_authorized(mock_get: mock.MagicMock):
supplies = await get_supplies("token", "123456789A")

mock_get.assert_called_once()
assert len(supplies) == 1
assert supplies[0]["address"] == SUPPLIES_RESPONSE[0]["address"]
assert mock_get.call_args[1]["params"]["authorizedNif"] == "123456789A"


@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.get", side_effect=mock_requests)
async def test_get_contract_detail(mock_get: mock.MagicMock):
contract_detail = await get_contract_detail("token", "cupaso", 2)

mock_get.assert_called_once()
assert contract_detail is not None
assert "authorizedNif" not in mock_get.call_args[1]["params"]


@pytest.mark.asyncio
Expand All @@ -87,6 +100,7 @@ async def test_get_consumption_data(mock_get: mock.MagicMock):

mock_get.assert_called_once()
assert contract_detail is not None
assert "authorizedNif" not in mock_get.call_args[1]["params"]


@pytest.mark.asyncio
Expand All @@ -98,3 +112,4 @@ async def test_get_max_power(mock_get: mock.MagicMock):

mock_get.assert_called_once()
assert contract_detail is not None
assert "authorizedNif" not in mock_get.call_args[1]["params"]

0 comments on commit 2cb5157

Please sign in to comment.