Skip to content

Commit c6ff074

Browse files
Merge pull request #15 from flora-hofmann-frequenz/add_pagination
Add pagination types
2 parents 5e6e7b7 + b4c3d53 commit c6ff074

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Module to define the pagination used with the common client."""
5+
6+
from __future__ import annotations # required for constructor type hinting
7+
8+
from dataclasses import dataclass
9+
from typing import Self
10+
11+
# pylint: disable=no-name-in-module
12+
from frequenz.api.common.v1.pagination.pagination_info_pb2 import PaginationInfo
13+
from frequenz.api.common.v1.pagination.pagination_params_pb2 import PaginationParams
14+
15+
# pylint: enable=no-name-in-module
16+
17+
18+
@dataclass(frozen=True, kw_only=True)
19+
class Params:
20+
"""Parameters for paginating list requests."""
21+
22+
page_size: int | None = None
23+
"""The maximum number of results to be returned per request."""
24+
25+
page_token: str | None = None
26+
"""The token identifying a specific page of the list results."""
27+
28+
@classmethod
29+
def from_proto(cls, pagination_params: PaginationParams) -> Self:
30+
"""Convert a protobuf Params to PaginationParams object.
31+
32+
Args:
33+
pagination_params: Params to convert.
34+
Returns:
35+
Params object corresponding to the protobuf message.
36+
"""
37+
return cls(
38+
page_size=pagination_params.page_size,
39+
page_token=pagination_params.page_token,
40+
)
41+
42+
def to_proto(self) -> PaginationParams:
43+
"""Convert a Params object to protobuf PaginationParams.
44+
45+
Returns:
46+
Protobuf message corresponding to the Params object.
47+
"""
48+
return PaginationParams(
49+
page_size=self.page_size,
50+
page_token=self.page_token,
51+
)
52+
53+
54+
@dataclass(frozen=True, kw_only=True)
55+
class Info:
56+
"""Information about the pagination of a list request."""
57+
58+
total_items: int
59+
"""The total number of items that match the request."""
60+
61+
next_page_token: str | None = None
62+
"""The token identifying the next page of results."""
63+
64+
@classmethod
65+
def from_proto(cls, pagination_info: PaginationInfo) -> Self:
66+
"""Convert a protobuf PBPaginationInfo to Info object.
67+
68+
Args:
69+
pagination_info: Info to convert.
70+
Returns:
71+
Info object corresponding to the protobuf message.
72+
"""
73+
return cls(
74+
total_items=pagination_info.total_items,
75+
next_page_token=pagination_info.next_page_token,
76+
)
77+
78+
def to_proto(self) -> PaginationInfo:
79+
"""Convert a Info object to protobuf PBPaginationInfo.
80+
81+
Returns:
82+
Protobuf message corresponding to the Info object.
83+
"""
84+
return PaginationInfo(
85+
total_items=self.total_items,
86+
next_page_token=self.next_page_token,
87+
)

0 commit comments

Comments
 (0)