Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cerebrusinc/pyzeus
The cors middleware that enables a FastAPI server to handle cors requests, specifically, on the router and individual route level. It also handles preflight requests :)
https://github.com/cerebrusinc/pyzeus
Last synced: 15 days ago
JSON representation
The cors middleware that enables a FastAPI server to handle cors requests, specifically, on the router and individual route level. It also handles preflight requests :)
- Host: GitHub
- URL: https://github.com/cerebrusinc/pyzeus
- Owner: cerebrusinc
- License: other
- Created: 2024-01-10T22:54:20.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-01-13T19:58:51.000Z (11 months ago)
- Last Synced: 2024-12-01T20:46:04.878Z (21 days ago)
- Language: Python
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyzeus ⚡
The cors middleware that enables a [FastAPI](https://fastapi.tiangolo.com) server to handle cors requests. It also handles preflight requests 😃.
## Installation
```py
pip install pyzeus
```or
```py
pip3 install pyzeus
```## Default Response Headers
If no options are provided, the response headers will be as follows:
```txt
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Headers: Content-Type,Origin
Access-Control-Max-Age: 5```
#### NOTES
1. The allow_headers will always append `Content-Type` and `Origin` to your response headers so no need to add it to the list
2. To handle preflight requests you will specifically need to add a `@router.options(...)` to your router## Usage Examples
### Router Instance
This implementation is equally
```py
from pyzeus import zeus
from fastapi import APIRouter, Dependsrouter = APIRouter(dependencies=[Depends(zeus().thunder)])
@router.get("/")
async def hander():
return { "message": "lorem ipsum" }@router.options("/")
async def options_hander():
return None
```### Specific Route
This implements a sync or async agnostic decorator that requires you to add request and response parameters in your route handler. Worry not, it works if you have pydanyic classes too! Needs python 3.8+ or run `pip install typing_extensions`
```py
from pyzeus import zeus
from fastapi import APIRouter, Dependsrouter = APIRouter()
# Synchronous example
@router.get("/")
@zeus().smite
def synchronous_handler(request: Request, response: Response):
return { "message": "lorem ipsum" }# Asynchronous example
@router.get("/")
@zeus().smite
async def asynchronous_handler(request: Request, response: Response):
return { "message": "lorem ipsum" }# Pydantic example
class Item(BaseModel):
name: str@router.post("/")
@zeus().smite
async def asynchronous_handler(request: Request, response: Response, item: Item):
return { "message": item }
```# Changelog
## v0.1.x
v0.1.3
- Fixed Origin determination for not preflight requests
v0.1.2
- Removed `functools`, and `typing_extensions` from dependencies
v0.1.1
- Added changelog to README
- Added dependenciesv0.1.0
- Initial release