Skip to content

Commit 699780f

Browse files
committed
Rename some "api" to "client" or "api_client"
API is too generic, a more specific name makes the code more clear. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 4b34d12 commit 699780f

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

src/frequenz/sdk/microgrid/_power_distributing/_component_managers/_battery_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,18 @@ async def _distribute_power(
309309

310310
async def _create_channels(self) -> None:
311311
"""Create channels to get data of components in microgrid."""
312-
api = connection_manager.get().api_client
312+
client = connection_manager.get().api_client
313313
manager_id = f"{type(self).__name__}«{hex(id(self))}»"
314314
for battery_id, inverter_ids in self._bat_invs_map.items():
315-
bat_recv: Receiver[BatteryData] = BatteryData.subscribe(api, battery_id)
315+
bat_recv: Receiver[BatteryData] = BatteryData.subscribe(client, battery_id)
316316
self._battery_caches[battery_id] = LatestValueCache(
317317
bat_recv,
318318
unique_id=f"{manager_id}:battery«{battery_id}»",
319319
)
320320

321321
for inverter_id in inverter_ids:
322322
inv_recv: Receiver[InverterData] = InverterData.subscribe(
323-
api, inverter_id
323+
client, inverter_id
324324
)
325325
self._inverter_caches[inverter_id] = LatestValueCache(
326326
inv_recv, unique_id=f"{manager_id}:inverter«{inverter_id}»"
@@ -635,11 +635,11 @@ async def _set_distributed_power(
635635
Tuple where first element is total failed power, and the second element
636636
set of batteries that failed.
637637
"""
638-
api = connection_manager.get().api_client
638+
client = connection_manager.get().api_client
639639

640640
tasks = {
641641
inverter_id: asyncio.create_task(
642-
api.set_component_power_active(inverter_id, power.as_watts())
642+
client.set_component_power_active(inverter_id, power.as_watts())
643643
)
644644
for inverter_id, power in distribution.distribution.items()
645645
if power != Power.zero()

src/frequenz/sdk/microgrid/component_graph.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,22 +544,22 @@ def refresh_from(
544544
self._graph = new_graph
545545
old_graph.clear() # just in case any references remain, but should not
546546

547-
async def refresh_from_api(
547+
async def refresh_from_client(
548548
self,
549-
api: MicrogridApiClient,
549+
client: MicrogridApiClient,
550550
correct_errors: Callable[["_MicrogridComponentGraph"], None] | None = None,
551551
) -> None:
552552
"""Refresh the contents of a component graph from the remote API.
553553
554554
Args:
555-
api: The API client from which to fetch graph data.
555+
client: The API client from which to fetch graph data
556556
correct_errors: The callback that, if set, will be invoked if the
557557
provided graph data is in any way invalid (it will attempt to
558558
correct the errors by inferring what the correct data should be).
559559
"""
560560
components, connections = await asyncio.gather(
561-
api.list_components(),
562-
api.list_connections(),
561+
client.list_components(),
562+
client.list_connections(),
563563
)
564564

565565
self.refresh_from(set(components), set(connections), correct_errors)

src/frequenz/sdk/microgrid/connection_manager.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def location(self) -> Location | None:
7676
the location of the microgrid if available, None otherwise.
7777
"""
7878

79-
async def _update_api(self, server_url: str) -> None:
79+
async def _update_client(self, server_url: str) -> None:
8080
self._server_url = server_url
8181

8282
@abstractmethod
@@ -98,8 +98,8 @@ def __init__(self, server_url: str) -> None:
9898
`grpc://localhost:1090?ssl=true`.
9999
"""
100100
super().__init__(server_url)
101-
self._api = MicrogridApiClient(server_url)
102-
# To create graph from the api we need await.
101+
self._client = MicrogridApiClient(server_url)
102+
# To create graph from the API client we need await.
103103
# So create empty graph here, and update it in `run` method.
104104
self._graph = _MicrogridComponentGraph()
105105

@@ -108,12 +108,8 @@ def __init__(self, server_url: str) -> None:
108108

109109
@property
110110
def api_client(self) -> MicrogridApiClient:
111-
"""Get the MicrogridApiClient.
112-
113-
Returns:
114-
api client
115-
"""
116-
return self._api
111+
"""The microgrid API client used by this connection manager."""
112+
return self._client
117113

118114
@property
119115
def microgrid_id(self) -> MicrogridId | None:
@@ -142,8 +138,8 @@ def component_graph(self) -> ComponentGraph:
142138
"""
143139
return self._graph
144140

145-
async def _update_api(self, server_url: str) -> None:
146-
"""Update api with new host and port.
141+
async def _update_client(self, server_url: str) -> None:
142+
"""Update the API client with a new server URL.
147143
148144
Args:
149145
server_url: The new location of the microgrid API server in the form of a
@@ -153,14 +149,14 @@ async def _update_api(self, server_url: str) -> None:
153149
a boolean (defaulting to false). For example:
154150
`grpc://localhost:1090?ssl=true`.
155151
"""
156-
await super()._update_api(server_url) # pylint: disable=protected-access
152+
await super()._update_client(server_url) # pylint: disable=protected-access
157153

158-
self._api = MicrogridApiClient(server_url)
154+
self._client = MicrogridApiClient(server_url)
159155
await self._initialize()
160156

161157
async def _initialize(self) -> None:
162-
self._metadata = await self._api.get_microgrid_info()
163-
await self._graph.refresh_from_api(self._api)
158+
self._metadata = await self._client.get_microgrid_info()
159+
await self._graph.refresh_from_client(self._client)
164160

165161

166162
_CONNECTION_MANAGER: ConnectionManager | None = None

tests/microgrid/test_graph.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,8 @@ def pretend_to_correct_errors(_g: gr._MicrogridComponentGraph) -> None:
950950
assert graph.connections() == {Connection(ComponentId(10), ComponentId(11))}
951951
graph.validate()
952952

953-
async def test_refresh_from_api(self) -> None:
954-
"""Test the refresh_from_api method."""
953+
async def test_refresh_from_client(self) -> None:
954+
"""Test the refresh_from_client method."""
955955
graph = gr._MicrogridComponentGraph()
956956
assert graph.components() == set()
957957
assert graph.connections() == set()
@@ -968,7 +968,7 @@ async def test_refresh_from_api(self) -> None:
968968

969969
# both components and connections must be non-empty
970970
with pytest.raises(gr.InvalidGraphError):
971-
await graph.refresh_from_api(client)
971+
await graph.refresh_from_client(client)
972972
assert graph.components() == set()
973973
assert graph.connections() == set()
974974
with pytest.raises(gr.InvalidGraphError):
@@ -978,7 +978,7 @@ async def test_refresh_from_api(self) -> None:
978978
Component(ComponentId(1), ComponentCategory.GRID)
979979
]
980980
with pytest.raises(gr.InvalidGraphError):
981-
await graph.refresh_from_api(client)
981+
await graph.refresh_from_client(client)
982982
assert graph.components() == set()
983983
assert graph.connections() == set()
984984
with pytest.raises(gr.InvalidGraphError):
@@ -987,7 +987,7 @@ async def test_refresh_from_api(self) -> None:
987987
client.list_components.return_value = []
988988
client.list_connections.return_value = [Connection(ComponentId(1), ComponentId(2))]
989989
with pytest.raises(gr.InvalidGraphError):
990-
await graph.refresh_from_api(client)
990+
await graph.refresh_from_client(client)
991991
assert graph.components() == set()
992992
assert graph.connections() == set()
993993
with pytest.raises(gr.InvalidGraphError):
@@ -1009,7 +1009,7 @@ async def test_refresh_from_api(self) -> None:
10091009
Connection(ComponentId(101), ComponentId(111)),
10101010
Connection(ComponentId(111), ComponentId(131)),
10111011
]
1012-
await graph.refresh_from_api(client)
1012+
await graph.refresh_from_client(client)
10131013

10141014
# Note: we need to add GriMetadata as a dict here, because that's what
10151015
# the ComponentGraph does too, and we need to be able to compare the
@@ -1053,7 +1053,7 @@ async def test_refresh_from_api(self) -> None:
10531053
Connection(ComponentId(727), ComponentId(737)),
10541054
Connection(ComponentId(717), ComponentId(747)),
10551055
]
1056-
await graph.refresh_from_api(client)
1056+
await graph.refresh_from_client(client)
10571057

10581058
expected = {
10591059
Component(

0 commit comments

Comments
 (0)