Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lonode/fastwapi
FastWAPI is a simple lightweight Websocket framework based on Starlette, which provide easy-to-use Python decorator to parse JSON incoming message.
https://github.com/lonode/fastwapi
api fastapi json pydantic starlette websocket
Last synced: about 2 months ago
JSON representation
FastWAPI is a simple lightweight Websocket framework based on Starlette, which provide easy-to-use Python decorator to parse JSON incoming message.
- Host: GitHub
- URL: https://github.com/lonode/fastwapi
- Owner: lonode
- License: mit
- Created: 2023-06-15T09:25:39.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-06-15T11:34:58.000Z (over 1 year ago)
- Last Synced: 2024-11-14T11:08:10.137Z (about 2 months ago)
- Topics: api, fastapi, json, pydantic, starlette, websocket
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Description
FastWAPI is a simple lightweight Websocket framework based on Starlette, which provide easy-to-use Python decorator to parse JSON incoming message.
It's in the same spirit as _FastAPI_, where each decorator map to an HTTP path. But here, each decorator map to a Pydantic object, and each object map to a function.
# Installation
`pip install fastwapi`
# Disclaimer- The module is available, but far from finished & polished, please do not use it in production.
# Roadmap
- Add authent middleware
- Add background job to asynchrounosly send JSON to the client# Usage
See example/main.py for complete code.
Getting started in three steps :
#### Instanciates the framework and declare the Websocket HTTP endpoint.
```python
import uvicorn
from fastwapi import FastWAPI, WebSocket
from pydantic import BaseModelapp = FastWAPI(endpoint="/ws")
```
#### Define Pyndantic model, for incoming and outgoing messages.
```python
class CM(BaseModel):
counter: intclass SD(BaseModel):
name: str
```#### Map each Pydantic model to your function.
```python
@app.parse(CM)
async def parse_CM(websocket: WebSocket, data: CM):
print("RECEIVED CM : ", data)
await websocket.send_json(data.dict())@app.parse(SD)
async def parse_SD(websocket: WebSocket, data: CM):
print("RECEIVED SD : ", data)
await websocket.send_json(data.dict())
```## Launch your app
Either through command line "uvicorn main:app" or directly inside the python file :
```python
if __name__ == "__main__":
uvicorn.run("main:app", port=5000, log_level="info")
```To test your app, you need a websocket client. You can use Postman, or [wscat](https://github.com/websockets/wscat) :
![wscat demo](https://github.com/lonode/FastWAPI/assets/32384862/ebc1f354-b673-48e7-b85e-881a343e35a2)