From c5b78649d15f5ae4629ac4b62efec10bfb8ab4b3 Mon Sep 17 00:00:00 2001 From: Bazyli Cyran Date: Mon, 2 Oct 2023 19:48:46 +0200 Subject: [PATCH] refactor: Fix mypy 1.5.1 problems --- src/philipstv/api.py | 8 ++++---- src/philipstv/tv.py | 14 ++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/philipstv/api.py b/src/philipstv/api.py index 6b0bfea..f245ddc 100644 --- a/src/philipstv/api.py +++ b/src/philipstv/api.py @@ -36,12 +36,12 @@ @contextmanager -def _wrap_unauthorized_exceptions(path: str) -> Iterator[None]: +def _wrap_unauthorized_exceptions(method: str, path: str) -> Iterator[None]: try: yield except PhilipsTVError as exc: if exc.status_code == 401: - raise PhilipsTVAPIUnauthorizedError(exc.method, path) from exc + raise PhilipsTVAPIUnauthorizedError(method, path) from exc raise @@ -256,11 +256,11 @@ def _api_get_model(self, path: str, response_model: Type[_T]) -> _T: return response_model.parse(raw_response) def _api_post(self, path: str, payload: Optional[APIObject] = None) -> Any: - with _wrap_unauthorized_exceptions(path): + with _wrap_unauthorized_exceptions("POST", path): return self._tv.post(self._api_path(path), payload.dump() if payload else None) def _api_get(self, path: str) -> Any: - with _wrap_unauthorized_exceptions(path): + with _wrap_unauthorized_exceptions("GET", path): return self._tv.get(self._api_path(path)) def _api_path(self, path: str) -> str: diff --git a/src/philipstv/tv.py b/src/philipstv/tv.py index 0813936..a1a9e3c 100644 --- a/src/philipstv/tv.py +++ b/src/philipstv/tv.py @@ -18,12 +18,12 @@ @contextmanager -def _wrap_http_exceptions() -> Iterator[None]: +def _wrap_http_exceptions(method: str, url: str) -> Iterator[None]: try: yield except RequestException as exc: status_code = exc.response.status_code if exc.response is not None else None - raise PhilipsTVError(exc.request.method, exc.request.url, status_code) from exc + raise PhilipsTVError(method, url, status_code) from exc class PhilipsTV: @@ -78,8 +78,9 @@ def post(self, path: str, payload: Any = None) -> Any: """ _LOGGER.debug("Request: POST %s %s", path, payload) - with _wrap_http_exceptions(): - response = self._session.post(urljoin(self.url, path), json=payload) + url = urljoin(self.url, path) + with _wrap_http_exceptions("POST", url): + response = self._session.post(url, json=payload) response.raise_for_status() response_body = response.json() if response.content else None _LOGGER.debug("Response: %s %s", response.status_code, response_body) @@ -96,8 +97,9 @@ def get(self, path: str) -> Any: """ _LOGGER.debug("Request: GET %s", path) - with _wrap_http_exceptions(): - response = self._session.get(urljoin(self.url, path)) + url = urljoin(self.url, path) + with _wrap_http_exceptions("GET", url): + response = self._session.get(url) response.raise_for_status() response_body = response.json() if response.content else None _LOGGER.debug("Response: %s %s", response.status_code, response_body)