Fastws

Latest version: v0.1.7

Safety actively analyzes 623360 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

0.1.7

Improved exception handling
Improved exception-handling by now raising standard `WebSocketException` from `Starlette` with edited reason🛠️
This causes the connection to terminate immediately and should provide more friendly reasoning in browser logging.

Exceptions are caused by:
- unable to JSON-parse the incoming message.
- the JSON-parsed message not matching the `pydantic`-schema.
- unable to find a matching operation-type.
- the connection has timed out at an application level (e.g. due to a max lifespan restriction or minimum application-level heartbeat).

0.1.6

Changes

- Fixed an issue where send-operations defined on a `OperationRouter` did not return a reply. This was caused by the reply-operation not being passed inside the `OperationRouter`'s function `add_route(...)` inside `include_router(...)`.
- Added minimal test example application with test-suite 🛠️

0.1.5

✨ Updated to FastAPI to version 0.100.0 with **Pydantic v2** ✨

Changes

- Added support for reply messages without a payload. Messages without a payload only contain their message type.
- All messages are now uniquely identified by their operation name, this corresponds to the `Message ID` in the AsyncAPI-specification.
- `send` operations with a return annotation **must** now specify a `reply` in the decorator. This creates a new message in the AsyncAPI-specification. The `reply`-argument must therefore also be unique across operations.
- `recv` operations now implicitly sets the reply-operation.
- `send` operations with a `reply` defined now create a `x-response` in the AsyncAPI-documentation mapping to the reply message.

Example 1
Python
service = FastWS()


service.send("ping", reply="pong")
async def ping(client: Client, app: FastWS):
logging.info(f"{client=} | {app.connections=}")

The snippet above allows us to send and recieve WebSocket-messages with the following format:

json
// Send message
{
"type": "ping"
}
Recieve message
{
"type": "pong"
}


Example 2
Python
class Payload(BaseModel):
job_id: str


class Result(BaseModel):
name: str


service.send("work", reply="result")
async def send_work(payload: Payload) -> Result:
logging.info(f"{payload.job_id=}")
return Result(name="ok")

The snippet above allows us to send and recieve WebSocket-messages with the following format:

json
// Send message
{
"type": "work",
"payload": {
"job_id": "string"
}
}
Recieve message
{
"type": "result",
"payload": {
"name": "string"
}
}

Links

Releases

Has known vulnerabilities

© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.