oncall-engine/engine/common/api_helpers/paginators.py
Vadim Stepanov d0904ca405
Improve OpenAPI schema coverage (#3629)
# What this PR does

Improves OpenAPI schema coverage for internal API:

- Fixes/Improves `alert group` and `feature` endpoints
- Adds `integration` and `user` endpoints

## Which issue(s) this PR fixes

https://github.com/grafana/oncall/issues/3444

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2024-01-12 15:11:22 +00:00

88 lines
2.8 KiB
Python

import typing
from rest_framework.pagination import BasePagination, CursorPagination, PageNumberPagination
from rest_framework.response import Response
from common.api_helpers.utils import create_engine_url
PaginatedData = typing.List[typing.Any]
class BasePaginatedResponseData(typing.TypedDict):
next: str | None
previous: str | None
results: PaginatedData
page_size: int
class PageBasedPaginationResponseData(BasePaginatedResponseData):
count: int
current_page_number: int
total_pages: int
class BasePathPrefixedPagination(BasePagination):
max_page_size = 100
page_query_param = "page"
page_size_query_param = "perpage"
def paginate_queryset(self, queryset, request, view=None):
request.build_absolute_uri = lambda: create_engine_url(request.get_full_path())
return super().paginate_queryset(queryset, request, view)
class PathPrefixedPagePagination(BasePathPrefixedPagination, PageNumberPagination):
def get_paginated_response(self, data: PaginatedData) -> Response:
response = super().get_paginated_response(data)
response.data.update(
{
"page_size": self.get_page_size(self.request),
"current_page_number": self.page.number,
"total_pages": self.page.paginator.num_pages,
}
)
return response
def get_paginated_response_schema(self, schema):
paginated_schema = super().get_paginated_response_schema(schema)
paginated_schema["properties"].update(
{
"page_size": {"type": "integer"},
"current_page_number": {"type": "integer"},
"total_pages": {"type": "integer"},
}
)
return paginated_schema
class PathPrefixedCursorPagination(BasePathPrefixedPagination, CursorPagination):
def get_paginated_response(self, data: PaginatedData) -> Response:
response = super().get_paginated_response(data)
response.data.update({"page_size": self.page_size})
return response
def get_paginated_response_schema(self, schema):
paginated_schema = super().get_paginated_response_schema(schema)
paginated_schema["properties"].update({"page_size": {"type": "integer"}})
return paginated_schema
class HundredPageSizePaginator(PathPrefixedPagePagination):
page_size = 100
class FiftyPageSizePaginator(PathPrefixedPagePagination):
page_size = 50
class TwentyFivePageSizePaginator(PathPrefixedPagePagination):
page_size = 25
class FifteenPageSizePaginator(PathPrefixedPagePagination):
page_size = 15
class AlertGroupCursorPaginator(PathPrefixedCursorPagination):
page_size = 25
ordering = "-started_at" # ordering by "-started_at", so it uses the right index (see AlertGroup.Meta.indexes)