Skip to content

Commit 9a33bf6

Browse files
authored
Support authorization via API key (#63)
An API key can be passed to the client to authorize access to the user's data.
2 parents ce65291 + 993b161 commit 9a33bf6

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
## New Features
1313

14-
* The `ReportingApiClient` exposes the resampling option of the data, with its
15-
resolution represented in second and its default set to `None`.
14+
* An API key for authorization can now be passed to the `ReportingApiClient`.
1615

1716
## Bug Fixes
1817

examples/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def main() -> None:
5454
parser.add_argument(
5555
"--display", choices=["iter", "df", "dict"], help="Display format", default="df"
5656
)
57+
parser.add_argument(
58+
"--key",
59+
type=str,
60+
help="API key",
61+
default=None,
62+
)
5763
args = parser.parse_args()
5864
asyncio.run(
5965
run(
@@ -65,6 +71,7 @@ def main() -> None:
6571
args.resolution,
6672
page_size=args.psize,
6773
service_address=args.url,
74+
key=args.key,
6875
display=args.display,
6976
)
7077
)
@@ -80,6 +87,7 @@ async def run(
8087
resolution: int,
8188
page_size: int,
8289
service_address: str,
90+
key: str,
8391
display: str,
8492
) -> None:
8593
"""Test the ReportingApiClient.
@@ -93,12 +101,13 @@ async def run(
93101
resolution: resampling resolution in sec
94102
page_size: page size
95103
service_address: service address
104+
key: API key
96105
display: display format
97106
98107
Raises:
99108
ValueError: if display format is invalid
100109
"""
101-
client = ReportingApiClient(service_address)
110+
client = ReportingApiClient(service_address, key)
102111

103112
metrics = [Metric[mn] for mn in metric_names]
104113

src/frequenz/client/reporting/_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ def next_page_token(self) -> str | None:
115115
class ReportingApiClient:
116116
"""A client for the Reporting service."""
117117

118-
def __init__(self, service_address: str):
118+
def __init__(self, service_address: str, key: str | None = None) -> None:
119119
"""Create a new Reporting client.
120120
121121
Args:
122122
service_address: The address of the Reporting service.
123+
key: The API key for the authorization.
123124
"""
124125
self._grpc_channel = grpcaio.insecure_channel(service_address)
125126
self._stub = ReportingStub(self._grpc_channel)
127+
self._metadata = (("key", key),) if key else ()
126128

127129
# pylint: disable=too-many-arguments
128130
async def list_single_component_data(
@@ -303,7 +305,9 @@ async def _fetch_page(
303305
)
304306
response = await cast(
305307
Awaitable[PBListMicrogridComponentsDataResponse],
306-
self._stub.ListMicrogridComponentsData(request),
308+
self._stub.ListMicrogridComponentsData(
309+
request, metadata=self._metadata
310+
),
307311
)
308312
except grpcaio.AioRpcError as e:
309313
print(f"RPC failed: {e}")

0 commit comments

Comments
 (0)