Skip to main content

This is a web socket wrapper for handling JSON messages in the 'handler' way

Project description

Extented Websockets

pipeline status

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 the observer, and that in turn calls all handlers whose first argument type name equals Type in JSON The returned object, in turn, will be the answer for the client When the observer finds the desired handlers, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extented_ws-0.1.4.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

extented_ws-0.1.4-py3-none-any.whl (8.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page