diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ef35dfe..384b0a2 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -14,6 +14,8 @@ * The `ReportingApiClient` exposes the option to resample the data, with its `resolution` represented in seconds and its default set to `None` (no resampling). +* An API key for authorization can now be passed to the `ReportingApiClient`. + ## Bug Fixes diff --git a/examples/client.py b/examples/client.py index 2a5fea2..ed436a7 100644 --- a/examples/client.py +++ b/examples/client.py @@ -54,6 +54,12 @@ def main() -> None: parser.add_argument( "--display", choices=["iter", "df", "dict"], help="Display format", default="df" ) + parser.add_argument( + "--key", + type=str, + help="API key", + default=None, + ) args = parser.parse_args() asyncio.run( run( @@ -65,6 +71,7 @@ def main() -> None: args.resolution, page_size=args.psize, service_address=args.url, + key=args.key, display=args.display, ) ) @@ -80,6 +87,7 @@ async def run( resolution: int, page_size: int, service_address: str, + key: str, display: str, ) -> None: """Test the ReportingApiClient. @@ -93,12 +101,13 @@ async def run( resolution: resampling resolution in sec page_size: page size service_address: service address + key: API key display: display format Raises: ValueError: if display format is invalid """ - client = ReportingApiClient(service_address) + client = ReportingApiClient(service_address, key) metrics = [Metric[mn] for mn in metric_names] diff --git a/src/frequenz/client/reporting/_client.py b/src/frequenz/client/reporting/_client.py index 635afb5..e451392 100644 --- a/src/frequenz/client/reporting/_client.py +++ b/src/frequenz/client/reporting/_client.py @@ -115,14 +115,16 @@ def next_page_token(self) -> str | None: class ReportingApiClient: """A client for the Reporting service.""" - def __init__(self, service_address: str): + def __init__(self, service_address: str, key: str | None = None) -> None: """Create a new Reporting client. Args: service_address: The address of the Reporting service. + key: The API key for the authorization. """ self._grpc_channel = grpcaio.insecure_channel(service_address) self._stub = ReportingStub(self._grpc_channel) + self._metadata = (("key", key),) if key else () # pylint: disable=too-many-arguments async def list_single_component_data( @@ -301,9 +303,12 @@ async def _fetch_page( filter=list_filter, pagination_params=pagination_params, ) + print(self._metadata) response = await cast( Awaitable[PBListMicrogridComponentsDataResponse], - self._stub.ListMicrogridComponentsData(request), + self._stub.ListMicrogridComponentsData( + request, metadata=self._metadata + ), ) except grpcaio.AioRpcError as e: print(f"RPC failed: {e}")