fastapi-pagination icon indicating copy to clipboard operation
fastapi-pagination copied to clipboard

`Page` with original response structure

Open mattibo opened this issue 1 year ago • 1 comments

Hi @uriyyo,

I would like to have a pagination style like the GitHub REST API (using link in reponse header) but with the original content as the response (and not the content nested inside e.g. items list with the default Page). Is this possible?

mattibo avatar Oct 10 '24 18:10 mattibo

Hi @mattibo,

Unfortunately, there is no easy solution like just import stmh like UseHeaderLinks but you can implement you own Page:

from typing import Any, Sequence, TypeVar

from fastapi.applications import FastAPI
from pydantic import RootModel
from typing_extensions import Self

from fastapi_pagination import Page, add_pagination, paginate, response
from fastapi_pagination.bases import AbstractPage, AbstractParams
from fastapi_pagination.links import Page as LinksPage

app = FastAPI()
add_pagination(app)

T = TypeVar("T")


class FlatPage(RootModel[list[T]], AbstractPage[T]):
    __params_type__ = Page.__params_type__

    @classmethod
    def create(
        cls,
        items: Sequence[T],
        params: AbstractParams,
        **kwargs: Any,
    ) -> Self:
        with_links = LinksPage.create(items, params, **kwargs)

        links = []
        for rel, link in (
            ("first", with_links.links.first),
            ("last", with_links.links.last),
            ("next", with_links.links.next),
            ("prev", with_links.links.prev),
        ):
            if link is not None:
                links.append(f'<{link}>; rel="{rel}"')

        if links:
            rsp = response()
            rsp.headers["Link"] = ", ".join(links)

        return cls(with_links.items)


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


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)

uriyyo avatar Oct 13 '24 08:10 uriyyo

Hi @mattibo,

Can I close this issue? Is there any other question?

uriyyo avatar Nov 16 '24 10:11 uriyyo