From 80e27e4d10ede46ee3feedbc85e415a29bef34c1 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Mon, 7 Oct 2024 04:18:36 +0000 Subject: [PATCH] fix unit tests and address more comments --- .../abstract_operations_async_client.py | 3 +- .../abstract_operations_base_client.py | 49 ++++--------------- .../operations_v1/transports/rest_asyncio.py | 35 +++++++++++-- .../test_operations_rest_client.py | 4 +- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/google/api_core/operations_v1/abstract_operations_async_client.py b/google/api_core/operations_v1/abstract_operations_async_client.py index 873597b0..8e180c81 100644 --- a/google/api_core/operations_v1/abstract_operations_async_client.py +++ b/google/api_core/operations_v1/abstract_operations_async_client.py @@ -66,8 +66,7 @@ def __init__( are specified, the client will attempt to ascertain the credentials from the environment. transport (Union[str, OperationsTransport]): The - transport to use. If set to None, a transport is chosen - automatically. + transport to use. If set to None, this defaults to 'rest_asyncio'. client_options (google.api_core.client_options.ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the diff --git a/google/api_core/operations_v1/abstract_operations_base_client.py b/google/api_core/operations_v1/abstract_operations_base_client.py index 5db9adf8..3304a73a 100644 --- a/google/api_core/operations_v1/abstract_operations_base_client.py +++ b/google/api_core/operations_v1/abstract_operations_base_client.py @@ -19,7 +19,6 @@ from typing import Dict, Optional, Type, Union from google.api_core import client_options as client_options_lib # 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.operations_v1.transports.base import ( DEFAULT_CLIENT_INFO, @@ -359,41 +358,13 @@ def __init__( self._transport = transport else: Transport = type(self).get_transport_class(transport) - # NOTE: The conditional logic below to initialize the transport can be removed - # once we have feature parity with the sync transport. - if "async" in str(Transport).lower(): - unsupported_params = { - # TODO(https://github.com/googleapis/python-api-core/issues/715): Add support for `credentials_file` to async REST transport. - "google.api_core.client_options.ClientOptions.credentials_file": client_options.credentials_file, - # TODO(https://github.com/googleapis/python-api-core/issues/716): Add support for `scopes` to async REST transport. - "google.api_core.client_options.ClientOptions.scopes": client_options.scopes, - # TODO(https://github.com/googleapis/python-api-core/issues/717): Add support for `quota_project_id` to async REST transport. - "google.api_core.client_options.ClientOptions.quota_project_id": client_options.quota_project_id, - # TODO(https://github.com/googleapis/python-api-core/issues/718): Add support for `client_cert_source` to async REST transport. - "google.api_core.client_options.ClientOptions.client_cert_source": client_options.client_cert_source, - } - provided_unsupported_params = [ - name - for name, value in unsupported_params.items() - if value is not None - ] - if provided_unsupported_params: - raise core_exceptions.AsyncRestUnsupportedParameterError( - f"The following provided parameters are not supported for `transport=rest_asyncio`: {', '.join(provided_unsupported_params)}" - ) - self._transport = Transport( - credentials=credentials, - host=api_endpoint, - client_info=client_info, - ) - else: - self._transport = Transport( - credentials=credentials, - credentials_file=client_options.credentials_file, - host=api_endpoint, - scopes=client_options.scopes, - client_cert_source_for_mtls=client_cert_source_func, - quota_project_id=client_options.quota_project_id, - client_info=client_info, - always_use_jwt_access=True, - ) + self._transport = Transport( + credentials=credentials, + credentials_file=client_options.credentials_file, + host=api_endpoint, + scopes=client_options.scopes, + client_cert_source_for_mtls=client_cert_source_func, + quota_project_id=client_options.quota_project_id, + client_info=client_info, + always_use_jwt_access=True, + ) diff --git a/google/api_core/operations_v1/transports/rest_asyncio.py b/google/api_core/operations_v1/transports/rest_asyncio.py index 52aeb881..8fdce84a 100644 --- a/google/api_core/operations_v1/transports/rest_asyncio.py +++ b/google/api_core/operations_v1/transports/rest_asyncio.py @@ -72,15 +72,19 @@ def __init__( *, host: str = "longrunning.googleapis.com", credentials: Optional[ga_credentials_async.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, always_use_jwt_access: Optional[bool] = False, url_scheme: str = "https", http_options: Optional[Dict] = None, path_prefix: str = "v1", - # TODO(https://github.com/googleapis/python-api-core/issues/715): Add support for `credentials_file` to async REST transport. - # TODO(https://github.com/googleapis/python-api-core/issues/716): Add support for `scopes` to async REST transport. - # TODO(https://github.com/googleapis/python-api-core/issues/717): Add support for `quota_project_id` to async REST transport. - # TODO(https://github.com/googleapis/python-api-core/issues/718): Add support for `client_cert_source` to async REST transport. + # TODO(https://github.com/googleapis/python-api-core/issues/715): Add docstring for `credentials_file` to async REST transport. + # TODO(https://github.com/googleapis/python-api-core/issues/716): Add docstring for `scopes` to async REST transport. + # TODO(https://github.com/googleapis/python-api-core/issues/717): Add docstring for `quota_project_id` to async REST transport. + # TODO(https://github.com/googleapis/python-api-core/issues/718): Add docstring for `client_cert_source` to async REST transport. ) -> None: """Instantiate the transport. @@ -109,12 +113,33 @@ def __init__( "v1" by default. """ + unsupported_params = { + # TODO(https://github.com/googleapis/python-api-core/issues/715): Add support for `credentials_file` to async REST transport. + "google.api_core.client_options.ClientOptions.credentials_file": credentials_file, + # TODO(https://github.com/googleapis/python-api-core/issues/716): Add support for `scopes` to async REST transport. + "google.api_core.client_options.ClientOptions.scopes": scopes, + # TODO(https://github.com/googleapis/python-api-core/issues/717): Add support for `quota_project_id` to async REST transport. + "google.api_core.client_options.ClientOptions.quota_project_id": quota_project_id, + # TODO(https://github.com/googleapis/python-api-core/issues/718): Add support for `client_cert_source` to async REST transport. + "google.api_core.client_options.ClientOptions.client_cert_source": client_cert_source_for_mtls, + # TODO(https://github.com/googleapis/python-api-core/issues/718): Add support for `client_cert_source` to async REST transport. + "google.api_core.client_options.ClientOptions.client_cert_source": client_cert_source_for_mtls, + } + provided_unsupported_params = [ + name for name, value in unsupported_params.items() if value is not None + ] + if provided_unsupported_params: + raise core_exceptions.AsyncRestUnsupportedParameterError( + f"The following provided parameters are not supported for `transport=rest_asyncio`: {', '.join(provided_unsupported_params)}" + ) + super().__init__( host=host, # TODO(https://github.com/googleapis/python-api-core/issues/709): Remove `type: ignore` when the linked issue is resolved. credentials=credentials, # type: ignore client_info=client_info, - always_use_jwt_access=always_use_jwt_access, + # TODO(https://github.com/googleapis/python-api-core/issues/725): Set always_use_jwt_access token when supported. + always_use_jwt_access=False, ) # TODO(https://github.com/googleapis/python-api-core/issues/708): add support for # `default_host` in AsyncAuthorizedSession for feature parity with the synchronous diff --git a/tests/unit/operations_v1/test_operations_rest_client.py b/tests/unit/operations_v1/test_operations_rest_client.py index 72d4f0ec..3fe4e70d 100644 --- a/tests/unit/operations_v1/test_operations_rest_client.py +++ b/tests/unit/operations_v1/test_operations_rest_client.py @@ -17,7 +17,7 @@ import mock import pytest -from typing import Any +from typing import Any, List try: import grpc # noqa: F401 @@ -68,7 +68,7 @@ ], } -CLIENTS: list[Any] = [ +CLIENTS: List[Any] = [ AbstractOperationsClient, ] CLIENTS_WITH_TRANSPORT = [