Skip to content

Commit

Permalink
Support authorization via API key
Browse files Browse the repository at this point in the history
An API key can be passed to the client to authorize access to the user's
data.

Signed-off-by: cwasicki <[email protected]>
  • Loading branch information
cwasicki committed Jun 5, 2024
1 parent 4ec5c26 commit 0e08949
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

## New Features

* The `ReportingApiClient` exposes the resampling option of the data, with its
* The `ReportingApiClient` exposes the option to resample the data, with its
resolution represented in second and its default set to `None.

* An API key for authorization can now be passed to the `ReportingApiClient`.

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
11 changes: 10 additions & 1 deletion examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -65,6 +71,7 @@ def main() -> None:
args.resolution,
page_size=args.psize,
service_address=args.url,
key=args.key,
display=args.display,
)
)
Expand All @@ -80,6 +87,7 @@ async def run(
resolution: int,
page_size: int,
service_address: str,
key: str,
display: str,
) -> None:
"""Test the ReportingApiClient.
Expand All @@ -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]

Expand Down
9 changes: 7 additions & 2 deletions src/frequenz/client/reporting/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit 0e08949

Please sign in to comment.