Skip to content

feat: expose async client #1250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: expose async client
Expose a property that returns the underlying async Spanner gRPC client.
  • Loading branch information
olavloite committed Dec 4, 2024
commit f77b2b34b724b6e84116d3f38e54c5f9b8506c72
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
Loading