Skip to content

Commit 2739924

Browse files
committed
refactor(pagination): Deprecate Params and Info and add PaginationInfo
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent f1ef400 commit 2739924

File tree

3 files changed

+141
-9
lines changed

3 files changed

+141
-9
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
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` class is deprecated; use the protobuf message directly.
9+
- The `pagination.Info` class is deprecated in favor of the new `pagination.PaginationInfo` class.
910

1011
## New Features
1112

1213
- Mapping for the new `Event` message has been added.
1314
- Add new common API enums for `ElectricalComponent` (previously `Components`).
1415

16+
- Added `v1alpha8` variants of the pagination data structures.
17+
1518
## Bug Fixes
1619

1720
- Updated display of protobuf version warnings

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

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@
99
from typing import Self
1010

1111
# 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
12+
from frequenz.api.common.v1.pagination.pagination_info_pb2 import (
13+
PaginationInfo as PBPaginationInfo,
14+
)
15+
from frequenz.api.common.v1.pagination.pagination_params_pb2 import (
16+
PaginationParams as PBPaginationParams,
17+
)
18+
from frequenz.api.common.v1alpha8.pagination.pagination_info_pb2 import (
19+
PaginationInfo as PBPaginationInfoAlpha8,
20+
)
21+
from typing_extensions import deprecated
1422

1523
# pylint: enable=no-name-in-module
1624

1725

26+
@deprecated(
27+
"Params is deprecated, use "
28+
"frequenz.api.common.v1.pagination.pagination_params_pb2.PaginationParams"
29+
" from the API directly instead.",
30+
)
1831
@dataclass(frozen=True, kw_only=True)
1932
class Params:
2033
"""Parameters for paginating list requests."""
@@ -26,7 +39,7 @@ class Params:
2639
"""The token identifying a specific page of the list results."""
2740

2841
@classmethod
29-
def from_proto(cls, pagination_params: PaginationParams) -> Self:
42+
def from_proto(cls, pagination_params: PBPaginationParams) -> Self:
3043
"""Convert a protobuf Params to PaginationParams object.
3144
3245
Args:
@@ -39,18 +52,21 @@ def from_proto(cls, pagination_params: PaginationParams) -> Self:
3952
page_token=pagination_params.page_token,
4053
)
4154

42-
def to_proto(self) -> PaginationParams:
55+
def to_proto(self) -> PBPaginationParams:
4356
"""Convert a Params object to protobuf PaginationParams.
4457
4558
Returns:
4659
Protobuf message corresponding to the Params object.
4760
"""
48-
return PaginationParams(
61+
return PBPaginationParams(
4962
page_size=self.page_size,
5063
page_token=self.page_token,
5164
)
5265

5366

67+
@deprecated(
68+
"Info is deprecated, use PaginationInfo instead.",
69+
)
5470
@dataclass(frozen=True, kw_only=True)
5571
class Info:
5672
"""Information about the pagination of a list request."""
@@ -62,7 +78,7 @@ class Info:
6278
"""The token identifying the next page of results."""
6379

6480
@classmethod
65-
def from_proto(cls, pagination_info: PaginationInfo) -> Self:
81+
def from_proto(cls, pagination_info: PBPaginationInfo) -> Self:
6682
"""Convert a protobuf PBPaginationInfo to Info object.
6783
6884
Args:
@@ -75,13 +91,62 @@ def from_proto(cls, pagination_info: PaginationInfo) -> Self:
7591
next_page_token=pagination_info.next_page_token,
7692
)
7793

78-
def to_proto(self) -> PaginationInfo:
94+
def to_proto(self) -> PBPaginationInfo:
7995
"""Convert a Info object to protobuf PBPaginationInfo.
8096
8197
Returns:
8298
Protobuf message corresponding to the Info object.
8399
"""
84-
return PaginationInfo(
100+
return PBPaginationInfo(
101+
total_items=self.total_items,
102+
next_page_token=self.next_page_token,
103+
)
104+
105+
106+
@dataclass(frozen=True, kw_only=True)
107+
class PaginationInfo:
108+
"""Information about the pagination of a list request."""
109+
110+
total_items: int
111+
"""The total number of items that match the request."""
112+
113+
next_page_token: str | None = None
114+
"""The token identifying the next page of results."""
115+
116+
@classmethod
117+
def from_proto(
118+
cls, pagination_info: PBPaginationInfoAlpha8 | PBPaginationInfo
119+
) -> Self:
120+
"""Convert a protobuf PBPaginationInfo to Info object.
121+
122+
Args:
123+
pagination_info: Info to convert.
124+
Returns:
125+
Info object corresponding to the protobuf message.
126+
"""
127+
return cls(
128+
total_items=pagination_info.total_items,
129+
next_page_token=pagination_info.next_page_token,
130+
)
131+
132+
def to_proto_v1alpha8(self) -> PBPaginationInfoAlpha8:
133+
"""Convert a Info object to protobuf PBPaginationInfo.
134+
135+
Returns:
136+
Protobuf message corresponding to the Info object.
137+
"""
138+
return PBPaginationInfoAlpha8(
139+
total_items=self.total_items,
140+
next_page_token=self.next_page_token,
141+
)
142+
143+
def to_proto(self) -> PBPaginationInfo:
144+
"""Convert a Info object to protobuf PBPaginationInfo.
145+
146+
Returns:
147+
Protobuf message corresponding to the Info object.
148+
"""
149+
return PBPaginationInfo(
85150
total_items=self.total_items,
86151
next_page_token=self.next_page_token,
87152
)

tests/test_pagination.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for the pagination module."""
5+
6+
import pytest
7+
from frequenz.api.common.v1.pagination.pagination_info_pb2 import (
8+
PaginationInfo as PBPaginationInfo,
9+
)
10+
from frequenz.api.common.v1alpha8.pagination.pagination_info_pb2 import (
11+
PaginationInfo as PBPaginationInfoAlpha8,
12+
)
13+
14+
from frequenz.client.common.pagination import Info, PaginationInfo
15+
16+
17+
def test_pagination_info_from_proto_v1() -> None:
18+
"""Test the PaginationInfo from_proto method with v1 proto."""
19+
proto = PBPaginationInfo(total_items=100, next_page_token="token")
20+
info = PaginationInfo.from_proto(proto)
21+
assert info.total_items == 100
22+
assert info.next_page_token == "token"
23+
24+
25+
def test_pagination_info_from_proto_v1alpha8() -> None:
26+
"""Test the PaginationInfo from_proto method with v1alpha8 proto."""
27+
proto = PBPaginationInfoAlpha8(total_items=100, next_page_token="token")
28+
info = PaginationInfo.from_proto(proto)
29+
assert info.total_items == 100
30+
assert info.next_page_token == "token"
31+
32+
33+
def test_pagination_info_to_proto_v1() -> None:
34+
"""Test the PaginationInfo to_proto method."""
35+
info = PaginationInfo(total_items=100, next_page_token="token")
36+
proto = info.to_proto()
37+
assert proto.total_items == 100
38+
assert proto.next_page_token == "token"
39+
40+
41+
def test_pagination_info_to_proto_v1alpha8() -> None:
42+
"""Test the PaginationInfo to_proto_v1alpha8 method."""
43+
info = PaginationInfo(total_items=100, next_page_token="token")
44+
proto = info.to_proto_v1alpha8()
45+
assert proto.total_items == 100
46+
assert proto.next_page_token == "token"
47+
48+
49+
def test_deprecated_info_from_proto() -> None:
50+
"""Test the deprecated Info from_proto method."""
51+
proto = PBPaginationInfo(total_items=100, next_page_token="token")
52+
with pytest.deprecated_call():
53+
info = Info.from_proto(proto)
54+
assert info.total_items == 100
55+
assert info.next_page_token == "token"
56+
57+
58+
def test_deprecated_info_to_proto() -> None:
59+
"""Test the deprecated Info to_proto method."""
60+
with pytest.deprecated_call():
61+
info = Info(total_items=100, next_page_token="token")
62+
proto = info.to_proto()
63+
assert proto.total_items == 100
64+
assert proto.next_page_token == "token"

0 commit comments

Comments
 (0)