From 279fc56241f54325483166ca52eccc7a39e7649e Mon Sep 17 00:00:00 2001 From: ohmayr Date: Tue, 17 Sep 2024 04:22:52 +0000 Subject: [PATCH] update workflow --- .github/workflows/tests.yaml | 3 ++- .../%service/transports/__init__.py.j2 | 20 +++++++++++++-- .../cloud_redis/transports/__init__.py | 14 +++++++++-- tests/system/conftest.py | 25 +++++++++++++------ 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 4c00a2bb3..2e433a8e3 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -55,8 +55,9 @@ jobs: strategy: # Run showcase tests on the lowest and highest supported runtimes matrix: + # TODO(https://github.com/googleapis/gapic-generator-python/issues/2121) Remove `showcase_w_rest_async` target when async rest is GA. python: ["3.7", "3.12"] - target: [showcase, showcase_alternative_templates] + target: [showcase, showcase_w_rest_async] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/__init__.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/__init__.py.j2 index 66be2e5c2..48232c710 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/__init__.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/__init__.py.j2 @@ -1,9 +1,11 @@ +{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #} +{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %} {% extends '_base.py.j2' %} {% block content %} from collections import OrderedDict -from typing import Dict, Type +from typing import Dict, Type{% if rest_async_io_enabled %}, Tuple{% endif +%} from .base import {{ service.name }}Transport {% if 'grpc' in opts.transport %} @@ -13,6 +15,16 @@ from .grpc_asyncio import {{ service.name }}GrpcAsyncIOTransport {% if 'rest' in opts.transport %} from .rest import {{ service.name }}RestTransport from .rest import {{ service.name }}RestInterceptor +{% if rest_async_io_enabled %} +ASYNC_REST_CLASSES: Tuple[str, ...] +try: + from .rest_asyncio import Async{{ service.name }}RestTransport + ASYNC_REST_CLASSES = ('Async{{ service.name }}RestTransport',) + HAS_REST_ASYNC = True +except ImportError: # pragma: NO COVER + ASYNC_REST_CLASSES = () + HAS_REST_ASYNC = False +{% endif %}{# if rest_async_io_enabled #} {% endif %} @@ -25,6 +37,10 @@ _transport_registry['grpc_asyncio'] = {{ service.name }}GrpcAsyncIOTransport {% endif %} {% if 'rest' in opts.transport %} _transport_registry['rest'] = {{ service.name }}RestTransport +{% if rest_async_io_enabled %} +if HAS_REST_ASYNC: # pragma: NO COVER + _transport_registry['rest_asyncio'] = Async{{ service.name }}RestTransport +{% endif %}{# if rest_async_io_enabled #} {% endif %} __all__ = ( @@ -37,5 +53,5 @@ __all__ = ( '{{ service.name }}RestTransport', '{{ service.name }}RestInterceptor', {% endif %} -) +){% if 'rest' in opts.transport and rest_async_io_enabled%} + ASYNC_REST_CLASSES{%endif%} {% endblock %} diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/__init__.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/__init__.py index 889648d30..7bd88e6c6 100755 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/__init__.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/__init__.py @@ -14,13 +14,21 @@ # limitations under the License. # from collections import OrderedDict -from typing import Dict, Type +from typing import Dict, Type, Tuple from .base import CloudRedisTransport from .grpc import CloudRedisGrpcTransport from .grpc_asyncio import CloudRedisGrpcAsyncIOTransport from .rest import CloudRedisRestTransport from .rest import CloudRedisRestInterceptor +ASYNC_REST_CLASSES: Tuple[str, ...] +try: + from .rest_asyncio import AsyncCloudRedisRestTransport + ASYNC_REST_CLASSES = ('AsyncCloudRedisRestTransport',) + HAS_REST_ASYNC = True +except ImportError: # pragma: NO COVER + ASYNC_REST_CLASSES = () + HAS_REST_ASYNC = False # Compile a registry of transports. @@ -28,6 +36,8 @@ _transport_registry['grpc'] = CloudRedisGrpcTransport _transport_registry['grpc_asyncio'] = CloudRedisGrpcAsyncIOTransport _transport_registry['rest'] = CloudRedisRestTransport +if HAS_REST_ASYNC: # pragma: NO COVER + _transport_registry['rest_asyncio'] = AsyncCloudRedisRestTransport __all__ = ( 'CloudRedisTransport', @@ -35,4 +45,4 @@ 'CloudRedisGrpcAsyncIOTransport', 'CloudRedisRestTransport', 'CloudRedisRestInterceptor', -) +) + ASYNC_REST_CLASSES diff --git a/tests/system/conftest.py b/tests/system/conftest.py index 6507078b6..9b454cfd7 100644 --- a/tests/system/conftest.py +++ b/tests/system/conftest.py @@ -38,10 +38,15 @@ from google.showcase import EchoAsyncClient from google.showcase import IdentityAsyncClient try: - import google.showcase.transports.rest_asyncio - SHOWCASE_ASYNC_TRANSPORTS = ["grpc_asyncio", "rest_asyncio"] + from google.showcase_v1beta1.services.echo.transports import AsyncEchoRestTransport + HAS_ASYNC_REST_ECHO_TRANSPORT = True except: - SHOWCASE_ASYNC_TRANSPORTS = ["grpc_asyncio"] + HAS_ASYNC_REST_ECHO_TRANSPORT = False + try: + from google.showcase_v1beta1.services.identity.transports import AsyncIdentityRestTransport + HAS_ASYNC_REST_IDENTITY_TRANSPORT = True + except: + HAS_ASYNC_REST_IDENTITY_TRANSPORT = False # TODO: use async auth anon credentials by default once the minimum version of google-auth is upgraded. # See related issue: https://github.com/googleapis/gapic-generator-python/issues/2107. @@ -62,22 +67,28 @@ def async_anonymous_credentials(): def event_loop(): return asyncio.get_event_loop() - @pytest.fixture(params=SHOWCASE_ASYNC_TRANSPORTS) + @pytest.fixture(params=["grpc_asyncio", "rest_asyncio"]) def async_echo(use_mtls, request, event_loop): + transport = request.param + if transport == "rest_asyncio" and not HAS_ASYNC_REST_ECHO_TRANSPORT: + pytest.skip("Skipping test with async rest.") return construct_client( EchoAsyncClient, use_mtls, - transport_name=request.param, + transport_name=transport, channel_creator=aio.insecure_channel if request.param == "grpc_asyncio" else None, credentials=async_anonymous_credentials(), ) - @pytest.fixture(params=SHOWCASE_ASYNC_TRANSPORTS) + @pytest.fixture(params=["grpc_asyncio", "rest_asyncio"]) def async_identity(use_mtls, request, event_loop): + transport = request.param + if transport == "rest_asyncio" and not HAS_ASYNC_REST_IDENTITY_TRANSPORT: + pytest.skip("Skipping test with async rest.") return construct_client( IdentityAsyncClient, use_mtls, - transport_name=request.param, + transport_name=transport, channel_creator=aio.insecure_channel if request.param == "grpc_asyncio" else None, credentials=async_anonymous_credentials(), )