File size: 2,171 Bytes
1e7e130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import httpx
from typing import Optional
from .Schema import (
    RegisterUserRequest,
    LoginRequest,
    SetUserStatusRequest,
    APIResponse,
)
from App.Portals.PortalRoutes import ANDROID
from App.Portals.Model import Portal


class AndroidClient:
    def __init__(self):
        self.base_url = "http://127.0.0.1:8080/"
        self.client = httpx.AsyncClient(base_url=self.base_url)

    async def register_user(self, request: RegisterUserRequest) -> APIResponse:
        """Register a new user."""
        response = await self.client.post("/users/register", json=request.dict())
        return APIResponse(**response.json())

    async def login_user(self, request: LoginRequest) -> APIResponse:
        """Login an existing user."""
        response = await self.client.post("/login", json=request.dict())
        return APIResponse(**response.json())

    async def logout_user(self, phone: str) -> APIResponse:
        """Logout a user by phone."""
        response = await self.client.post(f"/logout/{phone}")
        return APIResponse(**response.json())

    async def get_active_users(self) -> APIResponse:
        """Retrieve all active users."""
        response = await self.client.get("/users/active")
        print(response.json())
        return APIResponse(**response.json())

    async def set_user_status(self, request: SetUserStatusRequest) -> APIResponse:
        """Enable or disable a user."""
        response = await self.client.patch("/user/status", json=request.dict())
        return APIResponse(**response.json())

    async def get_users_list(self) -> APIResponse:
        """Retrieve all users (active and inactive)."""
        response = await self.client.get("/users")
        return APIResponse(**response.json())

    async def get_user_stats(self, phone: Optional[str] = None) -> APIResponse:
        """Retrieve user statistics for a specific user or all users."""
        url = f"/user/stats/{phone}" if phone else "/user/stats"
        response = await self.client.get(url)
        return APIResponse(**response.json())

    async def close(self):
        """Close the client session."""
        await self.client.aclose()