hotspot / App /Android /Android.py
Mbonea's picture
Working registration, testing payments
7875b25
raw
history blame
3.37 kB
import httpx
from typing import Optional, Union, List
from functools import wraps
from .Schema import (
RegisterUserRequest,
LoginRequest,
SetUserStatusRequest,
APIResponse,
)
from App.Portals.PortalRoutes import ANDROID
from App.Portals.Model import Portal
def require_base_url(func):
"""Decorator to set base_url before making a request."""
@wraps(func)
async def wrapper(self, *args, **kwargs):
await self.set_base_url()
return await func(self, *args, **kwargs)
return wrapper
class AndroidClient:
def __init__(self):
self.client = httpx.AsyncClient()
async def set_base_url(self):
"""Fetch the base_url from the database."""
portal = await Portal.get(name=ANDROID)
self.client.base_url = portal.url
@require_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())
@require_base_url
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())
@require_base_url
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())
@require_base_url
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())
@require_base_url
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())
@require_base_url
async def get_users_list(self) -> APIResponse:
"""Retrieve all users (active and inactive)."""
response = await self.client.get("/users")
return APIResponse(**response.json())
@require_base_url
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())
@require_base_url
async def send_message(
self,
message: str,
phone_numbers: Union[str, List[str]],
event: str = "string",
) -> APIResponse:
"""Send a message to a single phone number or a list of phone numbers."""
# Ensure phone_numbers is a list
if isinstance(phone_numbers, str):
phone_numbers = [phone_numbers]
data = {
"message": message,
"phoneNumbers": phone_numbers,
"event": event,
}
response = await self.client.post("/send_message", json=data)
print(response)
return response
async def close(self):
"""Close the client session."""
await self.client.aclose()