-
Hello, is there a way to wrap handlers responses (JSON/HTTP) with a generic schema? For example, an endpoint: from dataclasses import dataclass
from typing import Generic, TypeVar
from starlite import get
from pydantic import BaseModel
T = TypeVar("T")
@dataclass()
class ResponseWrapper(Generic[T]):
data: T
error: bool = False
# something to tell Starlite to wrap some (or all) HTTP handlers responses with a `ResponseWrapper`
...
class Pottery(BaseModel):
name: str
@get("/pottery")
def retrieve_pottery() -> Pottery:
return Pottery(name="Teapot")
# Registering the handler in Starlite
... Returns a response like: // GET /pottery 200 OK
{
"error": false, // Metadata
"data": {"name": "Teapot"} // Here the `Pottery` resource
} Already tried to use generics in handlers return annotation (used like the pagination feature) but the OpenAPI schema generation broke. |
Beta Was this translation helpful? Give feedback.
Answered by
Goldziher
Dec 6, 2022
Replies: 1 comment 1 reply
-
Hi, No there is no such a feature. You can use the |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
acwazz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
No there is no such a feature. You can use the
after_request
hook to transform responses into this form, and you can also define a customexception_handler
, so you can basically do this.