Skip to content
Discussion options

You must be logged in to vote

You won't have much luck with a middleware, since the response body has already been serialised.

There are several ways you could do this. One would be to implement a custom response class:

class ValidatingResponse[T](Response[T]):
    async def to_asgi_response[UserT, AuthT](self, request: Request[UserT, AuthT, Any]) -> ASGIResponse:
        content = self.content  # the unserialised response content
        user = request.user  # the user set by your auth middleware
        auth = request.auth  # the token instance created by your auth middleware


@get("/", response_class=ValidatingResponse)
async def get_things() -> Things:
    things = await fetch_things_from_db()
    return things

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@trashdid
Comment options

@provinzkraut
Comment options

@trashdid
Comment options

Answer selected by trashdid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants