Skip to content

Commit

Permalink
Use raise_for_status instead of raising errors manually
Browse files Browse the repository at this point in the history
  • Loading branch information
frwickst committed Jun 8, 2022
1 parent 52c5f72 commit fd891cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,13 @@ huum.turn_off()

This library uses Pydantic schemas for all method responses.
I recommend checking the /huum/schemas.py for checking responses.

## Handling exceptions

All exceptions that are raised from making an HTTP request are coming directly from `aiohttp`.
More of the time these will be `aiohtto.ClientResponseError` as all requests are done with `raise_for_status` set
to `True` for requests that goes through `aiohttp`.

If the door is open and the sauna is turned on, and the client is not told to explicitly bypass security
measures (see "Security concerns" above), then `huum.exceptions.SafetyException` will be raised.

33 changes: 9 additions & 24 deletions huum/huum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Optional
from urllib.parse import urljoin

import aiohttp
Expand Down Expand Up @@ -48,23 +48,6 @@ def __init__(
)
self.auth = aiohttp.BasicAuth(username, password)

async def handle_response(self, response: aiohttp.ClientResponse) -> Any:
"""
Handle a response and raise errors if status code is not 200
Args:
response: Response from the Huum API
Returns:
A response object
"""
if response.status != 200:
response_text = await response.text()
raise aiohttp.ClientError(
f"Request failed with status code {response.status}. {response_text}"
)
return await response.json()

async def _check_door(self) -> None:
"""
Check if the door is closed, if not, raise an exception
Expand Down Expand Up @@ -97,8 +80,10 @@ async def turn_on(
url = urljoin(API_BASE, "start")
data = {"targetTemperature": temperature}

response = await self.session.post(url, json=data, auth=self.auth)
json_data = await self.handle_response(response)
response = await self.session.post(
url, json=data, auth=self.auth, raise_for_status=True
)
json_data = await response.json()

return HuumStatusResponse(**json_data)

Expand All @@ -112,8 +97,8 @@ async def turn_off(self) -> HuumStatusResponse:
"""
url = urljoin(API_BASE, "stop")

response = await self.session.post(url, auth=self.auth)
json_data = await self.handle_response(response)
response = await self.session.post(url, auth=self.auth, raise_for_status=True)
json_data = await response.json()

return HuumStatusResponse(**json_data)

Expand Down Expand Up @@ -145,8 +130,8 @@ async def status(self) -> HuumStatusResponse:
"""
url = urljoin(API_BASE, "status")

response = await self.session.get(url, auth=self.auth)
json_data = await self.handle_response(response)
response = await self.session.get(url, auth=self.auth, raise_for_status=True)
json_data = await response.json()

return HuumStatusResponse(**json_data)

Expand Down
13 changes: 0 additions & 13 deletions tests/test_huum.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,3 @@ async def test_set_temperature_turn_on(mock_request: Any) -> None:
result_set_temperature = await huum.set_temperature(temperature=80)

assert result_turn_on == result_set_temperature


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_bad_response_code(mock_request: Any) -> None:
test_response_text = "__test string__"
mock_request.return_value = MockResponse({}, 400, test_response_text)

huum = Huum("test", "test")
await huum.open_session()
with pytest.raises(aiohttp.ClientError) as exception:
await huum.status()
assert test_response_text in str(exception.value)

0 comments on commit fd891cb

Please sign in to comment.