Skip to content

Commit 93788f3

Browse files
committed
refactor(client): Update Microgrid serialization and remove deprecated functions
- Replaced the `to_dict` methods in `DeliveryArea` and `Location` classes with a new `to_json` method in the `Microgrid` class for improved JSON serialization. - Removed the deprecated `delete_me` function from the codebase. - Updated the `RELEASE_NOTES.md` to reflect these changes and added new required dependencies. These modifications enhance the API's usability and maintainability while ensuring a cleaner codebase. Signed-off-by: eduardiazf <[email protected]>
1 parent cfb3448 commit 93788f3

File tree

4 files changed

+23
-60
lines changed

4 files changed

+23
-60
lines changed

RELEASE_NOTES.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ This release introduces a complete Assets API client with CLI support for intera
88

99
**Breaking Changes:**
1010

11-
- Removed deprecated `delete_me` function
1211
- Added new required dependencies: `frequenz-api-assets`, `frequenz-api-common`, `frequenz-client-base`, `grpcio`
1312

1413
**CLI Support:**

src/frequenz/client/assets/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def print_microgrid_details(microgrid: Microgrid) -> None:
5454
Args:
5555
microgrid: The Microgrid instance to print to console.
5656
"""
57-
click.echo(json.dumps(microgrid.to_dict(), indent=2))
57+
click.echo(microgrid.to_json())
5858

5959

6060
@click.group(invoke_without_command=True)

src/frequenz/client/assets/types.py

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
from __future__ import annotations
1111

12-
from dataclasses import dataclass
12+
import json
13+
from dataclasses import asdict, dataclass
1314
from datetime import datetime, timezone
14-
from typing import Any, Optional
1515

1616
from frequenz.api.common.v1alpha8.grid.delivery_area_pb2 import (
1717
DeliveryArea as PBDeliveryArea,
@@ -44,16 +44,10 @@ def from_protobuf(pb: PBDeliveryArea) -> "DeliveryArea":
4444
Returns:
4545
A new DeliveryArea instance populated with data from the protobuf message.
4646
"""
47-
return DeliveryArea(code=pb.code, code_type=str(pb.code_type))
48-
49-
def to_dict(self) -> dict[str, Any]:
50-
"""
51-
Convert the DeliveryArea instance to a dictionary.
52-
53-
Returns:
54-
A dictionary containing the delivery area details.
55-
"""
56-
return {"code": self.code, "code_type": self.code_type}
47+
return DeliveryArea(
48+
code=pb.code,
49+
code_type=str(pb.code_type),
50+
)
5751

5852

5953
@dataclass(frozen=True)
@@ -67,19 +61,6 @@ class Location:
6761
longitude: float
6862
country_code: str
6963

70-
def to_dict(self) -> dict[str, Any]:
71-
"""
72-
Convert the Location instance to a dictionary.
73-
74-
Returns:
75-
A dictionary containing the location details.
76-
"""
77-
return {
78-
"latitude": self.latitude,
79-
"longitude": self.longitude,
80-
"country_code": self.country_code,
81-
}
82-
8364
@staticmethod
8465
def from_protobuf(pb: PBLocation) -> "Location":
8566
"""
@@ -92,7 +73,9 @@ def from_protobuf(pb: PBLocation) -> "Location":
9273
A new Location instance populated with data from the protobuf message.
9374
"""
9475
return Location(
95-
latitude=pb.latitude, longitude=pb.longitude, country_code=pb.country_code
76+
latitude=pb.latitude,
77+
longitude=pb.longitude,
78+
country_code=pb.country_code,
9679
)
9780

9881

@@ -120,30 +103,11 @@ class Microgrid:
120103
id: MicrogridId
121104
enterprise_id: EnterpriseId
122105
name: str
123-
delivery_area: Optional[DeliveryArea]
124-
location: Optional[Location]
106+
delivery_area: DeliveryArea | None
107+
location: Location | None
125108
status: int
126109
create_time: datetime
127110

128-
def to_dict(self) -> dict[str, Any]:
129-
"""
130-
Convert the Microgrid instance to a dictionary.
131-
132-
Returns:
133-
A dictionary containing the microgrid details.
134-
"""
135-
return {
136-
"id": int(self.id),
137-
"enterprise_id": int(self.enterprise_id),
138-
"name": self.name,
139-
"delivery_area": (
140-
self.delivery_area.to_dict() if self.delivery_area else None
141-
),
142-
"location": self.location.to_dict() if self.location else None,
143-
"status": self.status,
144-
"create_time": self.create_time.isoformat() if self.create_time else None,
145-
}
146-
147111
@staticmethod
148112
def from_protobuf(pb: PBMicrogrid) -> "Microgrid":
149113
"""
@@ -155,11 +119,11 @@ def from_protobuf(pb: PBMicrogrid) -> "Microgrid":
155119
Returns:
156120
A new Microgrid instance populated with data from the protobuf message.
157121
"""
158-
delivery_area: Optional[DeliveryArea] = None
122+
delivery_area: DeliveryArea = None
159123
if pb.HasField("delivery_area"):
160124
delivery_area = DeliveryArea.from_protobuf(pb.delivery_area)
161125

162-
location: Optional[Location] = None
126+
location: Location = None
163127
if pb.HasField("location"):
164128
location = Location.from_protobuf(pb.location)
165129

@@ -172,3 +136,12 @@ def from_protobuf(pb: PBMicrogrid) -> "Microgrid":
172136
status=pb.status,
173137
create_time=pb.create_timestamp.ToDatetime().replace(tzinfo=timezone.utc),
174138
)
139+
140+
def to_json(self) -> str:
141+
"""Convert the Microgrid instance to a JSON string."""
142+
microgrid_dict = asdict(self)
143+
microgrid_dict["id"] = int(self.id)
144+
microgrid_dict["enterprise_id"] = int(self.enterprise_id)
145+
microgrid_dict["create_time"] = self.create_time.isoformat()
146+
147+
return json.dumps(microgrid_dict, indent=2)

tests/conftest.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,3 @@ def client_setup() -> Iterator[ClientSetup]:
7070
mock_location=mock_location,
7171
mock_response=mock_response,
7272
)
73-
74-
# Cleanup: reset ALL mocks automatically
75-
mock_stub.reset_mock()
76-
mock_microgrid.reset_mock()
77-
mock_delivery_area.reset_mock()
78-
mock_location.reset_mock()
79-
mock_response.reset_mock()
80-
client._stub = None # pylint: disable=protected-access
81-
client._channel = None # pylint: disable=protected-access

0 commit comments

Comments
 (0)