pait icon indicating copy to clipboard operation
pait copied to clipboard

A simpler way to use Json responses

Open so1n opened this issue 2 months ago • 0 comments

The declaration of the JsonResponse in the example is too complicated:

class DemoResponseModel(JsonResponseModel):
    class ResponseModel(BaseModel):
        uid: int = Field()
        user_name: str = Field()

    description: str = "demo response"
    response_data: Type[BaseModel] = ResponseModel


@pait(response_model_list=[DemoResponseModel])
async def demo_post(
    uid: int = Json.t(description="user id", gt=10, lt=1000),
    username: str = Json.t(description="user name", min_length=2, max_length=4),
) -> JSONResponse:
    return JSONResponse({"uid": uid, "user_name": username})

Json is a common API response format, can define some rules to make the definition of JsonResponse more simple, such as :

class DemoResponseModel(BaseModel):
    """demo response"""
    uid: int = Field()
    user_name: str = Field()

@pait(response_model_list=[DemoResponseModel])
async def demo_post(
    uid: int = Json.t(description="user id", gt=10, lt=1000),
    username: str = Json.t(description="user name", min_length=2, max_length=4),
) -> JSONResponse:
    return JSONResponse({"uid": uid, "user_name": username})

so1n avatar Apr 24 '24 15:04 so1n