Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions gapic/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,6 @@ def _generate_samples_and_manifest(
autogen_specs = list(
samplegen.generate_sample_specs(api_schema, opts=opts))

# TODO: Support the generation of REST snippets.
autogen_specs = [
spec for spec in autogen_specs
if spec["transport"] != api.TRANSPORT_REST
]

# Also process any handwritten sample specs
handwritten_specs = samplegen.parse_handwritten_specs(
self._sample_configs)
Expand Down
23 changes: 16 additions & 7 deletions gapic/samplegen/samplegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,18 +997,24 @@ def generate_request_object(api_schema: api.API, service: wrappers.Service, mess
return request


def _transport_type_from_transport(transport: str) -> str:
if transport == api.TRANSPORT_GRPC:
def _sync_or_async_from_transport(transport: str) -> str:
if transport in (api.TRANSPORT_GRPC, api.TRANSPORT_REST):
return "sync"
elif transport == api.TRANSPORT_GRPC_ASYNC:
else: # transport is api.TRANSPORT_GRPC_ASYNC
# Currently the REST transport does not support async.
return "async"
else: # api.TRANSPORT_REST
return "rest"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now method will return "sync" whereas before it was "rest". Will this break anything?

Copy link
Copy Markdown
Contributor Author

@dizcology dizcology May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



def _supports_grpc(service) -> bool:
return api.TRANSPORT_GRPC in service.clients.keys()


def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, Any], None, None]:
"""Given an API, generate basic sample specs for each method.

If a service supports gRPC transport, we do not generate
spec for REST even if it also supports REST transport.

Args:
api_schema (api.API): The schema that defines the API.

Expand All @@ -1021,12 +1027,15 @@ def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, A
for service_name, service in gapic_metadata.services.items():
api_short_name = api_schema.services[f"{api_schema.naming.proto_package}.{service_name}"].shortname
api_version = api_schema.naming.version
supports_grpc = _supports_grpc(service)
for transport, client in service.clients.items():
transport_type = _transport_type_from_transport(transport)
if supports_grpc and transport == api.TRANSPORT_REST:
continue
sync_or_async = _sync_or_async_from_transport(transport)
for rpc_name, method_list in client.rpcs.items():
# Region Tag Format:
# [{START|END} ${apishortname}_${apiVersion}_generated_${serviceName}_${rpcName}_{sync|async|rest}]
region_tag = f"{api_short_name}_{api_version}_generated_{service_name}_{rpc_name}_{transport_type}"
region_tag = f"{api_short_name}_{api_version}_generated_{service_name}_{rpc_name}_{sync_or_async}"
spec = {
"rpc": rpc_name,
"transport": transport,
Expand Down
Loading