from __future__ import annotations
from typing import Any
from ._base import BaseClient, RequestConfig
class AuthService:
def __init__(self, client: BaseClient) -> None:
self._client = client
async def login(
self,
username: str,
password: str,
remember_me: bool = False,
) -> dict[str, Any]:
data = {
"username": username,
"password": password,
"remember_me": remember_me,
}
config = RequestConfig(json_data=data)
response = await self._client.make_request("POST", "/auth/login", config=config)
if "access_token" in response:
self._client.set_access_token(response["access_token"])
return response
async def logout(self) -> dict[str, Any]:
response = await self._client.make_request("POST", "/auth/logout")
self._client.clear_access_token()
return response
async def refresh_token(self, refresh_token: str) -> dict[str, Any]:
data = {"refresh_token": refresh_token}
config = RequestConfig(json_data=data)
response = await self._client.make_request(
"POST", "/auth/refresh", config=config
)
if "access_token" in response:
self._client.set_access_token(response["access_token"])
return response
async def register(
self,
username: str,
email: str,
password: str,
user_data: dict[str, Any] | None = None,
) -> dict[str, Any]:
data = {
"username": username,
"email": email,
"password": password,
}
if user_data:
data.update(user_data)
config = RequestConfig(json_data=data)
return await self._client.make_request("POST", "/auth/register", config=config)
async def verify_email(self, token: str) -> dict[str, Any]:
data = {"token": token}
config = RequestConfig(json_data=data)
return await self._client.make_request(
"POST", "/auth/verify-email", config=config
)
async def reset_password_request(self, email: str) -> dict[str, Any]:
data = {"email": email}
config = RequestConfig(json_data=data)
return await self._client.make_request(
"POST", "/auth/reset-password", config=config
)
async def reset_password_confirm(
self,
token: str,
new_password: str,
) -> dict[str, Any]:
data = {
"token": token,
"new_password": new_password,
}
config = RequestConfig(json_data=data)
return await self._client.make_request(
"POST", "/auth/reset-password/confirm", config=config
)
async def change_password(
self,
current_password: str,
new_password: str,
) -> dict[str, Any]:
data = {
"current_password": current_password,
"new_password": new_password,
}
config = RequestConfig(json_data=data)
return await self._client.make_request(
"POST", "/auth/change-password", config=config
)
async def validate_token(self, token: str | None = None) -> dict[str, Any]:
if token:
original_token = self._client.get_access_token()
self._client.set_access_token(token)
try:
response = await self._client.make_request("GET", "/auth/validate")
return response
finally:
if original_token:
self._client.set_access_token(original_token)
else:
self._client.clear_access_token()
return await self._client.make_request("GET", "/auth/validate")