Skip to content

Commit

Permalink
Add exception class for auth errors
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Blyler <[email protected]>
  • Loading branch information
ablyler committed Oct 4, 2023
1 parent 2155173 commit 19aaaad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
22 changes: 12 additions & 10 deletions aquahawk_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def http_post_request(self, uri, headers, data):
"""
async with self._session.post(uri, headers=headers, data=data) as response:
response.raise_for_status()
return response
return await response.json()

async def get_usage(
self,
Expand Down Expand Up @@ -151,18 +151,20 @@ async def get_usage_this_year(self) -> Usage:

async def authenticate(self):
"""
Authenticates the client with the AquaHawk API.
Sends an HTTP POST request to the "/login" endpoint with the client's
username and password in the request body. If the request is
successful, the client's session will be authenticated and authorized
to make further requests.
Authenticates the client with the server.
Raises:
aiohttp.ClientResponseError: If the response status code is not in
the 200-299 range.
AuthenticationError: If authentication fails.
Returns:
None
"""
headers = {"accept": "application/json"}
payload = {"username": self.username, "password": self.password}
await self.http_post_request("/login", data=payload, headers=headers)
json = await self.http_post_request("/login", data=payload, headers=headers)
if json.get("success") is not True:
raise AuthenticationError("Authentication failed")


class AuthenticationError(Exception):
"""Raised when authentication fails."""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dataclasses-json = ">=0.6.1"
[tool.poetry.dev-dependencies]
pre-commit = "^2.15.0"
pytest = "^7.0.0"
pytest-asyncio = "^0.19.0"
pytest-aiohttp = "*"
pytest-cov = "^3.0.0"

[tool.pytest.ini_options]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_aquahawk_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import aiohttp
import pytest_asyncio
from src.client import AquaHawkClient
from src.types import Usage
from aquahawk_client.client import AquaHawkClient
from aquahawk_client.types import Usage


@pytest_asyncio.fixture
Expand Down

0 comments on commit 19aaaad

Please sign in to comment.