Skip to content

Commit

Permalink
refactor: Fix mypy 1.5.1 problems
Browse files Browse the repository at this point in the history
  • Loading branch information
bcyran committed Oct 2, 2023
1 parent 05f56c1 commit c5b7864
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/philipstv/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
14 changes: 8 additions & 6 deletions src/philipstv/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit c5b7864

Please sign in to comment.