This is a web socket wrapper for handling JSON messages in the 'handler' way
Project description
Extented Websockets
The library is based on Websockets
The observer-handlers approach is used, like in FastAPI and Aiogram it's allows you to quickly integrate web sockets into the project, and expand the list of requests-responses
Installation
pip install extented-ws
Usage
By default, the server accepts a JSON message of the following format:
{
"Type": "CalculateRequest",
"Data": {
"numbers": [2, 2]
}
}
After that, the server calls
trigger
from theobserver
, and that in turn calls allhandlers
whose first argument type name equalsType
in JSON The returned object, in turn, will be the answer for the client When theobserver
finds the desiredhandlers
, it calls it by passing arguments:
- Unpacked object from
Data
in JSON - The user received from the authorization
protocol
For known how
create_protocol
work see: Authentication, Factory
from extented_ws import WebsocketServer, telegram_protocol_factory
server = WebsocketServer()
class CalculateRequest(BaseModel):
numbers: List[int]
class CalculateResponse(BaseModel):
result: int
@server.message()
async def calculate_handler(request: CalculateRequest, user: WebAppUser): # user is optional argument
result = 0
for i in request.numbers:
result += i
return CalculateResponse(result=result)
async def main() -> None:
"""
telegram_protocol_factory used for create TelegramAuthProtocol instance with bot token
"""
await server.listen(create_protocol=telegram_protocol_factory(
token=TOKEN
))
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())
Also see EXAMPLE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for extented_ws-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c09a7e26012bf5f9104bebab4d4aa3ae317f53577bc2895c2946b945c0a9085 |
|
MD5 | de7d12e92400d4904875725efa6b7656 |
|
BLAKE2b-256 | 07d38e5b1773f57f80a01e5053c27ee33127b592a73eb98a7bb2e81cd8e6c26b |