Skip to main content

Flask adapter for superfunctions.http

Project description

superfunctions-flask

Flask adapter for superfunctions.http

Location: packages/python-flask/
Package: superfunctions-flask
Import: from superfunctions_flask import create_blueprint

Installation

pip install superfunctions-flask

Usage

Option 1: Create Blueprint from Routes

from flask import Flask
from superfunctions.http import Route, HttpMethod, Response, NotFoundError
from superfunctions_flask import create_blueprint

app = Flask(__name__)

# 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 request.json()
    user = await db.create(...)
    return Response(status=201, body=user)

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

blueprint = create_blueprint(routes, name="api", url_prefix="/api/v1")
app.register_blueprint(blueprint)

Option 2: Convert Individual Handlers

from flask import Flask
from superfunctions_flask import to_flask_handler
from superfunctions.http import Response

app = Flask(__name__)

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

@app.route("/users/<id>")
def route(id):
    handler = to_flask_handler(get_user)
    return handler(id=id)

Features

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

Benefits

Write framework-agnostic handlers that work across:

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

Example with authfn

from flask import Flask
from superfunctions_flask import create_blueprint
from superfunctions.http import Route, HttpMethod, Response, UnauthorizedError
from authfn import create_authfn, AuthFnConfig

app = Flask(__name__)
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),
]

blueprint = create_blueprint(routes)
app.register_blueprint(blueprint)

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_flask-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

superfunctions_flask-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for superfunctions_flask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6265a61c95087ab107d28d3b3490dfa7ababb16c2e00696dbad1ae1a3e5f7253
MD5 7c780781b72bc39b3da7d8e4247c797f
BLAKE2b-256 62f9a57fbee5a69b448b31c5782c203f5e5a1241506741fa2c7666611e314e83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for superfunctions_flask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45a7bd587b5a75af69d5b9e74a365bdc4a709ae97dbcd9d7851acff3ec11f470
MD5 b423cac3ff4e85f429db61926d1c4841
BLAKE2b-256 c5fdc6bcf1aadba2bfad0b9465bf4f7ebd1fb8a856b8d067c16870397d0d0e49

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