|
2 | 2 | "paginate", |
3 | 3 | ] |
4 | 4 |
|
| 5 | +from typing import Any, Optional |
5 | 6 |
|
6 | 7 | from elasticsearch import Elasticsearch |
7 | 8 | from elasticsearch_dsl import Search |
8 | 9 |
|
9 | 10 | from fastapi_pagination.api import apply_items_transformer, create_page |
10 | | -from fastapi_pagination.bases import AbstractParams, CursorRawParams, is_limit_offset |
| 11 | +from fastapi_pagination.bases import AbstractParams, is_limit_offset |
11 | 12 | from fastapi_pagination.types import AdditionalData, SyncItemsTransformer |
12 | 13 | from fastapi_pagination.utils import verify_params |
13 | 14 |
|
14 | 15 |
|
15 | 16 | def paginate( |
16 | 17 | conn: Elasticsearch, |
17 | 18 | query: Search, |
18 | | - params: AbstractParams | None = None, |
| 19 | + params: Optional[AbstractParams] = None, |
19 | 20 | *, |
20 | | - transformer: SyncItemsTransformer | None = None, |
21 | | - additional_data: AdditionalData | None = None, |
22 | | -): |
| 21 | + transformer: Optional[SyncItemsTransformer] = None, |
| 22 | + additional_data: Optional[AdditionalData] = None, |
| 23 | +) -> Any: |
23 | 24 | params, raw_params = verify_params(params, "limit-offset", "cursor") |
24 | | - if is_limit_offset(raw_params): |
25 | | - total = query.using(conn).count() |
26 | | - response = query.using(conn)[raw_params.offset : raw_params.offset + raw_params.limit].execute() |
27 | | - items = response |
28 | | - t_items = apply_items_transformer(items, transformer) |
29 | | - return create_page( |
30 | | - t_items, |
31 | | - total=total, |
32 | | - params=params, |
33 | | - **(additional_data or {}), |
34 | | - ) |
35 | 25 |
|
36 | 26 | total = None |
37 | 27 | if raw_params.include_total: |
38 | 28 | total = query.using(conn).count() |
39 | | - raw_params: CursorRawParams |
40 | | - if not raw_params.cursor: |
41 | | - response = query.params(scroll="1m").extra(size=raw_params.size).execute() |
42 | | - items = response.hits |
43 | | - next_ = response._scroll_id |
| 29 | + |
| 30 | + kwargs = {} |
| 31 | + items: Any |
| 32 | + |
| 33 | + if is_limit_offset(raw_params): |
| 34 | + items = query.using(conn)[raw_params.as_slice()].execute() |
44 | 35 | else: |
45 | | - response = conn.scroll(scroll_id=raw_params.cursor, scroll="1m") |
46 | | - next_ = response.get("_scroll_id") |
47 | | - items = [item.get("_source") for item in response["hits"]["hits"]] |
| 36 | + raw_params = raw_params.as_cursor() |
| 37 | + |
| 38 | + response: Any |
| 39 | + if not raw_params.cursor: |
| 40 | + response = query.params(scroll="1m").extra(size=raw_params.size).execute() |
| 41 | + items = response.hits |
| 42 | + next_ = response._scroll_id |
| 43 | + else: |
| 44 | + response = conn.scroll(scroll_id=raw_params.cursor, scroll="1m") # type: ignore[arg-type] |
| 45 | + next_ = response.get("_scroll_id") |
| 46 | + items = [item.get("_source") for item in response["hits"]["hits"]] |
| 47 | + |
| 48 | + kwargs["next_"] = next_ |
| 49 | + |
48 | 50 | t_items = apply_items_transformer(items, transformer) |
| 51 | + |
49 | 52 | return create_page( |
50 | 53 | t_items, |
51 | 54 | total=total, |
52 | 55 | params=params, |
53 | | - next_=next_, |
| 56 | + **kwargs, |
54 | 57 | **(additional_data or {}), |
55 | 58 | ) |
0 commit comments