-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
service: Add generic create_session(s) API (#426)
* service: Add a helper function to get the gRPC device channel * tests: Add tests for get_insecure_grpc_device_channel * service: Add closing_session helper function * tests: Add unit tests for closing_session * service: Add generic create_session(s) methods * tests: Add tests for create_session(s) * service: Add TypedSessionInformation * service: Remove an unnecessary ExitStack * tests: Add comment about api_key * tests: Rename a couple test cases * service: Extract _get_matching_session_infos and refactor _with_session * tests: Update test cases * tests: Add a type-checking test case * service: Make instrument_type_id required for generic create_session(s) * tests: Update grpcdevice tests * service: Remove reference to session tuple * service: Update docstrings
- Loading branch information
Showing
9 changed files
with
775 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Shared code for interfacing with driver APIs.""" | ||
from __future__ import annotations | ||
|
||
import contextlib | ||
from typing import ContextManager, TypeVar | ||
|
||
TSession = TypeVar("TSession") | ||
|
||
|
||
def closing_session(session: TSession) -> ContextManager[TSession]: | ||
"""Create a context manager that closes the session. | ||
Args: | ||
session: A driver session. | ||
Returns: | ||
A context manager that yields the session and closes it. | ||
""" | ||
if isinstance(session, contextlib.AbstractContextManager): | ||
# Assume the session yields itself. | ||
return session | ||
elif hasattr(session, "close"): | ||
return contextlib.closing(session) | ||
else: | ||
raise TypeError( | ||
f"Invalid session type '{type(session)}'. A session must be a context manager and/or " | ||
"have a close() method." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Shared functions for interacting with NI gRPC Device Server.""" | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
import grpc | ||
|
||
from ni_measurementlink_service._channelpool import GrpcChannelPool | ||
from ni_measurementlink_service._configuration import ( | ||
GRPC_DEVICE_ADDRESS, | ||
USE_GRPC_DEVICE_SERVER, | ||
) | ||
from ni_measurementlink_service._internal.discovery_client import DiscoveryClient | ||
|
||
SERVICE_CLASS = "ni.measurementlink.v1.grpcdeviceserver" | ||
"""The service class for NI gRPC Device Server.""" | ||
|
||
|
||
def get_insecure_grpc_device_channel( | ||
discovery_client: DiscoveryClient, | ||
grpc_channel_pool: GrpcChannelPool, | ||
provided_interface: str, | ||
) -> Optional[grpc.Channel]: | ||
"""Get an unencrypted gRPC channel targeting NI gRPC Device Server. | ||
Args: | ||
discovery_client: The discovery client. | ||
grpc_channel_pool: The gRPC channel pool. | ||
provided_interface: The driver API's NI gRPC Device Server interface | ||
name. | ||
Returns: | ||
A gRPC channel targeting the NI gRPC Device Server, or ``None`` if the | ||
configuration file specifies that ``USE_GRPC_DEVICE_SERVER`` is false. | ||
""" | ||
if not USE_GRPC_DEVICE_SERVER: | ||
return None | ||
|
||
address = GRPC_DEVICE_ADDRESS | ||
if not address: | ||
service_location = discovery_client.resolve_service( | ||
provided_interface=provided_interface, | ||
service_class=SERVICE_CLASS, | ||
) | ||
address = service_location.insecure_address | ||
|
||
return grpc_channel_pool.get_channel(address) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Unit tests for ni_measurementlink_service._drivers.""" |
Oops, something went wrong.