From 2cb515739b548c0451aba268c9ecbfc9d78f8938 Mon Sep 17 00:00:00 2001 From: Alvaro Tinoco Date: Tue, 12 Oct 2021 10:34:22 +0000 Subject: [PATCH] feat: add support for authorized nif --- datadis/__init__.py | 26 ++++++++++++++++++++------ tests/test_datadis.py | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/datadis/__init__.py b/datadis/__init__.py index 0ab6043..259eec5 100644 --- a/datadis/__init__.py +++ b/datadis/__init__.py @@ -41,11 +41,14 @@ 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 @@ -53,11 +56,14 @@ async def get_supplies(token: str) -> List[Supplie]: 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 @@ -65,6 +71,7 @@ async def get_contract_detail( 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] @@ -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 ) @@ -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 @@ -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] @@ -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 ) @@ -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 @@ -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] @@ -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) diff --git a/tests/test_datadis.py b/tests/test_datadis.py index 9bd167b..388a40e 100644 --- a/tests/test_datadis.py +++ b/tests/test_datadis.py @@ -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 @@ -69,6 +70,17 @@ 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): @@ -76,6 +88,7 @@ async def test_get_contract_detail(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 @@ -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 @@ -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"]