"""asyncio 包装(同一包内)。""" from __future__ import annotations import asyncio from typing import Any, Optional from .client import Client from .types import ( Body, RegisterOptions, RegisterResult, SendOptions, SendResult, Target, ) class AsyncClient: """把同步 Client 的阻塞调用丢到线程池。""" def __init__(self, client: Optional[Client] = None, **kwargs: Any) -> None: self._client = client or Client(**kwargs) @property def sync(self) -> Client: return self._client def on_session(self, handler): # noqa: ANN001 self._client.on_session(handler) def on_message(self, handler): # noqa: ANN001 self._client.on_message(handler) def on_receipt(self, handler): # noqa: ANN001 self._client.on_receipt(handler) def on_revoked(self, handler): # noqa: ANN001 self._client.on_revoked(handler) def on_presence(self, handler): # noqa: ANN001 self._client.on_presence(handler) def on_group_event(self, handler): # noqa: ANN001 self._client.on_group_event(handler) def on_connection(self, handler): # noqa: ANN001 self._client.on_connection(handler) async def connect(self, url: str, endpoint_id: str, **kwargs: Any) -> None: await asyncio.to_thread(self._client.connect, url, endpoint_id, **kwargs) async def close(self) -> None: await asyncio.to_thread(self._client.close) async def logout(self) -> None: await asyncio.to_thread(self._client.logout) async def send(self, to: Target, body: Body | str | bytes, options: Optional[SendOptions] = None) -> SendResult: return await asyncio.to_thread(self._client.send, to, body, options) async def ack(self, message) -> None: # noqa: ANN001 await asyncio.to_thread(self._client.ack, message) async def recall(self, message_id: str): return await asyncio.to_thread(self._client.recall, message_id) async def status(self, message_id: str, cursor: str = "", limit: int = 100): return await asyncio.to_thread(self._client.status, message_id, cursor, limit) async def unlock(self, endpoint_id: str, talk_password: str): return await asyncio.to_thread(self._client.unlock, endpoint_id, talk_password) async def presence(self, ids: list[str]): return await asyncio.to_thread(self._client.presence, ids) async def directory(self, cursor: str = "", query: str = "", limit: int = 100): return await asyncio.to_thread(self._client.directory, cursor, query, limit) async def watch_presence(self, ids: Optional[list[str]] = None, *, all: bool = False): return await asyncio.to_thread(self._client.watch_presence, ids, all=all) async def get_self(self): return await asyncio.to_thread(self._client.get_self) async def update_self(self, **kwargs: Any): return await asyncio.to_thread(self._client.update_self, **kwargs) async def set_talk_password(self, talk_password: str): return await asyncio.to_thread(self._client.set_talk_password, talk_password) async def change_login_password(self, old_password: str, new_password: str): return await asyncio.to_thread(self._client.change_login_password, old_password, new_password) async def group_create(self, name: str, members: list[dict[str, str]], group_id: str = ""): return await asyncio.to_thread(self._client.group_create, name, members, group_id) async def group_add(self, group_id: str, members: list[dict[str, str]]): return await asyncio.to_thread(self._client.group_add, group_id, members) async def group_remove(self, group_id: str, endpoint_id: str): return await asyncio.to_thread(self._client.group_remove, group_id, endpoint_id) async def group_leave(self, group_id: str): return await asyncio.to_thread(self._client.group_leave, group_id) async def group_transfer(self, group_id: str, endpoint_id: str): return await asyncio.to_thread(self._client.group_transfer, group_id, endpoint_id) async def group_rename(self, group_id: str, name: str): return await asyncio.to_thread(self._client.group_rename, group_id, name) async def group_dissolve(self, group_id: str): return await asyncio.to_thread(self._client.group_dissolve, group_id) async def group_list(self, cursor: str = "", limit: int = 100): return await asyncio.to_thread(self._client.group_list, cursor, limit) async def group_get(self, group_id: str, cursor: str = "", limit: int = 100): return await asyncio.to_thread(self._client.group_get, group_id, cursor, limit) @staticmethod async def register(url: str, registration_code: str, options: Optional[RegisterOptions] = None) -> RegisterResult: return await asyncio.to_thread(Client.register, url, registration_code, options)