Skip to content

Commit

Permalink
feat: login for user
Browse files Browse the repository at this point in the history
  • Loading branch information
New-dev0 committed Aug 24, 2024
1 parent 9c58d9f commit abeaaf0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
8 changes: 7 additions & 1 deletion swibots/api/auth/auth_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Type, TypeVar
import swibots
from swibots.api.auth.models import AuthUser
from swibots.api.auth.models import AuthUser, AuthResult
from swibots.base import SwitchRestClient
from swibots.config import get_config

Expand Down Expand Up @@ -47,3 +47,9 @@ async def get_me(self, user_type: Type[T] = AuthUser) -> T:
"""
response = await self.get("/api/user")
return self.build_object(user_type, response.data)

def login(self, email: str, password: str):
response = self.sync_post(
"/api/login", data={"email": email, "password": password}
)
return self.build_object(AuthResult, response.data)
3 changes: 2 additions & 1 deletion swibots/api/auth/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .auth_user import AuthUser
from .auth_result import AuthResult

__all__ = ["AuthUser"]
__all__ = ["AuthUser", "AuthResult"]
32 changes: 32 additions & 0 deletions swibots/api/auth/models/auth_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Optional, List
from swibots.base.switch_object import SwitchObject
from swibots.utils.types import JSONDict


class AuthResult(SwitchObject):
def __init__(
self,
access_token: Optional[str] = None,
refresh_token: Optional[str] = None,
user_id: Optional[int] = None,
is_bot: Optional[bool] = False,
roles: Optional[List[str]] = None,
active: Optional[bool] = False
):
self.access_token = access_token
self.refresh_token = refresh_token
self.user_id = user_id
self.is_bot = is_bot
self.roles = roles
self.active = active

def from_json(self, data: Optional[JSONDict]) -> "AuthResult":
super().from_json(data)
if data is not None:
self.access_token = data.get("access_token")
self.refresh_token = data.get("refresh_token")
self.user_id = data.get("user_id")
self.is_bot = data.get("is_bot")
self.roles = data.get("roles")
self.active = data.get("active")
return self

0 comments on commit abeaaf0

Please sign in to comment.