Skip to content

Commit

Permalink
Support authorization via API key
Browse files Browse the repository at this point in the history
  • Loading branch information
cwasicki committed Jun 5, 2024
1 parent 4ec5c26 commit 85f6bc7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ 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 +68,7 @@ def main() -> None:
args.resolution,
page_size=args.psize,
service_address=args.url,
key=args.key,
display=args.display,
)
)
Expand All @@ -80,6 +84,7 @@ async def run(
resolution: int,
page_size: int,
service_address: str,
key: str,
display: str,
) -> None:
"""Test the ReportingApiClient.
Expand All @@ -93,12 +98,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
6 changes: 4 additions & 2 deletions src/frequenz/client/reporting/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ 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:
"""Create a new Reporting client.
Args:
service_address: The address of the Reporting service.
"""
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 +302,10 @@ 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 85f6bc7

Please sign in to comment.