Skip to main content

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

Project description

pyzeus logo

pyzeus ⚡

The cors middleware that enables a FastAPI server to handle cors requests. It also handles preflight requests 😃.

Installation

pip install pyzeus

or

pip3 install pyzeus

Default Response Headers

If no options are provided, the response headers will be as follows:

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

from pyzeus import zeus
from fastapi import APIRouter, Depends

router = 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

from pyzeus import zeus
from fastapi import APIRouter, Depends

router = 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 dependencies
v0.1.0
  • Initial release

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

pyzeus-0.1.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pyzeus-0.1.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file pyzeus-0.1.3.tar.gz.

File metadata

  • Download URL: pyzeus-0.1.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pyzeus-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2eab4f6e0c553acfb4e5b158a14453a17899bff576dd989d844413dadcf2e81e
MD5 d579829e0c9ce226bac65511ffa08a15
BLAKE2b-256 7b6903c0ecaac3e306045c088bab90e37b54d3d8c06fced58a4dcea2041099e6

See more details on using hashes here.

File details

Details for the file pyzeus-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pyzeus-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pyzeus-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f8f280a32ba52377fbca30eea4db3c9c862ce76b2417a04380638b38475e78c1
MD5 3afba4af231e6c7a15146c989e4ccd52
BLAKE2b-256 db9ec22a72ec7acd4dc5c88d9479263e5f4309f4f76254991d2b8e3049d39027

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