Metadata-Version: 2.1
Name: extented-ws
Version: 0.1.4
Summary: This is a web socket wrapper for handling JSON messages in the 'handler' way
Author-email: Sergei Ivanov <dexsperpro@gmail.com>
License: MIT License
        
        Copyright (c) 2023 Sergei
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://gitlab.com/ts-workflow/template/tbot-ts-example-sockets-webapp
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.11
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: websockets >=11.0.3
Requires-Dist: aiogram >=3.1.1

# Extented Websockets

[![pipeline status](https://gitlab.com/ts-workflow/template/tbot-ts-example-sockets-webapp/badges/main/pipeline.svg?ignore_skipped=true)](https://gitlab.com/ts-workflow/template/tbot-ts-example-sockets-webapp/-/commits/main)

The library is based on [Websockets](https://github.com/python-websockets/websockets/tree/main)<br/>
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
```sh
pip install extented-ws
```

## Usage
By default, the server accepts a **JSON** message of the following format:
```json
{
  "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](https://websockets.readthedocs.io/en/stable/topics/authentication.html#query-parameter), [Factory](https://websockets.readthedocs.io/en/stable/faq/common.html#how-can-i-pass-arguments-to-a-custom-protocol-subclass)

```python
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](https://gitlab.com/ts-workflow/template/tbot-ts-example-sockets-webapp/-/tree/main/example?ref_type=heads)
