File size: 5,208 Bytes
1e7e130 7875b25 1e7e130 7875b25 1e7e130 7875b25 1e7e130 7875b25 1e7e130 7875b25 1e7e130 7875b25 1e7e130 6a3e1a1 3c7058e 4e4e828 e23c4c3 6a3e1a1 7875b25 1e7e130 7875b25 6a3e1a1 1e7e130 6a3e1a1 33ed18b 1e7e130 7875b25 1e7e130 7875b25 1e7e130 7875b25 5236daa 7875b25 5236daa 7875b25 4e4e828 69d4494 4e4e828 7875b25 1e7e130 768269f 33ed18b 768269f 6a3e1a1 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
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 is_user_active(self, phone_number):
"""Retrieve all active users."""
response = await self.client.get("/users/active")
try:
data = response.json()
except:
return None
if "active_users" in data:
active_users = data["active_users"]
for user in active_users:
if phone_number == user["phone"]:
return True
@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 remove_session(self, phone_number) -> APIResponse:
"""Enable or disable a user."""
path = f"/users/{phone_number}/remove-session"
response = await self.client.post(path, json=None)
return APIResponse(**response.json())
@require_base_url
async def disable_user(self, request: SetUserStatusRequest) -> APIResponse:
"""disable a user."""
path = f"/users/{request.phone}/disable"
response = await self.client.post(path, json=None)
return APIResponse(**response.json())
@require_base_url
async def enable_user(self, request: SetUserStatusRequest) -> APIResponse:
"""Enable or disable a user."""
path = f"/users/{request.phone}/enable"
response = await self.client.post(path, json=None)
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,
}
print(data)
response = await self.client.post("/send_message", json=data)
x = response.content
print(x)
if response.status_code < 200 or response.status_code >= 300:
return response
else:
raise Exception(f"Error sending message : {x}")
async def close(self):
"""Close the client session."""
await self.client.aclose()
async def activate_user(self, phone_number: str):
request = SetUserStatusRequest(phone=phone_number, disabled=False)
await self.enable_user(request=request)
# Replace this with actual API call logic
return {"status": "success", "message": "User activated."}
async def deactivate_user(self, phone_number: str):
request = SetUserStatusRequest(phone=phone_number, disabled=False)
await self.disable_user(request=request)
# Replace this with actual API call logic
return {"status": "success", "message": "User activated."}
|