Skip to content

Commit 630f662

Browse files
committed
Add v1alpha8 variant of pagination structs
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent f1ef400 commit 630f662

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
## Upgrading
77

8-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
8+
- The `pagination.Params` and `pagination.Info` classes have been deprecated in favor of `pagination.PaginationParams` and `pagination.PaginationInfo` respectively.
99

1010
## New Features
1111

1212
- Mapping for the new `Event` message has been added.
1313
- Add new common API enums for `ElectricalComponent` (previously `Components`).
1414

15+
- Added `v1alpha8` variants of the pagination data structures.
16+
1517
## Bug Fixes
1618

1719
- Updated display of protobuf version warnings

src/frequenz/client/common/pagination/__init__.py

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@
88
from dataclasses import dataclass
99
from typing import Self
1010

11+
from typing_extensions import deprecated
12+
1113
# 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+
from frequenz.api.common.v1.pagination.pagination_info_pb2 import (
15+
PaginationInfo as PBPaginationInfo,
16+
)
17+
from frequenz.api.common.v1.pagination.pagination_params_pb2 import (
18+
PaginationParams as PBPaginationParams,
19+
)
20+
from frequenz.api.common.v1alpha8.pagination.pagination_info_pb2 import (
21+
PaginationInfo as PBPaginationInfoAlpha8,
22+
)
23+
from frequenz.api.common.v1alpha8.pagination.pagination_params_pb2 import (
24+
PaginationParams as PBPaginationParamsAlpha8,
25+
)
1426

1527
# pylint: enable=no-name-in-module
1628

1729

30+
@deprecated(
31+
"Params is deprecated, use PaginationParams instead.",
32+
)
1833
@dataclass(frozen=True, kw_only=True)
1934
class Params:
2035
"""Parameters for paginating list requests."""
@@ -26,7 +41,7 @@ class Params:
2641
"""The token identifying a specific page of the list results."""
2742

2843
@classmethod
29-
def from_proto(cls, pagination_params: PaginationParams) -> Self:
44+
def from_proto(cls, pagination_params: PBPaginationParams) -> Self:
3045
"""Convert a protobuf Params to PaginationParams object.
3146
3247
Args:
@@ -39,18 +54,21 @@ def from_proto(cls, pagination_params: PaginationParams) -> Self:
3954
page_token=pagination_params.page_token,
4055
)
4156

42-
def to_proto(self) -> PaginationParams:
57+
def to_proto(self) -> PBPaginationParams:
4358
"""Convert a Params object to protobuf PaginationParams.
4459
4560
Returns:
4661
Protobuf message corresponding to the Params object.
4762
"""
48-
return PaginationParams(
63+
return PBPaginationParams(
4964
page_size=self.page_size,
5065
page_token=self.page_token,
5166
)
5267

5368

69+
@deprecated(
70+
"Info is deprecated, use PaginationInfo instead.",
71+
)
5472
@dataclass(frozen=True, kw_only=True)
5573
class Info:
5674
"""Information about the pagination of a list request."""
@@ -62,7 +80,79 @@ class Info:
6280
"""The token identifying the next page of results."""
6381

6482
@classmethod
65-
def from_proto(cls, pagination_info: PaginationInfo) -> Self:
83+
def from_proto(cls, pagination_info: PBPaginationInfo) -> Self:
84+
"""Convert a protobuf PBPaginationInfo to Info object.
85+
86+
Args:
87+
pagination_info: Info to convert.
88+
Returns:
89+
Info object corresponding to the protobuf message.
90+
"""
91+
return cls(
92+
total_items=pagination_info.total_items,
93+
next_page_token=pagination_info.next_page_token,
94+
)
95+
96+
def to_proto(self) -> PBPaginationInfo:
97+
"""Convert a Info object to protobuf PBPaginationInfo.
98+
99+
Returns:
100+
Protobuf message corresponding to the Info object.
101+
"""
102+
return PBPaginationInfo(
103+
total_items=self.total_items,
104+
next_page_token=self.next_page_token,
105+
)
106+
107+
108+
@dataclass(frozen=True, kw_only=True)
109+
class PaginationParams:
110+
"""Parameters for paginating list requests."""
111+
112+
page_size: int
113+
"""The maximum number of results to be returned per request."""
114+
115+
page_token: str
116+
"""The token identifying a specific page of the list results."""
117+
118+
@classmethod
119+
def from_proto(cls, pagination_params: PBPaginationParamsAlpha8) -> Self:
120+
"""Convert a protobuf PaginationParams to python PaginationParams object.
121+
122+
Args:
123+
pagination_params: Params to convert.
124+
Returns:
125+
Params object corresponding to the protobuf message.
126+
"""
127+
return cls(
128+
page_size=pagination_params.page_size,
129+
page_token=pagination_params.page_token,
130+
)
131+
132+
def to_proto(self) -> PBPaginationParamsAlpha8:
133+
"""Convert a Params object to protobuf PaginationParams.
134+
135+
Returns:
136+
Protobuf message corresponding to the Params object.
137+
"""
138+
return PBPaginationParamsAlpha8(
139+
page_size=self.page_size,
140+
page_token=self.page_token,
141+
)
142+
143+
144+
@dataclass(frozen=True, kw_only=True)
145+
class PaginationInfo:
146+
"""Information about the pagination of a list request."""
147+
148+
total_items: int
149+
"""The total number of items that match the request."""
150+
151+
next_page_token: str | None = None
152+
"""The token identifying the next page of results."""
153+
154+
@classmethod
155+
def from_proto(cls, pagination_info: PBPaginationInfoAlpha8) -> Self:
66156
"""Convert a protobuf PBPaginationInfo to Info object.
67157
68158
Args:
@@ -75,13 +165,13 @@ def from_proto(cls, pagination_info: PaginationInfo) -> Self:
75165
next_page_token=pagination_info.next_page_token,
76166
)
77167

78-
def to_proto(self) -> PaginationInfo:
168+
def to_proto(self) -> PBPaginationInfoAlpha8:
79169
"""Convert a Info object to protobuf PBPaginationInfo.
80170
81171
Returns:
82172
Protobuf message corresponding to the Info object.
83173
"""
84-
return PaginationInfo(
174+
return PBPaginationInfoAlpha8(
85175
total_items=self.total_items,
86176
next_page_token=self.next_page_token,
87177
)

0 commit comments

Comments
 (0)