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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file extented_ws-0.1.4.tar.gz.

File metadata

  • Download URL: extented_ws-0.1.4.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for extented_ws-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a02adc023d0d940493709869c89186c6e9270a198ce372c9a12bf4b8755aadb4
MD5 ee53ed153fad4155c4711caa82878950
BLAKE2b-256 35e0c7892d11794096ee2cdccfb8d3dcc2a89111f399c17ee7cd3c58f54eb167

See more details on using hashes here.

File details

Details for the file extented_ws-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: extented_ws-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for extented_ws-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1c09a7e26012bf5f9104bebab4d4aa3ae317f53577bc2895c2946b945c0a9085
MD5 de7d12e92400d4904875725efa6b7656
BLAKE2b-256 07d38e5b1773f57f80a01e5053c27ee33127b592a73eb98a7bb2e81cd8e6c26b

See more details on using hashes here.

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