From c62669ddb5a3f2fca80c9ad90e7d492fb1e8e0b1 Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Thu, 20 Jan 2022 20:07:48 +0100 Subject: [PATCH 1/5] Move exceptions --- oligo/asyncio/asynciber.py | 23 +++-------------------- oligo/exception.py | 22 ++++++++++++++++++++++ oligo/iber.py | 20 +------------------- tests/test_iber.py | 2 +- 4 files changed, 27 insertions(+), 40 deletions(-) create mode 100644 oligo/exception.py diff --git a/oligo/asyncio/asynciber.py b/oligo/asyncio/asynciber.py index dc6205a..c8c3749 100644 --- a/oligo/asyncio/asynciber.py +++ b/oligo/asyncio/asynciber.py @@ -1,3 +1,6 @@ +from oligo.exception import SessionException, ResponseException, NoResponseException, LoginException, \ + SelectContractException + try: import aiohttp except ImportError: @@ -20,26 +23,6 @@ OBTENER_PERIODO_GENERACION_URL = "consumoNew/obtenerDatosGeneracionPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" -class ResponseException(Exception): - pass - - -class LoginException(Exception): - pass - - -class SessionException(Exception): - pass - - -class NoResponseException(Exception): - pass - - -class SelectContractException(Exception): - pass - - class AsyncIber: def __init__(self) -> None: """Iber class __init__ method.""" diff --git a/oligo/exception.py b/oligo/exception.py new file mode 100644 index 0000000..4ed6822 --- /dev/null +++ b/oligo/exception.py @@ -0,0 +1,22 @@ +class IberException(Exception): + pass + + +class ResponseException(IberException): + pass + + +class LoginException(IberException): + pass + + +class SessionException(IberException): + pass + + +class NoResponseException(IberException): + pass + + +class SelectContractException(IberException): + pass diff --git a/oligo/iber.py b/oligo/iber.py index 5948eb6..4f0c188 100644 --- a/oligo/iber.py +++ b/oligo/iber.py @@ -1,25 +1,7 @@ from requests import Session from datetime import datetime - -class ResponseException(Exception): - pass - - -class LoginException(Exception): - pass - - -class SessionException(Exception): - pass - - -class NoResponseException(Exception): - pass - - -class SelectContractException(Exception): - pass +from .exception import LoginException, ResponseException, NoResponseException, SelectContractException class Iber: diff --git a/tests/test_iber.py b/tests/test_iber.py index d079829..5b61517 100644 --- a/tests/test_iber.py +++ b/tests/test_iber.py @@ -4,7 +4,7 @@ from requests_mock import Adapter from oligo import Iber -from oligo.iber import LoginException, ResponseException +from oligo.exception import LoginException, ResponseException class TestIber(unittest.TestCase): From dad7922d90881b8a3ef555e68f70b623fb06a24d Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Thu, 20 Jan 2022 20:08:37 +0100 Subject: [PATCH 2/5] Fix SessionException --- oligo/iber.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oligo/iber.py b/oligo/iber.py index 4f0c188..c63a8cd 100644 --- a/oligo/iber.py +++ b/oligo/iber.py @@ -1,7 +1,7 @@ from requests import Session from datetime import datetime -from .exception import LoginException, ResponseException, NoResponseException, SelectContractException +from .exception import LoginException, ResponseException, NoResponseException, SelectContractException, SessionException class Iber: From 6d86b06d176b6ef47fbb675245d66b90cb522845 Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Thu, 20 Jan 2022 20:29:06 +0100 Subject: [PATCH 3/5] Test exceptions --- oligo/asyncio/asynciber.py | 8 +++----- oligo/exception.py | 9 ++++++--- oligo/iber.py | 31 ++++++++++++++++--------------- tests/test_exception.py | 24 ++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 tests/test_exception.py diff --git a/oligo/asyncio/asynciber.py b/oligo/asyncio/asynciber.py index c8c3749..ecd2d7c 100644 --- a/oligo/asyncio/asynciber.py +++ b/oligo/asyncio/asynciber.py @@ -35,9 +35,7 @@ async def __request( self, path: str, data: Optional[Union[list, dict]] = None ) -> dict: if not self.__session: - raise SessionException( - "Session required, use login() method to obtain a session" - ) + raise SessionException() if data is None: response = await self.__session.get( f"https://www.i-de.es/consumidores/rest/{path}", @@ -51,7 +49,7 @@ async def __request( ) if response.status != 200: self.__session = None - raise ResponseException("Response error, code: {}".format(response.status)) + raise ResponseException(response.status) data = await response.json() if not data: raise NoResponseException @@ -74,7 +72,7 @@ async def login(self, user: str, password: str) -> bool: data = await self.__request(LOGIN_URL, data=payload) if data["success"] != "true": self.__session = None - raise LoginException("Login error, bad login") + raise LoginException() return True async def measurement(self) -> dict: diff --git a/oligo/exception.py b/oligo/exception.py index 4ed6822..20fdf38 100644 --- a/oligo/exception.py +++ b/oligo/exception.py @@ -3,15 +3,18 @@ class IberException(Exception): class ResponseException(IberException): - pass + def __init__(self, status_code): + super().__init__("Response error, code: {}".format(status_code)) class LoginException(IberException): - pass + def __init__(self): + super().__init__('Login error, bad login') class SessionException(IberException): - pass + def __init__(self): + super().__init__('Session required, use login() method to obtain a session') class NoResponseException(IberException): diff --git a/oligo/iber.py b/oligo/iber.py index c63a8cd..ba6e9c0 100644 --- a/oligo/iber.py +++ b/oligo/iber.py @@ -1,6 +1,7 @@ -from requests import Session from datetime import datetime +from requests import Session + from .exception import LoginException, ResponseException, NoResponseException, SelectContractException, SessionException @@ -38,22 +39,22 @@ def login(self, user, password, session=Session()): response = self.__session.request("POST", self.__login_url, data=login_data, headers=self.__headers) if response.status_code != 200: self.__session = None - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) json_response = response.json() if json_response["success"] != "true": self.__session = None - raise LoginException("Login error, bad login") + raise LoginException() def __check_session(self): if not self.__session: - raise SessionException("Session required, use login() method to obtain a session") + raise SessionException() def measurement(self): """Returns a measurement from the powermeter.""" self.__check_session() response = self.__session.request("GET", self.__watthourmeter_url, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -73,7 +74,7 @@ def icpstatus(self): self.__check_session() response = self.__session.request("POST", self.__icp_status_url, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -86,7 +87,7 @@ def contracts(self): self.__check_session() response = self.__session.request("GET", self.__contracts_url, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -97,7 +98,7 @@ def contract(self): self.__check_session() response = self.__session.request("GET", self.__contract_detail_url, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException return response.json() @@ -106,7 +107,7 @@ def contractselect(self, id): self.__check_session() response = self.__session.request("GET", self.__contract_selection_url + id, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -117,7 +118,7 @@ def scene_list(self): self.__check_session() response = self.__session.request("GET", self.__obtener_escenarios_url, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -131,7 +132,7 @@ def scene_get(self, name): get_data = "{{\"nomEscenario\":\"{}\"}}".format(name) response = self.__session.request("POST", self.__obtener_escenario_url, data=get_data, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -149,7 +150,7 @@ def scene_save(self, consumption, measurement_id, description): save_data = "{{\"nomEscenario\":\"{}\",\"descripcion\":\"{}\"}}".format(name, description) response = self.__session.request("POST", self.__guardar_escenario_url.format(consumption, measurement_id), data=save_data, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException json_response = response.json() @@ -163,7 +164,7 @@ def scene_delete(self, name): delete_data = "{{\"nomEscenario\":\"{}\"}}".format(name) response = self.__session.request("POST", self.__borrar_escenario_url, data=delete_data, headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) return True def _consumption_raw(self, start, end): @@ -173,7 +174,7 @@ def _consumption_raw(self, start, end): response = self.__session.request("GET", self.__obtener_periodo_url.format(start_str, end_str), headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException return response.json() @@ -204,7 +205,7 @@ def _production_raw(self, start, end): response = self.__session.request("GET", self.__obtener_periodo_generacion_url.format(start_str, end_str), headers=self.__headers) if response.status_code != 200: - raise ResponseException("Response error, code: {}".format(response.status_code)) + raise ResponseException(response.status_code) if not response.text: raise NoResponseException return response.json() diff --git a/tests/test_exception.py b/tests/test_exception.py new file mode 100644 index 0000000..9c1cd41 --- /dev/null +++ b/tests/test_exception.py @@ -0,0 +1,24 @@ +import unittest + +from oligo.exception import SessionException, LoginException, ResponseException + + +class TestResponseException(unittest.TestCase): + + def test_message(self): + login_exception = ResponseException(418) + self.assertEqual('Response error, code: 418', login_exception.args[0]) + + +class TestLoginException(unittest.TestCase): + + def test_message(self): + login_exception = LoginException() + self.assertEqual('Login error, bad login', login_exception.args[0]) + + +class TestSessionException(unittest.TestCase): + + def test_message(self): + session_exception = SessionException() + self.assertEqual('Session required, use login() method to obtain a session', session_exception.args[0]) From f016ac017e5130abacde04e35d046858078f8dfd Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Thu, 20 Jan 2022 20:35:22 +0100 Subject: [PATCH 4/5] SonarCloud --- .github/workflows/test.yml | 5 +++++ sonar-project.properties | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 sonar-project.properties diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6dd43f2..648baa1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,3 +33,8 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with unittest run: python -m unittest + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..8391119 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,12 @@ +sonar.projectKey=hectorespert_python-oligo +sonar.organization=hectorespert + +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=python-oligo +#sonar.projectVersion=1.0 + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +#sonar.sources=. + +# Encoding of the source code. Default is default system encoding +#sonar.sourceEncoding=UTF-8 \ No newline at end of file From 20ffcc4db56a7813a178ea4fad30aacff2d35633 Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Thu, 20 Jan 2022 20:36:34 +0100 Subject: [PATCH 5/5] Revert sonar cloud config --- .github/workflows/test.yml | 5 ----- sonar-project.properties | 12 ------------ 2 files changed, 17 deletions(-) delete mode 100644 sonar-project.properties diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 648baa1..6dd43f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,8 +33,3 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with unittest run: python -m unittest - - name: SonarCloud Scan - uses: SonarSource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/sonar-project.properties b/sonar-project.properties deleted file mode 100644 index 8391119..0000000 --- a/sonar-project.properties +++ /dev/null @@ -1,12 +0,0 @@ -sonar.projectKey=hectorespert_python-oligo -sonar.organization=hectorespert - -# This is the name and version displayed in the SonarCloud UI. -#sonar.projectName=python-oligo -#sonar.projectVersion=1.0 - -# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. -#sonar.sources=. - -# Encoding of the source code. Default is default system encoding -#sonar.sourceEncoding=UTF-8 \ No newline at end of file