Skip to main content

FastAPI adapter for superfunctions.http

Project description

superfunctions-fastapi

FastAPI adapter for superfunctions.http

Location: packages/python-fastapi/
Package: superfunctions-fastapi
Import: from superfunctions_fastapi import create_router

Installation

pip install superfunctions-fastapi

Usage

Option 1: Create Router from Routes

from fastapi import FastAPI
from superfunctions.http import Route, HttpMethod, Response, NotFoundError
from superfunctions_fastapi import create_router

app = FastAPI()

# Define handlers using superfunctions abstractions
async def get_user(request, context):
    user_id = context.params["id"]
    
    # Your logic here
    user = await db.find_one(...)
    if not user:
        raise NotFoundError(f"User {user_id} not found")
    
    return Response(status=200, body=user)

async def create_user(request, context):
    data = await context.json()
    user = await db.create(...)
    return Response(status=201, body=user)

# Create router from routes
routes = [
    Route(method=HttpMethod.GET, path="/users/{id}", handler=get_user),
    Route(method=HttpMethod.POST, path="/users", handler=create_user),
]

router = create_router(routes, prefix="/api/v1", tags=["users"])
app.include_router(router)

Option 2: Convert Individual Handlers

from fastapi import FastAPI, Request
from superfunctions_fastapi import to_fastapi_handler
from superfunctions.http import Response

app = FastAPI()

async def get_user(request, context):
    return Response(status=200, body={"id": context.params["id"]})

@app.get("/users/{id}")
async def route(request: Request, id: str):
    handler = to_fastapi_handler(get_user)
    return await handler(request, id=id)

Features

  • ✅ Automatic request/response conversion
  • ✅ HTTP error handling
  • ✅ Path parameters
  • ✅ Query parameters
  • ✅ Headers
  • ✅ JSON body parsing
  • ✅ OpenAPI documentation support

Benefits

Write framework-agnostic handlers that work across:

  • FastAPI
  • Flask (with superfunctions-flask)
  • Django (with superfunctions-django)

Example with authfn

from fastapi import FastAPI, Depends
from superfunctions_fastapi import create_router
from superfunctions.http import Route, HttpMethod, Response, UnauthorizedError
from authfn import create_authfn, AuthFnConfig

app = FastAPI()
auth = create_authfn(AuthFnConfig(database=adapter))

async def get_protected_resource(request, context):
    # Authenticate using authfn
    auth_header = context.headers.get("authorization")
    if not auth_header:
        raise UnauthorizedError("Missing authorization header")
    
    session = await auth.provider.authenticate(request)
    if not session:
        raise UnauthorizedError("Invalid credentials")
    
    return Response(status=200, body={"message": "Protected resource", "user": session.keyId})

routes = [
    Route(method=HttpMethod.GET, path="/protected", handler=get_protected_resource),
]

router = create_router(routes)
app.include_router(router)

License

MIT

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

superfunctions_fastapi-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

superfunctions_fastapi-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file superfunctions_fastapi-0.1.0.tar.gz.

File metadata

  • Download URL: superfunctions_fastapi-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for superfunctions_fastapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 89c35b0e3f4bbbea9ffa68aa3cbf65976631f5a1740de7d4ab61d6bd37e9a3c5
MD5 e6ae66ed981e0cc6b12469cac9db5faa
BLAKE2b-256 0b63aba5cabd20666f4501ffe601d01752874ebb76f37c180fef43c58a7b1f92

See more details on using hashes here.

File details

Details for the file superfunctions_fastapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for superfunctions_fastapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bbb8692cf44d04a975a146abd1b6f369b180eb8a4480585a44f67335a7c29ef
MD5 75afbf47aa676637d723d56e6fad5ca2
BLAKE2b-256 d1c52c1b2038c4a4d2123b82dc5dbc165ae0c1d789bae2e30c4d10ffc8d1c7f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page