From 2900354ba94f7b71039ed3669f5d82261d2745eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Wed, 4 Dec 2024 14:52:59 +0100 Subject: [PATCH] feat: expose async client Expose a property that returns the underlying async Spanner gRPC client. --- google/cloud/spanner_v1/database.py | 24 ++++++++++++++++++++++++ noxfile.py | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/google/cloud/spanner_v1/database.py b/google/cloud/spanner_v1/database.py index abddd5d97d..1080ed5892 100644 --- a/google/cloud/spanner_v1/database.py +++ b/google/cloud/spanner_v1/database.py @@ -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 ( @@ -143,6 +144,7 @@ class Database(object): """ _spanner_api = None + _spanner_async_api: SpannerAsyncClient = None def __init__( self, @@ -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 diff --git a/noxfile.py b/noxfile.py index f5a2761d73..451d099612 100644 --- a/noxfile.py +++ b/noxfile.py @@ -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",