Skip to content
Permalink
Browse files
feat: add context manager support in client (#637)
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 408420890

Source-Link: googleapis/googleapis@2921f9f

Source-Link: https://github.com/googleapis/googleapis-gen/commit/6598ca8cbbf5226733a099c4506518a5af6ff74c
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNjU5OGNhOGNiYmY1MjI2NzMzYTA5OWM0NTA2NTE4YTVhZjZmZjc0YyJ9

docs: list oneofs in docstring
fix(deps): require google-api-core >= 1.28.0
fix(deps): drop packaging dependency
feat: add context manager support in client
chore: fix docstring for first attribute of protos
fix: improper types in pagers generation
chore: use gapic-generator-python 0.56.2
  • Loading branch information
gcf-owl-bot[bot] committed Nov 10, 2021
1 parent 5e0c364 commit 5ae4be8ce0a429b33b31a119d7079ce4deb50ca2
Showing with 1,195 additions and 913 deletions.
  1. +68 −55 google/cloud/spanner_admin_database_v1/services/database_admin/async_client.py
  2. +86 −63 google/cloud/spanner_admin_database_v1/services/database_admin/client.py
  3. +18 −18 google/cloud/spanner_admin_database_v1/services/database_admin/pagers.py
  4. +17 −41 google/cloud/spanner_admin_database_v1/services/database_admin/transports/base.py
  5. +10 −7 google/cloud/spanner_admin_database_v1/services/database_admin/transports/grpc.py
  6. +10 −8 google/cloud/spanner_admin_database_v1/services/database_admin/transports/grpc_asyncio.py
  7. +3 −0 google/cloud/spanner_admin_database_v1/types/backup.py
  8. +1 −0 google/cloud/spanner_admin_database_v1/types/common.py
  9. +17 −0 google/cloud/spanner_admin_database_v1/types/spanner_database_admin.py
  10. +45 −34 google/cloud/spanner_admin_instance_v1/services/instance_admin/async_client.py
  11. +63 −42 google/cloud/spanner_admin_instance_v1/services/instance_admin/client.py
  12. +10 −10 google/cloud/spanner_admin_instance_v1/services/instance_admin/pagers.py
  13. +17 −41 google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/base.py
  14. +10 −7 google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/grpc.py
  15. +10 −8 google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/grpc_asyncio.py
  16. +1 −0 google/cloud/spanner_admin_instance_v1/types/spanner_instance_admin.py
  17. +60 −49 google/cloud/spanner_v1/services/spanner/async_client.py
  18. +78 −57 google/cloud/spanner_v1/services/spanner/client.py
  19. +6 −6 google/cloud/spanner_v1/services/spanner/pagers.py
  20. +15 −39 google/cloud/spanner_v1/services/spanner/transports/base.py
  21. +8 −5 google/cloud/spanner_v1/services/spanner/transports/grpc.py
  22. +8 −6 google/cloud/spanner_v1/services/spanner/transports/grpc_asyncio.py
  23. +2 −0 google/cloud/spanner_v1/types/commit_response.py
  24. +15 −0 google/cloud/spanner_v1/types/keys.py
  25. +18 −0 google/cloud/spanner_v1/types/mutation.py
  26. +11 −0 google/cloud/spanner_v1/types/result_set.py
  27. +18 −0 google/cloud/spanner_v1/types/spanner.py
  28. +47 −2 google/cloud/spanner_v1/types/transaction.py
  29. +1 −0 google/cloud/spanner_v1/types/type.py
  30. +18 −18 scripts/fixup_spanner_admin_database_v1_keywords.py
  31. +11 −11 scripts/fixup_spanner_admin_instance_v1_keywords.py
  32. +16 −16 scripts/fixup_spanner_v1_keywords.py
  33. +203 −139 tests/unit/gapic/spanner_admin_database_v1/test_database_admin.py
  34. +143 −119 tests/unit/gapic/spanner_admin_instance_v1/test_instance_admin.py
  35. +131 −112 tests/unit/gapic/spanner_v1/test_spanner.py

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -15,13 +15,13 @@
#
from typing import (
Any,
AsyncIterable,
AsyncIterator,
Awaitable,
Callable,
Iterable,
Sequence,
Tuple,
Optional,
Iterator,
)

from google.cloud.spanner_admin_database_v1.types import backup
@@ -76,14 +76,14 @@ def __getattr__(self, name: str) -> Any:
return getattr(self._response, name)

@property
def pages(self) -> Iterable[spanner_database_admin.ListDatabasesResponse]:
def pages(self) -> Iterator[spanner_database_admin.ListDatabasesResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = self._method(self._request, metadata=self._metadata)
yield self._response

def __iter__(self) -> Iterable[spanner_database_admin.Database]:
def __iter__(self) -> Iterator[spanner_database_admin.Database]:
for page in self.pages:
yield from page.databases

@@ -140,14 +140,14 @@ def __getattr__(self, name: str) -> Any:
@property
async def pages(
self,
) -> AsyncIterable[spanner_database_admin.ListDatabasesResponse]:
) -> AsyncIterator[spanner_database_admin.ListDatabasesResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = await self._method(self._request, metadata=self._metadata)
yield self._response

def __aiter__(self) -> AsyncIterable[spanner_database_admin.Database]:
def __aiter__(self) -> AsyncIterator[spanner_database_admin.Database]:
async def async_generator():
async for page in self.pages:
for response in page.databases:
@@ -206,14 +206,14 @@ def __getattr__(self, name: str) -> Any:
return getattr(self._response, name)

@property
def pages(self) -> Iterable[backup.ListBackupsResponse]:
def pages(self) -> Iterator[backup.ListBackupsResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = self._method(self._request, metadata=self._metadata)
yield self._response

def __iter__(self) -> Iterable[backup.Backup]:
def __iter__(self) -> Iterator[backup.Backup]:
for page in self.pages:
yield from page.backups

@@ -268,14 +268,14 @@ def __getattr__(self, name: str) -> Any:
return getattr(self._response, name)

@property
async def pages(self) -> AsyncIterable[backup.ListBackupsResponse]:
async def pages(self) -> AsyncIterator[backup.ListBackupsResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = await self._method(self._request, metadata=self._metadata)
yield self._response

def __aiter__(self) -> AsyncIterable[backup.Backup]:
def __aiter__(self) -> AsyncIterator[backup.Backup]:
async def async_generator():
async for page in self.pages:
for response in page.backups:
@@ -334,14 +334,14 @@ def __getattr__(self, name: str) -> Any:
return getattr(self._response, name)

@property
def pages(self) -> Iterable[spanner_database_admin.ListDatabaseOperationsResponse]:
def pages(self) -> Iterator[spanner_database_admin.ListDatabaseOperationsResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = self._method(self._request, metadata=self._metadata)
yield self._response

def __iter__(self) -> Iterable[operations_pb2.Operation]:
def __iter__(self) -> Iterator[operations_pb2.Operation]:
for page in self.pages:
yield from page.operations

@@ -400,14 +400,14 @@ def __getattr__(self, name: str) -> Any:
@property
async def pages(
self,
) -> AsyncIterable[spanner_database_admin.ListDatabaseOperationsResponse]:
) -> AsyncIterator[spanner_database_admin.ListDatabaseOperationsResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = await self._method(self._request, metadata=self._metadata)
yield self._response

def __aiter__(self) -> AsyncIterable[operations_pb2.Operation]:
def __aiter__(self) -> AsyncIterator[operations_pb2.Operation]:
async def async_generator():
async for page in self.pages:
for response in page.operations:
@@ -466,14 +466,14 @@ def __getattr__(self, name: str) -> Any:
return getattr(self._response, name)

@property
def pages(self) -> Iterable[backup.ListBackupOperationsResponse]:
def pages(self) -> Iterator[backup.ListBackupOperationsResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = self._method(self._request, metadata=self._metadata)
yield self._response

def __iter__(self) -> Iterable[operations_pb2.Operation]:
def __iter__(self) -> Iterator[operations_pb2.Operation]:
for page in self.pages:
yield from page.operations

@@ -528,14 +528,14 @@ def __getattr__(self, name: str) -> Any:
return getattr(self._response, name)

@property
async def pages(self) -> AsyncIterable[backup.ListBackupOperationsResponse]:
async def pages(self) -> AsyncIterator[backup.ListBackupOperationsResponse]:
yield self._response
while self._response.next_page_token:
self._request.page_token = self._response.next_page_token
self._response = await self._method(self._request, metadata=self._metadata)
yield self._response

def __aiter__(self) -> AsyncIterable[operations_pb2.Operation]:
def __aiter__(self) -> AsyncIterator[operations_pb2.Operation]:
async def async_generator():
async for page in self.pages:
for response in page.operations:
@@ -15,15 +15,14 @@
#
import abc
from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
import packaging.version
import pkg_resources

import google.auth # type: ignore
import google.api_core # type: ignore
from google.api_core import exceptions as core_exceptions # type: ignore
from google.api_core import gapic_v1 # type: ignore
from google.api_core import retry as retries # type: ignore
from google.api_core import operations_v1 # type: ignore
import google.api_core
from google.api_core import exceptions as core_exceptions
from google.api_core import gapic_v1
from google.api_core import retry as retries
from google.api_core import operations_v1
from google.auth import credentials as ga_credentials # type: ignore
from google.oauth2 import service_account # type: ignore

@@ -44,15 +43,6 @@
except pkg_resources.DistributionNotFound:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()

try:
# google.auth.__version__ was added in 1.26.0
_GOOGLE_AUTH_VERSION = google.auth.__version__
except AttributeError:
try: # try pkg_resources if it is available
_GOOGLE_AUTH_VERSION = pkg_resources.get_distribution("google-auth").version
except pkg_resources.DistributionNotFound: # pragma: NO COVER
_GOOGLE_AUTH_VERSION = None


class DatabaseAdminTransport(abc.ABC):
"""Abstract transport class for DatabaseAdmin."""
@@ -105,7 +95,7 @@ def __init__(
host += ":443"
self._host = host

scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)
scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}

# Save the scopes.
self._scopes = scopes
@@ -127,7 +117,7 @@ def __init__(
**scopes_kwargs, quota_project_id=quota_project_id
)

# If the credentials is service account credentials, then always try to use self signed JWT.
# If the credentials are service account credentials, then always try to use self signed JWT.
if (
always_use_jwt_access
and isinstance(credentials, service_account.Credentials)
@@ -138,29 +128,6 @@ def __init__(
# Save the credentials.
self._credentials = credentials

# TODO(busunkim): This method is in the base transport
# to avoid duplicating code across the transport classes. These functions
# should be deleted once the minimum required versions of google-auth is increased.

# TODO: Remove this function once google-auth >= 1.25.0 is required
@classmethod
def _get_scopes_kwargs(
cls, host: str, scopes: Optional[Sequence[str]]
) -> Dict[str, Optional[Sequence[str]]]:
"""Returns scopes kwargs to pass to google-auth methods depending on the google-auth version"""

scopes_kwargs = {}

if _GOOGLE_AUTH_VERSION and (
packaging.version.parse(_GOOGLE_AUTH_VERSION)
>= packaging.version.parse("1.25.0")
):
scopes_kwargs = {"scopes": scopes, "default_scopes": cls.AUTH_SCOPES}
else:
scopes_kwargs = {"scopes": scopes or cls.AUTH_SCOPES}

return scopes_kwargs

def _prep_wrapped_messages(self, client_info):
# Precompute the wrapped methods.
self._wrapped_methods = {
@@ -363,8 +330,17 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def operations_client(self) -> operations_v1.OperationsClient:
def operations_client(self):
"""Return the client designed to process long-running operations."""
raise NotImplementedError()

@@ -16,9 +16,9 @@
import warnings
from typing import Callable, Dict, Optional, Sequence, Tuple, Union

from google.api_core import grpc_helpers # type: ignore
from google.api_core import operations_v1 # type: ignore
from google.api_core import gapic_v1 # type: ignore
from google.api_core import grpc_helpers
from google.api_core import operations_v1
from google.api_core import gapic_v1
import google.auth # type: ignore
from google.auth import credentials as ga_credentials # type: ignore
from google.auth.transport.grpc import SslCredentials # type: ignore
@@ -92,16 +92,16 @@ def __init__(
api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
If provided, it overrides the ``host`` argument and tries to create
a mutual TLS channel with client SSL credentials from
``client_cert_source`` or applicatin default SSL credentials.
``client_cert_source`` or application default SSL credentials.
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]):
Deprecated. A callback to provide client SSL certificate bytes and
private key bytes, both in PEM format. It is ignored if
``api_mtls_endpoint`` is None.
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
for grpc channel. It is ignored if ``channel`` is provided.
for the grpc channel. It is ignored if ``channel`` is provided.
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
A callback to provide client certificate bytes and private key bytes,
both in PEM format. It is used to configure mutual TLS channel. It is
both in PEM format. It is used to configure a mutual TLS channel. It is
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
@@ -122,7 +122,7 @@ def __init__(
self._grpc_channel = None
self._ssl_channel_credentials = ssl_channel_credentials
self._stubs: Dict[str, Callable] = {}
self._operations_client = None
self._operations_client: Optional[operations_v1.OperationsClient] = None

if api_mtls_endpoint:
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
@@ -815,5 +815,8 @@ def list_backup_operations(
)
return self._stubs["list_backup_operations"]

def close(self):
self.grpc_channel.close()


__all__ = ("DatabaseAdminGrpcTransport",)
@@ -16,12 +16,11 @@
import warnings
from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union

from google.api_core import gapic_v1 # type: ignore
from google.api_core import grpc_helpers_async # type: ignore
from google.api_core import operations_v1 # type: ignore
from google.api_core import gapic_v1
from google.api_core import grpc_helpers_async
from google.api_core import operations_v1
from google.auth import credentials as ga_credentials # type: ignore
from google.auth.transport.grpc import SslCredentials # type: ignore
import packaging.version

import grpc # type: ignore
from grpc.experimental import aio # type: ignore
@@ -139,16 +138,16 @@ def __init__(
api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
If provided, it overrides the ``host`` argument and tries to create
a mutual TLS channel with client SSL credentials from
``client_cert_source`` or applicatin default SSL credentials.
``client_cert_source`` or application default SSL credentials.
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]):
Deprecated. A callback to provide client SSL certificate bytes and
private key bytes, both in PEM format. It is ignored if
``api_mtls_endpoint`` is None.
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
for grpc channel. It is ignored if ``channel`` is provided.
for the grpc channel. It is ignored if ``channel`` is provided.
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
A callback to provide client certificate bytes and private key bytes,
both in PEM format. It is used to configure mutual TLS channel. It is
both in PEM format. It is used to configure a mutual TLS channel. It is
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
@@ -169,7 +168,7 @@ def __init__(
self._grpc_channel = None
self._ssl_channel_credentials = ssl_channel_credentials
self._stubs: Dict[str, Callable] = {}
self._operations_client = None
self._operations_client: Optional[operations_v1.OperationsAsyncClient] = None

if api_mtls_endpoint:
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
@@ -833,5 +832,8 @@ def list_backup_operations(
)
return self._stubs["list_backup_operations"]

def close(self):
return self.grpc_channel.close()


__all__ = ("DatabaseAdminGrpcAsyncIOTransport",)
@@ -42,6 +42,7 @@

class Backup(proto.Message):
r"""A backup of a Cloud Spanner database.
Attributes:
database (str):
Required for the
@@ -461,6 +462,7 @@ def raw_page(self):

class BackupInfo(proto.Message):
r"""Information about a backup.
Attributes:
backup (str):
Name of the backup.
@@ -491,6 +493,7 @@ class BackupInfo(proto.Message):

class CreateBackupEncryptionConfig(proto.Message):
r"""Encryption configuration for the backup to create.
Attributes:
encryption_type (google.cloud.spanner_admin_database_v1.types.CreateBackupEncryptionConfig.EncryptionType):
Required. The encryption type of the backup.
@@ -47,6 +47,7 @@ class OperationProgress(proto.Message):

class EncryptionConfig(proto.Message):
r"""Encryption configuration for a Cloud Spanner database.
Attributes:
kms_key_name (str):
The Cloud KMS key to be used for encrypting and decrypting

0 comments on commit 5ae4be8

Please sign in to comment.