diff --git a/.gitignore b/.gitignore index 52b77d7f42..b87e1ed580 100644 --- a/.gitignore +++ b/.gitignore @@ -29,7 +29,6 @@ pip-log.txt .nox .cache .pytest_cache -.pytype # Mac diff --git a/google/cloud/firestore_admin_v1/services/firestore_admin/async_client.py b/google/cloud/firestore_admin_v1/services/firestore_admin/async_client.py index 7e7dcc3f65..7cbbf204c9 100644 --- a/google/cloud/firestore_admin_v1/services/firestore_admin/async_client.py +++ b/google/cloud/firestore_admin_v1/services/firestore_admin/async_client.py @@ -28,8 +28,8 @@ from google.auth import credentials # type: ignore from google.oauth2 import service_account # type: ignore -from google.api_core import operation as ga_operation # type: ignore -from google.api_core import operation_async # type: ignore +from google.api_core import operation as ga_operation +from google.api_core import operation_async from google.cloud.firestore_admin_v1.services.firestore_admin import pagers from google.cloud.firestore_admin_v1.types import field from google.cloud.firestore_admin_v1.types import field as gfa_field @@ -39,7 +39,7 @@ from google.cloud.firestore_admin_v1.types import operation as gfa_operation from google.protobuf import empty_pb2 as empty # type: ignore -from .transports.base import FirestoreAdminTransport +from .transports.base import FirestoreAdminTransport, DEFAULT_CLIENT_INFO from .transports.grpc_asyncio import FirestoreAdminGrpcAsyncIOTransport from .client import FirestoreAdminClient @@ -71,6 +71,7 @@ def __init__( credentials: credentials.Credentials = None, transport: Union[str, FirestoreAdminTransport] = "grpc_asyncio", client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the firestore admin client. @@ -103,7 +104,10 @@ def __init__( """ self._client = FirestoreAdminClient( - credentials=credentials, transport=transport, client_options=client_options, + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, ) async def create_index( @@ -179,7 +183,7 @@ async def create_index( rpc = gapic_v1.method_async.wrap_method( self._client._transport.create_index, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -261,7 +265,7 @@ async def list_indexes( rpc = gapic_v1.method_async.wrap_method( self._client._transport.list_indexes, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -339,7 +343,7 @@ async def get_index( rpc = gapic_v1.method_async.wrap_method( self._client._transport.get_index, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -404,7 +408,7 @@ async def delete_index( rpc = gapic_v1.method_async.wrap_method( self._client._transport.delete_index, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -477,7 +481,7 @@ async def get_field( rpc = gapic_v1.method_async.wrap_method( self._client._transport.get_field, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -569,7 +573,7 @@ async def update_field( rpc = gapic_v1.method_async.wrap_method( self._client._transport.update_field, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -660,7 +664,7 @@ async def list_fields( rpc = gapic_v1.method_async.wrap_method( self._client._transport.list_fields, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -751,7 +755,7 @@ async def export_documents( rpc = gapic_v1.method_async.wrap_method( self._client._transport.export_documents, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -851,7 +855,7 @@ async def import_documents( rpc = gapic_v1.method_async.wrap_method( self._client._transport.import_documents, default_timeout=None, - client_info=_client_info, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -876,11 +880,11 @@ async def import_documents( try: - _client_info = gapic_v1.client_info.ClientInfo( + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution("google-cloud-firestore",).version, ) except pkg_resources.DistributionNotFound: - _client_info = gapic_v1.client_info.ClientInfo() + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() __all__ = ("FirestoreAdminAsyncClient",) diff --git a/google/cloud/firestore_admin_v1/services/firestore_admin/client.py b/google/cloud/firestore_admin_v1/services/firestore_admin/client.py index b88b18dfb4..9360f86fa2 100644 --- a/google/cloud/firestore_admin_v1/services/firestore_admin/client.py +++ b/google/cloud/firestore_admin_v1/services/firestore_admin/client.py @@ -30,9 +30,9 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore -from google.api_core import operation as ga_operation # type: ignore -from google.api_core import operation # type: ignore -from google.api_core import operation_async # type: ignore +from google.api_core import operation as ga_operation +from google.api_core import operation +from google.api_core import operation_async from google.cloud.firestore_admin_v1.services.firestore_admin import pagers from google.cloud.firestore_admin_v1.types import field from google.cloud.firestore_admin_v1.types import field as gfa_field @@ -42,7 +42,7 @@ from google.cloud.firestore_admin_v1.types import operation as gfa_operation from google.protobuf import empty_pb2 as empty # type: ignore -from .transports.base import FirestoreAdminTransport +from .transports.base import FirestoreAdminTransport, DEFAULT_CLIENT_INFO from .transports.grpc import FirestoreAdminGrpcTransport from .transports.grpc_asyncio import FirestoreAdminGrpcAsyncIOTransport @@ -177,6 +177,7 @@ def __init__( credentials: credentials.Credentials = None, transport: Union[str, FirestoreAdminTransport] = None, client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the firestore admin client. @@ -202,6 +203,11 @@ def __init__( (2) The ``client_cert_source`` property is used to provide client SSL credentials for mutual TLS transport. If not provided, the default SSL credentials will be used if present. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport @@ -258,6 +264,8 @@ def __init__( scopes=client_options.scopes, api_mtls_endpoint=client_options.api_endpoint, client_cert_source=client_options.client_cert_source, + quota_project_id=client_options.quota_project_id, + client_info=client_info, ) def create_index( @@ -312,29 +320,31 @@ def create_index( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([parent, index]): + has_flattened_params = any([parent, index]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.CreateIndexRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.CreateIndexRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.CreateIndexRequest): + request = firestore_admin.CreateIndexRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if parent is not None: - request.parent = parent - if index is not None: - request.index = index + if parent is not None: + request.parent = parent + if index is not None: + request.index = index # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.create_index, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.create_index] # Certain fields should be provided within the metadata header; # add these here. @@ -396,27 +406,29 @@ def list_indexes( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([parent]): + has_flattened_params = any([parent]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.ListIndexesRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.ListIndexesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.ListIndexesRequest): + request = firestore_admin.ListIndexesRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if parent is not None: - request.parent = parent + if parent is not None: + request.parent = parent # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.list_indexes, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.list_indexes] # Certain fields should be provided within the metadata header; # add these here. @@ -474,25 +486,29 @@ def get_index( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([name]): + has_flattened_params = any([name]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.GetIndexRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.GetIndexRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.GetIndexRequest): + request = firestore_admin.GetIndexRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if name is not None: - request.name = name + if name is not None: + request.name = name # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.get_index, default_timeout=None, client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.get_index] # Certain fields should be provided within the metadata header; # add these here. @@ -537,27 +553,29 @@ def delete_index( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([name]): + has_flattened_params = any([name]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.DeleteIndexRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.DeleteIndexRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.DeleteIndexRequest): + request = firestore_admin.DeleteIndexRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if name is not None: - request.name = name + if name is not None: + request.name = name # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.delete_index, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.delete_index] # Certain fields should be provided within the metadata header; # add these here. @@ -610,25 +628,29 @@ def get_field( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([name]): + has_flattened_params = any([name]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.GetFieldRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.GetFieldRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.GetFieldRequest): + request = firestore_admin.GetFieldRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if name is not None: - request.name = name + if name is not None: + request.name = name # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.get_field, default_timeout=None, client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.get_field] # Certain fields should be provided within the metadata header; # add these here. @@ -700,27 +722,29 @@ def update_field( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([field]): + has_flattened_params = any([field]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.UpdateFieldRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.UpdateFieldRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.UpdateFieldRequest): + request = firestore_admin.UpdateFieldRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if field is not None: - request.field = field + if field is not None: + request.field = field # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.update_field, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.update_field] # Certain fields should be provided within the metadata header; # add these here. @@ -791,25 +815,29 @@ def list_fields( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([parent]): + has_flattened_params = any([parent]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.ListFieldsRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.ListFieldsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.ListFieldsRequest): + request = firestore_admin.ListFieldsRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if parent is not None: - request.parent = parent + if parent is not None: + request.parent = parent # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.list_fields, default_timeout=None, client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.list_fields] # Certain fields should be provided within the metadata header; # add these here. @@ -880,27 +908,29 @@ def export_documents( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([name]): + has_flattened_params = any([name]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.ExportDocumentsRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.ExportDocumentsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.ExportDocumentsRequest): + request = firestore_admin.ExportDocumentsRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if name is not None: - request.name = name + if name is not None: + request.name = name # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.export_documents, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.export_documents] # Certain fields should be provided within the metadata header; # add these here. @@ -980,27 +1010,29 @@ def import_documents( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([name]): + has_flattened_params = any([name]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = firestore_admin.ImportDocumentsRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a firestore_admin.ImportDocumentsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, firestore_admin.ImportDocumentsRequest): + request = firestore_admin.ImportDocumentsRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if name is not None: - request.name = name + if name is not None: + request.name = name # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.import_documents, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.import_documents] # Certain fields should be provided within the metadata header; # add these here. @@ -1024,11 +1056,11 @@ def import_documents( try: - _client_info = gapic_v1.client_info.ClientInfo( + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution("google-cloud-firestore",).version, ) except pkg_resources.DistributionNotFound: - _client_info = gapic_v1.client_info.ClientInfo() + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() __all__ = ("FirestoreAdminClient",) diff --git a/google/cloud/firestore_admin_v1/services/firestore_admin/transports/base.py b/google/cloud/firestore_admin_v1/services/firestore_admin/transports/base.py index ee9ce819e4..2e8b631069 100644 --- a/google/cloud/firestore_admin_v1/services/firestore_admin/transports/base.py +++ b/google/cloud/firestore_admin_v1/services/firestore_admin/transports/base.py @@ -17,9 +17,12 @@ import abc import typing +import pkg_resources -from google import auth # type: ignore +from google import auth from google.api_core import 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 from google.auth import credentials # type: ignore @@ -30,6 +33,14 @@ from google.protobuf import empty_pb2 as empty # type: ignore +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution("google-cloud-firestore",).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + class FirestoreAdminTransport(abc.ABC): """Abstract transport class for FirestoreAdmin.""" @@ -45,6 +56,8 @@ def __init__( credentials: credentials.Credentials = None, credentials_file: typing.Optional[str] = None, scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES, + quota_project_id: typing.Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, **kwargs, ) -> None: """Instantiate the transport. @@ -60,6 +73,13 @@ def __init__( be loaded with :func:`google.auth.load_credentials_from_file`. This argument is mutually exclusive with credentials. scope (Optional[Sequence[str]]): A list of scopes. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. """ # Save the hostname. Default to port 443 (HTTPS) if none is specified. if ":" not in host: @@ -75,14 +95,52 @@ def __init__( if credentials_file is not None: credentials, _ = auth.load_credentials_from_file( - credentials_file, scopes=scopes + credentials_file, scopes=scopes, quota_project_id=quota_project_id ) + elif credentials is None: - credentials, _ = auth.default(scopes=scopes) + credentials, _ = auth.default( + scopes=scopes, quota_project_id=quota_project_id + ) # Save the credentials. self._credentials = credentials + # Lifted into its own function so it can be stubbed out during tests. + self._prep_wrapped_messages(client_info) + + def _prep_wrapped_messages(self, client_info): + # Precompute the wrapped methods. + self._wrapped_methods = { + self.create_index: gapic_v1.method.wrap_method( + self.create_index, default_timeout=None, client_info=client_info, + ), + self.list_indexes: gapic_v1.method.wrap_method( + self.list_indexes, default_timeout=None, client_info=client_info, + ), + self.get_index: gapic_v1.method.wrap_method( + self.get_index, default_timeout=None, client_info=client_info, + ), + self.delete_index: gapic_v1.method.wrap_method( + self.delete_index, default_timeout=None, client_info=client_info, + ), + self.get_field: gapic_v1.method.wrap_method( + self.get_field, default_timeout=None, client_info=client_info, + ), + self.update_field: gapic_v1.method.wrap_method( + self.update_field, default_timeout=None, client_info=client_info, + ), + self.list_fields: gapic_v1.method.wrap_method( + self.list_fields, default_timeout=None, client_info=client_info, + ), + self.export_documents: gapic_v1.method.wrap_method( + self.export_documents, default_timeout=None, client_info=client_info, + ), + self.import_documents: gapic_v1.method.wrap_method( + self.import_documents, default_timeout=None, client_info=client_info, + ), + } + @property def operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc.py b/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc.py index 9143e3f9ee..46143eaf99 100644 --- a/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc.py +++ b/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc.py @@ -19,6 +19,7 @@ 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 import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -32,7 +33,7 @@ from google.longrunning import operations_pb2 as operations # type: ignore from google.protobuf import empty_pb2 as empty # type: ignore -from .base import FirestoreAdminTransport +from .base import FirestoreAdminTransport, DEFAULT_CLIENT_INFO class FirestoreAdminGrpcTransport(FirestoreAdminTransport): @@ -60,7 +61,9 @@ def __init__( scopes: Sequence[str] = None, channel: grpc.Channel = None, api_mtls_endpoint: str = None, - client_cert_source: Callable[[], Tuple[bytes, bytes]] = None + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. @@ -87,6 +90,13 @@ def __init__( callback to provide client SSL certificate bytes and private key bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` is None. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport @@ -109,7 +119,9 @@ def __init__( ) if credentials is None: - credentials, _ = auth.default(scopes=self.AUTH_SCOPES) + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) # Create SSL credentials with client_cert_source or application # default SSL credentials. @@ -128,18 +140,21 @@ def __init__( credentials_file=credentials_file, ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, ) + self._stubs = {} # type: Dict[str, Callable] + # Run the base constructor. super().__init__( host=host, credentials=credentials, credentials_file=credentials_file, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, ) - self._stubs = {} # type: Dict[str, Callable] - @classmethod def create_channel( cls, @@ -147,7 +162,8 @@ def create_channel( credentials: credentials.Credentials = None, credentials_file: str = None, scopes: Optional[Sequence[str]] = None, - **kwargs + quota_project_id: Optional[str] = None, + **kwargs, ) -> grpc.Channel: """Create and return a gRPC channel object. Args: @@ -163,6 +179,8 @@ def create_channel( scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: @@ -178,7 +196,8 @@ def create_channel( credentials=credentials, credentials_file=credentials_file, scopes=scopes, - **kwargs + quota_project_id=quota_project_id, + **kwargs, ) @property diff --git a/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc_asyncio.py b/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc_asyncio.py index 9fdccc5fd0..e521076215 100644 --- a/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc_asyncio.py +++ b/google/cloud/firestore_admin_v1/services/firestore_admin/transports/grpc_asyncio.py @@ -17,6 +17,7 @@ from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple +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.auth import credentials # type: ignore @@ -31,7 +32,7 @@ from google.longrunning import operations_pb2 as operations # type: ignore from google.protobuf import empty_pb2 as empty # type: ignore -from .base import FirestoreAdminTransport +from .base import FirestoreAdminTransport, DEFAULT_CLIENT_INFO from .grpc import FirestoreAdminGrpcTransport @@ -59,7 +60,8 @@ def create_channel( credentials: credentials.Credentials = None, credentials_file: Optional[str] = None, scopes: Optional[Sequence[str]] = None, - **kwargs + quota_project_id: Optional[str] = None, + **kwargs, ) -> aio.Channel: """Create and return a gRPC AsyncIO channel object. Args: @@ -75,6 +77,8 @@ def create_channel( scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: @@ -86,7 +90,8 @@ def create_channel( credentials=credentials, credentials_file=credentials_file, scopes=scopes, - **kwargs + quota_project_id=quota_project_id, + **kwargs, ) def __init__( @@ -98,7 +103,9 @@ def __init__( scopes: Optional[Sequence[str]] = None, channel: aio.Channel = None, api_mtls_endpoint: str = None, - client_cert_source: Callable[[], Tuple[bytes, bytes]] = None + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + quota_project_id=None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. @@ -126,6 +133,13 @@ def __init__( callback to provide client SSL certificate bytes and private key bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` is None. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -164,6 +178,7 @@ def __init__( credentials_file=credentials_file, ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, ) # Run the base constructor. @@ -172,6 +187,8 @@ def __init__( credentials=credentials, credentials_file=credentials_file, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, ) self._stubs = {} diff --git a/google/cloud/firestore_admin_v1/types/__init__.py b/google/cloud/firestore_admin_v1/types/__init__.py index 8838c5bb96..f5cbaa99c9 100644 --- a/google/cloud/firestore_admin_v1/types/__init__.py +++ b/google/cloud/firestore_admin_v1/types/__init__.py @@ -30,6 +30,7 @@ ExportDocumentsRequest, ImportDocumentsRequest, ) +from .location import LocationMetadata from .operation import ( IndexOperationMetadata, FieldOperationMetadata, @@ -38,7 +39,6 @@ ExportDocumentsResponse, Progress, ) -from .location import LocationMetadata __all__ = ( @@ -55,11 +55,11 @@ "ListFieldsResponse", "ExportDocumentsRequest", "ImportDocumentsRequest", + "LocationMetadata", "IndexOperationMetadata", "FieldOperationMetadata", "ExportDocumentsMetadata", "ImportDocumentsMetadata", "ExportDocumentsResponse", "Progress", - "LocationMetadata", ) diff --git a/google/cloud/firestore_v1/services/firestore/transports/base.py b/google/cloud/firestore_v1/services/firestore/transports/base.py index 857997f44a..87edcbcdad 100644 --- a/google/cloud/firestore_v1/services/firestore/transports/base.py +++ b/google/cloud/firestore_v1/services/firestore/transports/base.py @@ -18,7 +18,7 @@ import abc import typing -from google import auth # type: ignore +from google import auth from google.api_core import exceptions # type: ignore from google.auth import credentials # type: ignore diff --git a/google/cloud/firestore_v1/types/__init__.py b/google/cloud/firestore_v1/types/__init__.py index 465a2d92e5..137c3130aa 100644 --- a/google/cloud/firestore_v1/types/__init__.py +++ b/google/cloud/firestore_v1/types/__init__.py @@ -68,54 +68,6 @@ BatchWriteRequest, BatchWriteResponse, ) -from typing import Tuple - - -__all__: Tuple[ - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, - str, -] __all__ = ( diff --git a/google/cloud/firestore_v1/types/common.py b/google/cloud/firestore_v1/types/common.py index f7bd22a3d9..b03242a4a8 100644 --- a/google/cloud/firestore_v1/types/common.py +++ b/google/cloud/firestore_v1/types/common.py @@ -19,9 +19,6 @@ from google.protobuf import timestamp_pb2 as timestamp # type: ignore -from typing import Any - -__protobuf__: Any __protobuf__ = proto.module( diff --git a/google/cloud/firestore_v1/types/document.py b/google/cloud/firestore_v1/types/document.py index b2111b34f2..7104bfc61a 100644 --- a/google/cloud/firestore_v1/types/document.py +++ b/google/cloud/firestore_v1/types/document.py @@ -21,9 +21,6 @@ from google.protobuf import struct_pb2 as struct # type: ignore from google.protobuf import timestamp_pb2 as timestamp # type: ignore from google.type import latlng_pb2 as latlng # type: ignore -from typing import Any - -__protobuf__: Any __protobuf__ = proto.module( diff --git a/google/cloud/firestore_v1/types/firestore.py b/google/cloud/firestore_v1/types/firestore.py index 909a782c81..345d67f709 100644 --- a/google/cloud/firestore_v1/types/firestore.py +++ b/google/cloud/firestore_v1/types/firestore.py @@ -24,9 +24,6 @@ from google.cloud.firestore_v1.types import write from google.protobuf import timestamp_pb2 as timestamp # type: ignore from google.rpc import status_pb2 as gr_status # type: ignore -from typing import Any - -__protobuf__: Any __protobuf__ = proto.module( @@ -579,14 +576,16 @@ class PartitionQueryRequest(proto.Message): resource names can be specified. structured_query (~.gf_query.StructuredQuery): A structured query. - Filters, order bys, limits, offsets, and + Query must specify collection with all + descendants and be ordered by name ascending. + Other filters, order bys, limits, offsets, and start/end cursors are not supported. partition_count (int): The desired maximum number of partition points. The partitions may be returned across multiple pages of results. The number must be - strictly positive. The actual number of - partitions returned may be fewer. + positive. The actual number of partitions + returned may be fewer. For example, this may be set to one fewer than the number of parallel queries to be run, or in @@ -655,6 +654,9 @@ class PartitionQueryResponse(proto.Message): - query, end_at A - query, start_at A, end_at B - query, start_at B + + An empty result may indicate that the query has too few + results to be partitioned. next_page_token (str): A page token that may be used to request an additional set of results, up to the number specified by diff --git a/google/cloud/firestore_v1/types/query.py b/google/cloud/firestore_v1/types/query.py index bea9a10a50..8a65a3623a 100644 --- a/google/cloud/firestore_v1/types/query.py +++ b/google/cloud/firestore_v1/types/query.py @@ -20,9 +20,6 @@ from google.cloud.firestore_v1.types import document from google.protobuf import wrappers_pb2 as wrappers # type: ignore -from typing import Any - -__protobuf__: Any __protobuf__ = proto.module( @@ -178,9 +175,11 @@ class Operator(proto.Enum): GREATER_THAN = 3 GREATER_THAN_OR_EQUAL = 4 EQUAL = 5 + NOT_EQUAL = 6 ARRAY_CONTAINS = 7 IN = 8 ARRAY_CONTAINS_ANY = 9 + NOT_IN = 10 field = proto.Field( proto.MESSAGE, number=1, message="StructuredQuery.FieldReference", @@ -207,6 +206,8 @@ class Operator(proto.Enum): OPERATOR_UNSPECIFIED = 0 IS_NAN = 2 IS_NULL = 3 + IS_NOT_NAN = 4 + IS_NOT_NULL = 5 op = proto.Field( proto.ENUM, number=1, enum="StructuredQuery.UnaryFilter.Operator", @@ -219,6 +220,22 @@ class Operator(proto.Enum): message="StructuredQuery.FieldReference", ) + class Order(proto.Message): + r"""An order on a field. + + Attributes: + field (~.query.StructuredQuery.FieldReference): + The field to order by. + direction (~.query.StructuredQuery.Direction): + The direction to order by. Defaults to ``ASCENDING``. + """ + + field = proto.Field( + proto.MESSAGE, number=1, message="StructuredQuery.FieldReference", + ) + + direction = proto.Field(proto.ENUM, number=2, enum="StructuredQuery.Direction",) + class FieldReference(proto.Message): r"""A reference to a field, such as ``max(messages.time) as max_time``. @@ -244,22 +261,6 @@ class Projection(proto.Message): proto.MESSAGE, number=2, message="StructuredQuery.FieldReference", ) - class Order(proto.Message): - r"""An order on a field. - - Attributes: - field (~.query.StructuredQuery.FieldReference): - The field to order by. - direction (~.query.StructuredQuery.Direction): - The direction to order by. Defaults to ``ASCENDING``. - """ - - field = proto.Field( - proto.MESSAGE, number=1, message="StructuredQuery.FieldReference", - ) - - direction = proto.Field(proto.ENUM, number=2, enum="StructuredQuery.Direction",) - select = proto.Field(proto.MESSAGE, number=1, message=Projection,) from_ = proto.RepeatedField(proto.MESSAGE, number=2, message=CollectionSelector,) diff --git a/google/cloud/firestore_v1/types/write.py b/google/cloud/firestore_v1/types/write.py index 12cdf99b62..6b3f49b530 100644 --- a/google/cloud/firestore_v1/types/write.py +++ b/google/cloud/firestore_v1/types/write.py @@ -21,9 +21,6 @@ from google.cloud.firestore_v1.types import common from google.cloud.firestore_v1.types import document as gf_document from google.protobuf import timestamp_pb2 as timestamp # type: ignore -from typing import Any - -__protobuf__: Any __protobuf__ = proto.module( diff --git a/noxfile.py b/noxfile.py index 82daad6af0..e7b710c266 100644 --- a/noxfile.py +++ b/noxfile.py @@ -22,7 +22,7 @@ import nox -PYTYPE_VERSION = "pytype==2020.7.24" + BLACK_VERSION = "black==19.10b0" BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"] @@ -61,14 +61,6 @@ def blacken(session): ) -@nox.session(python="3.7") -def pytype(session): - """Run pytype - """ - session.install(PYTYPE_VERSION) - session.run("pytype",) - - @nox.session(python=DEFAULT_PYTHON_VERSION) def lint_setup_py(session): """Verify that setup.py is valid (including RST check).""" @@ -76,15 +68,16 @@ def lint_setup_py(session): session.run("python", "setup.py", "check", "--restructuredtext", "--strict") -def default(session, test_dir, ignore_dir=None): +def default(session): # Install all test dependencies, then install this package in-place. - session.install("pytest-asyncio", "aiounittest") + session.install("asyncmock", "pytest-asyncio") session.install("mock", "pytest", "pytest-cov") session.install("-e", ".") # Run py.test against the unit tests. - args = [ + session.run( + "py.test", "--quiet", "--cov=google.cloud.firestore", "--cov=google.cloud", @@ -93,22 +86,15 @@ def default(session, test_dir, ignore_dir=None): "--cov-config=.coveragerc", "--cov-report=", "--cov-fail-under=0", - test_dir, + os.path.join("tests", "unit"), *session.posargs, - ] - - if ignore_dir: - args.insert(0, f"--ignore={ignore_dir}") - - session.run("py.test", *args) + ) @nox.session(python=UNIT_TEST_PYTHON_VERSIONS) def unit(session): - """Run the unit test suite for sync tests.""" - default( - session, os.path.join("tests", "unit"), - ) + """Run the unit test suite.""" + default(session) @nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS) @@ -132,7 +118,7 @@ def system(session): # Install all test dependencies, then install this package into the # virtualenv's dist-packages. session.install( - "mock", "pytest", "pytest-asyncio", "google-cloud-testutils", + "mock", "pytest", "google-cloud-testutils", ) session.install("-e", ".") @@ -151,7 +137,7 @@ def cover(session): test runs (not system test runs), and then erases coverage data. """ session.install("coverage", "pytest-cov") - session.run("coverage", "report", "--show-missing") + session.run("coverage", "report", "--show-missing", "--fail-under=100") session.run("coverage", "erase") diff --git a/scripts/fixup_admin_v1_keywords.py b/scripts/fixup_admin_v1_keywords.py index b3cb9d1478..4666d89efc 100644 --- a/scripts/fixup_admin_v1_keywords.py +++ b/scripts/fixup_admin_v1_keywords.py @@ -49,6 +49,7 @@ class adminCallTransformer(cst.CSTTransformer): 'list_fields': ('parent', 'filter', 'page_size', 'page_token', ), 'list_indexes': ('parent', 'filter', 'page_size', 'page_token', ), 'update_field': ('field', 'update_mask', ), + } def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: diff --git a/setup.cfg b/setup.cfg index f0c722b1ed..c3a2b39f65 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,14 +17,3 @@ # Generated by synthtool. DO NOT EDIT! [bdist_wheel] universal = 1 - -[pytype] -python_version = 3.8 -inputs = - google/cloud/ -exclude = - tests/ -output = .pytype/ -# Workaround for https://github.com/google/pytype/issues/150 -disable = pyi-error - diff --git a/synth.metadata b/synth.metadata index cdaf4ab812..25245b7b65 100644 --- a/synth.metadata +++ b/synth.metadata @@ -3,16 +3,16 @@ { "git": { "name": ".", - "remote": "git@github.com:crwilcox/python-firestore.git", - "sha": "cc25d5ebfb8cc39b63bff2383e81d16793d42b20" + "remote": "https://github.com/googleapis/python-firestore.git", + "sha": "a22ecddfa31865fcb02ffa0eed44a776d228b8a4" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "5099a037c974066832474771c5dfab504b8daaf6", - "internalRef": "321186647" + "sha": "17de2b31f9450385e739bedeeaac6e1ec4f239a8", + "internalRef": "327504150" } }, { diff --git a/tests/unit/gapic/admin_v1/__init__.py b/tests/unit/gapic/admin_v1/__init__.py index e69de29bb2..8b13789179 100644 --- a/tests/unit/gapic/admin_v1/__init__.py +++ b/tests/unit/gapic/admin_v1/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/gapic/admin_v1/test_firestore_admin.py b/tests/unit/gapic/admin_v1/test_firestore_admin.py index 0e6e9c27cb..a52f68efd2 100644 --- a/tests/unit/gapic/admin_v1/test_firestore_admin.py +++ b/tests/unit/gapic/admin_v1/test_firestore_admin.py @@ -58,6 +58,17 @@ def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# If default endpoint is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint(client): + return ( + "foo.googleapis.com" + if ("localhost" in client.DEFAULT_ENDPOINT) + else client.DEFAULT_ENDPOINT + ) + + def test__get_default_mtls_endpoint(): api_endpoint = "example.googleapis.com" api_mtls_endpoint = "example.mtls.googleapis.com" @@ -124,6 +135,16 @@ def test_firestore_admin_client_get_transport_class(): ), ], ) +@mock.patch.object( + FirestoreAdminClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(FirestoreAdminClient), +) +@mock.patch.object( + FirestoreAdminAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(FirestoreAdminAsyncClient), +) def test_firestore_admin_client_client_options( client_class, transport_class, transport_name ): @@ -150,64 +171,31 @@ def test_firestore_admin_client_client_options( scopes=None, api_mtls_endpoint="squid.clam.whelk", client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is # "never". - os.environ["GOOGLE_API_USE_MTLS"] = "never" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - ) + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "never"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + api_mtls_endpoint=client.DEFAULT_ENDPOINT, + client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is # "always". - os.environ["GOOGLE_API_USE_MTLS"] = "always" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and client_cert_source is provided. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - options = client_options.ClientOptions( - client_cert_source=client_cert_source_callback - ) - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=client_cert_source_callback, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and default_client_cert_source is provided. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}): + with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() patched.assert_called_once_with( @@ -217,34 +205,93 @@ def test_firestore_admin_client_client_options( scopes=None, api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", but client_cert_source and default_client_cert_source are None. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, - ): + # "auto", and client_cert_source is provided. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + options = client_options.ClientOptions( + client_cert_source=client_cert_source_callback + ) + with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None - client = client_class() + client = client_class(client_options=options) patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_ENDPOINT, + host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, + client_cert_source=client_cert_source_callback, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) + # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is + # "auto", and default_client_cert_source is provided. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=True, + ): + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_MTLS_ENDPOINT, + scopes=None, + api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, + client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is + # "auto", but client_cert_source and default_client_cert_source are None. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=False, + ): + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + api_mtls_endpoint=client.DEFAULT_ENDPOINT, + client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has # unsupported value. - os.environ["GOOGLE_API_USE_MTLS"] = "Unsupported" - with pytest.raises(MutualTLSChannelError): - client = client_class() + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() - del os.environ["GOOGLE_API_USE_MTLS"] + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + api_mtls_endpoint=client.DEFAULT_ENDPOINT, + client_cert_source=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) @pytest.mark.parametrize( @@ -273,6 +320,8 @@ def test_firestore_admin_client_client_options_scopes( scopes=["1", "2"], api_mtls_endpoint=client.DEFAULT_ENDPOINT, client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -302,6 +351,8 @@ def test_firestore_admin_client_client_options_credentials_file( scopes=None, api_mtls_endpoint=client.DEFAULT_ENDPOINT, client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -320,17 +371,21 @@ def test_firestore_admin_client_client_options_from_dict(): scopes=None, api_mtls_endpoint="squid.clam.whelk", client_cert_source=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) -def test_create_index(transport: str = "grpc"): +def test_create_index( + transport: str = "grpc", request_type=firestore_admin.CreateIndexRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.CreateIndexRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.create_index), "__call__") as call: @@ -343,12 +398,16 @@ def test_create_index(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.CreateIndexRequest() # Establish that the response is the type that we expect. assert isinstance(response, future.Future) +def test_create_index_from_dict(): + test_create_index(request_type=dict) + + @pytest.mark.asyncio async def test_create_index_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -514,14 +573,16 @@ async def test_create_index_flattened_error_async(): ) -def test_list_indexes(transport: str = "grpc"): +def test_list_indexes( + transport: str = "grpc", request_type=firestore_admin.ListIndexesRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.ListIndexesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.list_indexes), "__call__") as call: @@ -536,7 +597,7 @@ def test_list_indexes(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.ListIndexesRequest() # Establish that the response is the type that we expect. assert isinstance(response, pagers.ListIndexesPager) @@ -544,6 +605,10 @@ def test_list_indexes(transport: str = "grpc"): assert response.next_page_token == "next_page_token_value" +def test_list_indexes_from_dict(): + test_list_indexes(request_type=dict) + + @pytest.mark.asyncio async def test_list_indexes_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -756,8 +821,8 @@ def test_list_indexes_pages(): RuntimeError, ) pages = list(client.list_indexes(request={}).pages) - for page, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page.raw_page.next_page_token == token + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token @pytest.mark.asyncio @@ -821,20 +886,22 @@ async def test_list_indexes_async_pages(): RuntimeError, ) pages = [] - async for page in (await client.list_indexes(request={})).pages: - pages.append(page) - for page, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page.raw_page.next_page_token == token + async for page_ in (await client.list_indexes(request={})).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -def test_get_index(transport: str = "grpc"): +def test_get_index( + transport: str = "grpc", request_type=firestore_admin.GetIndexRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.GetIndexRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.get_index), "__call__") as call: @@ -851,7 +918,7 @@ def test_get_index(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.GetIndexRequest() # Establish that the response is the type that we expect. assert isinstance(response, index.Index) @@ -863,6 +930,10 @@ def test_get_index(transport: str = "grpc"): assert response.state == index.Index.State.CREATING +def test_get_index_from_dict(): + test_get_index(request_type=dict) + + @pytest.mark.asyncio async def test_get_index_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -1022,14 +1093,16 @@ async def test_get_index_flattened_error_async(): ) -def test_delete_index(transport: str = "grpc"): +def test_delete_index( + transport: str = "grpc", request_type=firestore_admin.DeleteIndexRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.DeleteIndexRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.delete_index), "__call__") as call: @@ -1042,12 +1115,16 @@ def test_delete_index(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.DeleteIndexRequest() # Establish that the response is the type that we expect. assert response is None +def test_delete_index_from_dict(): + test_delete_index(request_type=dict) + + @pytest.mark.asyncio async def test_delete_index_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -1195,14 +1272,16 @@ async def test_delete_index_flattened_error_async(): ) -def test_get_field(transport: str = "grpc"): +def test_get_field( + transport: str = "grpc", request_type=firestore_admin.GetFieldRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.GetFieldRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.get_field), "__call__") as call: @@ -1215,7 +1294,7 @@ def test_get_field(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.GetFieldRequest() # Establish that the response is the type that we expect. assert isinstance(response, field.Field) @@ -1223,6 +1302,10 @@ def test_get_field(transport: str = "grpc"): assert response.name == "name_value" +def test_get_field_from_dict(): + test_get_field(request_type=dict) + + @pytest.mark.asyncio async def test_get_field_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -1374,14 +1457,16 @@ async def test_get_field_flattened_error_async(): ) -def test_update_field(transport: str = "grpc"): +def test_update_field( + transport: str = "grpc", request_type=firestore_admin.UpdateFieldRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.UpdateFieldRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.update_field), "__call__") as call: @@ -1394,12 +1479,16 @@ def test_update_field(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.UpdateFieldRequest() # Establish that the response is the type that we expect. assert isinstance(response, future.Future) +def test_update_field_from_dict(): + test_update_field(request_type=dict) + + @pytest.mark.asyncio async def test_update_field_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -1555,14 +1644,16 @@ async def test_update_field_flattened_error_async(): ) -def test_list_fields(transport: str = "grpc"): +def test_list_fields( + transport: str = "grpc", request_type=firestore_admin.ListFieldsRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.ListFieldsRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.list_fields), "__call__") as call: @@ -1577,7 +1668,7 @@ def test_list_fields(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.ListFieldsRequest() # Establish that the response is the type that we expect. assert isinstance(response, pagers.ListFieldsPager) @@ -1585,6 +1676,10 @@ def test_list_fields(transport: str = "grpc"): assert response.next_page_token == "next_page_token_value" +def test_list_fields_from_dict(): + test_list_fields(request_type=dict) + + @pytest.mark.asyncio async def test_list_fields_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -1791,8 +1886,8 @@ def test_list_fields_pages(): RuntimeError, ) pages = list(client.list_fields(request={}).pages) - for page, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page.raw_page.next_page_token == token + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token @pytest.mark.asyncio @@ -1852,20 +1947,22 @@ async def test_list_fields_async_pages(): RuntimeError, ) pages = [] - async for page in (await client.list_fields(request={})).pages: - pages.append(page) - for page, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page.raw_page.next_page_token == token + async for page_ in (await client.list_fields(request={})).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token -def test_export_documents(transport: str = "grpc"): +def test_export_documents( + transport: str = "grpc", request_type=firestore_admin.ExportDocumentsRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.ExportDocumentsRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -1880,12 +1977,16 @@ def test_export_documents(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.ExportDocumentsRequest() # Establish that the response is the type that we expect. assert isinstance(response, future.Future) +def test_export_documents_from_dict(): + test_export_documents(request_type=dict) + + @pytest.mark.asyncio async def test_export_documents_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -2043,14 +2144,16 @@ async def test_export_documents_flattened_error_async(): ) -def test_import_documents(transport: str = "grpc"): +def test_import_documents( + transport: str = "grpc", request_type=firestore_admin.ImportDocumentsRequest +): client = FirestoreAdminClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = firestore_admin.ImportDocumentsRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2065,12 +2168,16 @@ def test_import_documents(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == firestore_admin.ImportDocumentsRequest() # Establish that the response is the type that we expect. assert isinstance(response, future.Future) +def test_import_documents_from_dict(): + test_import_documents(request_type=dict) + + @pytest.mark.asyncio async def test_import_documents_async(transport: str = "grpc_asyncio"): client = FirestoreAdminAsyncClient( @@ -2299,9 +2406,13 @@ def test_firestore_admin_base_transport_error(): def test_firestore_admin_base_transport(): # Instantiate the base transport. - transport = transports.FirestoreAdminTransport( - credentials=credentials.AnonymousCredentials(), - ) + with mock.patch( + "google.cloud.firestore_admin_v1.services.firestore_admin.transports.FirestoreAdminTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.FirestoreAdminTransport( + credentials=credentials.AnonymousCredentials(), + ) # Every method on the transport should just blindly # raise NotImplementedError. @@ -2328,10 +2439,15 @@ def test_firestore_admin_base_transport(): def test_firestore_admin_base_transport_with_credentials_file(): # Instantiate the base transport with a credentials file - with mock.patch.object(auth, "load_credentials_from_file") as load_creds: + with mock.patch.object( + auth, "load_credentials_from_file" + ) as load_creds, mock.patch( + "google.cloud.firestore_admin_v1.services.firestore_admin.transports.FirestoreAdminTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None load_creds.return_value = (credentials.AnonymousCredentials(), None) transport = transports.FirestoreAdminTransport( - credentials_file="credentials.json", + credentials_file="credentials.json", quota_project_id="octopus", ) load_creds.assert_called_once_with( "credentials.json", @@ -2339,6 +2455,7 @@ def test_firestore_admin_base_transport_with_credentials_file(): "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/datastore", ), + quota_project_id="octopus", ) @@ -2351,7 +2468,8 @@ def test_firestore_admin_auth_adc(): scopes=( "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/datastore", - ) + ), + quota_project_id=None, ) @@ -2360,12 +2478,15 @@ def test_firestore_admin_transport_auth_adc(): # ADC credentials. with mock.patch.object(auth, "default") as adc: adc.return_value = (credentials.AnonymousCredentials(), None) - transports.FirestoreAdminGrpcTransport(host="squid.clam.whelk") + transports.FirestoreAdminGrpcTransport( + host="squid.clam.whelk", quota_project_id="octopus" + ) adc.assert_called_once_with( scopes=( "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/datastore", - ) + ), + quota_project_id="octopus", ) @@ -2456,6 +2577,7 @@ def test_firestore_admin_grpc_transport_channel_mtls_with_client_cert_source( "https://www.googleapis.com/auth/datastore", ), ssl_credentials=mock_ssl_cred, + quota_project_id=None, ) assert transport.grpc_channel == mock_grpc_channel @@ -2493,6 +2615,7 @@ def test_firestore_admin_grpc_asyncio_transport_channel_mtls_with_client_cert_so "https://www.googleapis.com/auth/datastore", ), ssl_credentials=mock_ssl_cred, + quota_project_id=None, ) assert transport.grpc_channel == mock_grpc_channel @@ -2532,6 +2655,7 @@ def test_firestore_admin_grpc_transport_channel_mtls_with_adc( "https://www.googleapis.com/auth/datastore", ), ssl_credentials=mock_ssl_cred, + quota_project_id=None, ) assert transport.grpc_channel == mock_grpc_channel @@ -2571,6 +2695,7 @@ def test_firestore_admin_grpc_asyncio_transport_channel_mtls_with_adc( "https://www.googleapis.com/auth/datastore", ), ssl_credentials=mock_ssl_cred, + quota_project_id=None, ) assert transport.grpc_channel == mock_grpc_channel @@ -2653,3 +2778,24 @@ def test_parse_index_path(): # Check that the path construction is reversible. actual = FirestoreAdminClient.parse_index_path(path) assert expected == actual + + +def test_client_withDEFAULT_CLIENT_INFO(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.FirestoreAdminTransport, "_prep_wrapped_messages" + ) as prep: + client = FirestoreAdminClient( + credentials=credentials.AnonymousCredentials(), client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.FirestoreAdminTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = FirestoreAdminClient.get_transport_class() + transport = transport_class( + credentials=credentials.AnonymousCredentials(), client_info=client_info, + ) + prep.assert_called_once_with(client_info)