Skip to content

Commit

Permalink
feat: expose async client
Browse files Browse the repository at this point in the history
Expose a property that returns the underlying async Spanner gRPC client.
  • Loading branch information
olavloite committed Dec 4, 2024
1 parent 5e8ca94 commit f77b2b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions google/cloud/spanner_v1/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from google.cloud.spanner_v1 import TransactionSelector
from google.cloud.spanner_v1 import TransactionOptions
from google.cloud.spanner_v1 import RequestOptions
from google.cloud.spanner_v1 import SpannerAsyncClient
from google.cloud.spanner_v1 import SpannerClient
from google.cloud.spanner_v1._helpers import _merge_query_options
from google.cloud.spanner_v1._helpers import (
Expand Down Expand Up @@ -143,6 +144,7 @@ class Database(object):
"""

_spanner_api = None
_spanner_async_api: SpannerAsyncClient = None

def __init__(
self,
Expand Down Expand Up @@ -438,6 +440,28 @@ def spanner_api(self):
)
return self._spanner_api

@property
def spanner_async_api(self):
if self._spanner_async_api is None:
client_info = self._instance._client._client_info
client_options = self._instance._client._client_options
if self._instance.emulator_host is not None:
channel = grpc.aio.insecure_channel(target=self._instance.emulator_host)
transport = SpannerGrpcTransport(channel=channel)
self._spanner_async_api = SpannerAsyncClient(
client_info=client_info, transport=transport
)
return self._spanner_async_api
credentials = self._instance._client.credentials
if isinstance(credentials, google.auth.credentials.Scoped):
credentials = credentials.with_scopes((SPANNER_DATA_SCOPE,))
self._spanner_async_api = SpannerAsyncClient(
credentials=credentials,
client_info=client_info,
client_options=client_options,
)
return self._spanner_async_api

def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(
if client_cert_source:
warnings.warn("client_cert_source is deprecated", DeprecationWarning)

if isinstance(channel, grpc.Channel):
if isinstance(channel, grpc.Channel) or isinstance(channel, grpc.aio.Channel):
# Ignore credentials if a channel was passed.
credentials = None
self._ignore_credentials = True
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
ISORT_VERSION = "isort==5.11.0"
LINT_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]

DEFAULT_PYTHON_VERSION = "3.8"
DEFAULT_PYTHON_VERSION = "3.12"

UNIT_TEST_PYTHON_VERSIONS: List[str] = [
"3.7",
Expand Down

0 comments on commit f77b2b3

Please sign in to comment.