Skip to content

feat(ext): fastapi network context#2762

Open
ChickenBenny wants to merge 4 commits intoApeWorX:mainfrom
ChickenBenny:feat/ext-fastapi-network-context
Open

feat(ext): fastapi network context#2762
ChickenBenny wants to merge 4 commits intoApeWorX:mainfrom
ChickenBenny:feat/ext-fastapi-network-context

Conversation

@ChickenBenny
Copy link

What I did

Added ape.ext.fastapi — a purpose-built FastAPI integration module that provides
a network_context() decorator for managing Ape network contexts in route handlers.

This is the first integration under the new ape.ext package, which is intended
to house future framework integrations (e.g. Flask, Django).

fixes: #2761

How I did it

  • Created ape/ext/ as a new package for external framework integrations
  • Implemented @network_context() in ape/ext/fastapi.py
  • The decorator automatically injects ecosystem and network as FastAPI
    Query parameters by patching wrapper.__signature__ at decoration time —
    so route handlers do not need to declare them explicitly
  • If the handler does declare ecosystem/network, they are forwarded as normal
  • fastapi is kept as an optional dependency, imported lazily inside the decorator
    to avoid making it a hard requirement for all Ape users
  • Only synchronous routes are supported for now, as async network context
    is not yet available in Ape

How to verify it

Install fastapi and run:

pytest tests/functional/test_ext_fastapi.py -v

Expected: 9 passed.

Example usage:

from ape.ext.fastapi import network_context

@app.post("/faucet/{token}")
@network_context()
def send_token(token: str, receiver: str, amount: int) -> str:
    # POST /faucet/USDC?ecosystem=ethereum&network=mainnet
    # Ape is automatically connected to the requested network
    ...

Checklist

  • All changes are completed
  • Change is covered in tests
  • Documentation is complete

@fubuloubu
Copy link
Member

I think that the tests might not work without adding fastapi to test dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integrate with FastAPI

2 participants