uriyyo / fastapi-pagination

FastAPI pagination 📖
https://uriyyo-fastapi-pagination.netlify.app/
MIT License
1.2k stars 136 forks source link

I hope the pagination parameters can be obtained through the POST method #1295

Closed elkon028 closed 1 month ago

elkon028 commented 1 month ago

I hope the pagination parameters can be obtained through the POST method

How to obtain the pagination params using the POST method

uriyyo commented 1 month ago

Hi @elkon028,

Sorry for long response, here is an example:

from typing import TypeVar

from fastapi import Body, FastAPI

from fastapi_pagination import Page as BasePage
from fastapi_pagination import Params as BaseParams
from fastapi_pagination import add_pagination, paginate
from fastapi_pagination.customization import CustomizedPage, UseParams

app = FastAPI()
add_pagination(app)

T = TypeVar("T")

class Params(BaseParams):
    page: int = Body(1, embed=True, ge=1)
    size: int = Body(50, embed=True, ge=1, le=100)

Page = CustomizedPage[
    BasePage[T],
    UseParams(Params),
]

@app.post("/items")
async def read_items() -> Page[int]:
    return paginate([*range(1_000)])

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)
niravjdn commented 1 month ago

Thanks @uriyyo . I'm using the code you gave in below format. and making a POST Call, shows this errors. Can you please help me understand what is going on?

T = TypeVar("T")

class Params(BaseParams):
    page: int = Body(1, embed=True, ge=1)
    size: int = Body(50, embed=True, ge=1, le=100)

CustomPage = CustomizedPage[BasePage[T], UseParams(Params),]

@router.post("/search", status_code=status.HTTP_200_OK, response_model=CustomPage[DatasetDto])
def search_dataset(
    payload: DatasetSearchRequest, service: Annotated[DatasetService, Depends(DatasetService)]
):
    return service.search_dataset(payload)

Curl Request and Response:

Request:

curl --location 'http://127.0.0.1:8080/api/v1/metadata/datasets/search' \
--header 'Authorization: Bearer ey' \
--header 'username: pateln2' \
--header 'Content-Type: application/json' \
--data '{    
   "datasetType": "TABLE"
}'

Response: 422 Error Code


    "detail": [
        {
            "type": "missing",
            "loc": [
                "body",
                "payload"
            ],
            "msg": "Field required",
            "input": null
        }
    ]
}```

If I use Page class, It works but params has to be passed in url.
uriyyo commented 1 month ago

Hi @niravjdn,

Could you please try to use latest version of fastapi-pagination 0.12.28?

niravjdn commented 1 month ago

@uriyyo Still getting same error.

uriyyo commented 1 month ago

@niravjdn

Here is an example of working example, hope it will help you 🙏

from typing import TypeVar

from fastapi import Body, FastAPI, status
from pydantic import BaseModel

from fastapi_pagination import Page as BasePage, add_pagination
from fastapi_pagination import Params as BaseParams
from fastapi_pagination import paginate
from fastapi_pagination.customization import CustomizedPage, UseParams

app = FastAPI()
add_pagination(app)

T = TypeVar("T")

class Params(BaseParams):
    page: int = Body(1, embed=True, ge=1)
    size: int = Body(50, embed=True, ge=1, le=100)

CustomPage = CustomizedPage[
    BasePage[T],
    UseParams(Params),
]

class DatasetDto(BaseModel):
    website: str

class DatasetSearchRequest(BaseModel):
    query: str = "hello"

@app.post("/search", status_code=status.HTTP_200_OK, response_model=CustomPage[DatasetDto])
def search_dataset(
    payload: DatasetSearchRequest = Body(),
):
    return paginate([])

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app)
niravjdn commented 1 month ago

Really Appreciate @uriyyo . Turned out my fastapi version was 0.110.0. Updating to 0.115.0 fixed it.

Thansk a lot :)

uriyyo commented 1 month ago

Great, then I'm closing this issue.