From 5d0f7cb86f9429322c4e549a7eebef59b8e7b35f Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Thu, 11 Feb 2021 14:49:46 -0800 Subject: [PATCH 01/17] feat: add gapic metadata file The GAPIC metadata file is used to track code, samples, and test coverage for every RPC and library method. --- .../%name/%version/gapic_metadata.json.j2 | 33 +++++++++++++++ gapic/cli/generate.py | 9 ++-- .../%name_%version/gapic_metadata.json.j2 | 41 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 create mode 100644 gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 diff --git a/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 b/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 new file mode 100644 index 0000000000..8312268e59 --- /dev/null +++ b/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 @@ -0,0 +1,33 @@ +{ + "schema": "1.0", + "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", + "language": "python", + "protoPackage": "{{ api.naming.proto_package }}", + "libraryPackage": "{{ '.'.join(api.naming.module_namespace+(api.naming.versioned_module_name,)) }}", + "services": { + {% for service in api.services.values()|sort(attribute='name') -%} + "{{ service.name }}":{ + {% if 'grpc' in opts.transport -%} + "grpc": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + }{{ "," if 'rest' in opts.transport else "" -}} + {% endif -%} + {% if 'rest' in opts.transport -%} + "rest": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + } + {% endif -%}{# opts.transport #} + }{{ "," if not loop.last else "" }} + {% endfor -%}{# service -#} + } +} diff --git a/gapic/cli/generate.py b/gapic/cli/generate.py index b0e50ccf2e..414170a213 100644 --- a/gapic/cli/generate.py +++ b/gapic/cli/generate.py @@ -47,10 +47,11 @@ def generate( # This generator uses a slightly different mechanism for determining # which files to generate; it tracks at package level rather than file # level. - package = os.path.commonprefix([i.package for i in filter( - lambda p: p.name in req.file_to_generate, - req.proto_file, - )]).rstrip('.') + package = os.path.commonprefix([ + p.package + for p in req.proto_file + if p.name in req.file_to_generate + ]).rstrip('.') # Build the API model object. # This object is a frozen representation of the whole API, and is sent diff --git a/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 new file mode 100644 index 0000000000..74ea4d6949 --- /dev/null +++ b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 @@ -0,0 +1,41 @@ +{ + "schema": "1.0", + "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", + "language": "python", + "protoPackage": "{{ api.naming.proto_package }}", + "libraryPackage": "{{ '.'.join(api.naming.module_namespace+(api.naming.versioned_module_name,)) }}", + "services": { + {% for service in api.services.values()|sort(attribute='name') -%} + "{{ service.name }}":{ + {% if 'grpc' in opts.transport -%} + "grpc": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + }, + "grpcAsync": { + "libraryClient": "{{ service.async_client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + }{{ "," if 'rest' in opts.transport else "" -}} + {% endif -%} + {% if 'rest' in opts.transport -%} + "rest": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + } + {% endif -%}{# opts.transport #} + }{{ "," if not loop.last else "" }} + {% endfor -%}{# service -#} + } +} From 4ba77f036080f8bda90e81963cf1729bcb7e5dd4 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Wed, 17 Feb 2021 10:39:32 -0800 Subject: [PATCH 02/17] Tweak CODEOWNERS to allow all actools to approve changes to configs --- .github/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 0546e1d814..9426cab4dd 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -5,3 +5,4 @@ # https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax * @googleapis/actools-python @googleapis/yoshi-python +*.yaml @googleapis/actools @googleapis/yoshi-python @googleapis/actools-python \ No newline at end of file From 3401c6b6c4ec6dc8fa3b263ee180e74efd2be998 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Thu, 11 Feb 2021 14:49:46 -0800 Subject: [PATCH 03/17] feat: add gapic metadata file The GAPIC metadata file is used to track code, samples, and test coverage for every RPC and library method. --- .../%name/%version/gapic_metadata.json.j2 | 33 +++++++++++++++ gapic/cli/generate.py | 9 ++-- .../%name_%version/gapic_metadata.json.j2 | 41 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 create mode 100644 gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 diff --git a/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 b/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 new file mode 100644 index 0000000000..8312268e59 --- /dev/null +++ b/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 @@ -0,0 +1,33 @@ +{ + "schema": "1.0", + "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", + "language": "python", + "protoPackage": "{{ api.naming.proto_package }}", + "libraryPackage": "{{ '.'.join(api.naming.module_namespace+(api.naming.versioned_module_name,)) }}", + "services": { + {% for service in api.services.values()|sort(attribute='name') -%} + "{{ service.name }}":{ + {% if 'grpc' in opts.transport -%} + "grpc": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + }{{ "," if 'rest' in opts.transport else "" -}} + {% endif -%} + {% if 'rest' in opts.transport -%} + "rest": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + } + {% endif -%}{# opts.transport #} + }{{ "," if not loop.last else "" }} + {% endfor -%}{# service -#} + } +} diff --git a/gapic/cli/generate.py b/gapic/cli/generate.py index b0e50ccf2e..414170a213 100644 --- a/gapic/cli/generate.py +++ b/gapic/cli/generate.py @@ -47,10 +47,11 @@ def generate( # This generator uses a slightly different mechanism for determining # which files to generate; it tracks at package level rather than file # level. - package = os.path.commonprefix([i.package for i in filter( - lambda p: p.name in req.file_to_generate, - req.proto_file, - )]).rstrip('.') + package = os.path.commonprefix([ + p.package + for p in req.proto_file + if p.name in req.file_to_generate + ]).rstrip('.') # Build the API model object. # This object is a frozen representation of the whole API, and is sent diff --git a/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 new file mode 100644 index 0000000000..74ea4d6949 --- /dev/null +++ b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 @@ -0,0 +1,41 @@ +{ + "schema": "1.0", + "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", + "language": "python", + "protoPackage": "{{ api.naming.proto_package }}", + "libraryPackage": "{{ '.'.join(api.naming.module_namespace+(api.naming.versioned_module_name,)) }}", + "services": { + {% for service in api.services.values()|sort(attribute='name') -%} + "{{ service.name }}":{ + {% if 'grpc' in opts.transport -%} + "grpc": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + }, + "grpcAsync": { + "libraryClient": "{{ service.async_client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + }{{ "," if 'rest' in opts.transport else "" -}} + {% endif -%} + {% if 'rest' in opts.transport -%} + "rest": { + "libraryClient": "{{ service.client_name }}", + "rpcs": { + {% for method in service.methods.values() -%} + "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} + {% endfor -%}{# method -#} + } + } + {% endif -%}{# opts.transport #} + }{{ "," if not loop.last else "" }} + {% endfor -%}{# service -#} + } +} From f1c4135638d19e0c5f546ce372444c538118a9b4 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Wed, 17 Feb 2021 10:39:32 -0800 Subject: [PATCH 04/17] Tweak CODEOWNERS to allow all actools to approve changes to configs --- .github/CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index f5c036fe95..5bc27edbcc 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -4,5 +4,5 @@ # For syntax help see: # https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax -* @googleapis/actools-python @googleapis/yoshi-python @lukesneeringer -*.yaml @googleapis/actools @googleapis/yoshi-python @googleapis/actools-python @lukesneeringer +* @googleapis/actools-python @googleapis/yoshi-python +*.yaml @googleapis/actools @googleapis/yoshi-python @googleapis/actools-python From dbe0f953de56382846926a624bec3457527df03d Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Thu, 25 Feb 2021 14:54:29 -0800 Subject: [PATCH 05/17] Use the protobuf description instead of hand-tooled json --- gapic/schema/api.py | 38 ++++ .../%name_%version/gapic_metadata.json.j2 | 42 +--- tests/unit/schema/test_api.py | 192 +++++++++++++++++- 3 files changed, 229 insertions(+), 43 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 35a59dcee0..3ce2ae2e9a 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -28,8 +28,10 @@ from google.api_core import exceptions # type: ignore from google.api import resource_pb2 # type: ignore +from google.gapic.metada import gapic_metadata_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import descriptor_pb2 +from google.protobuf.json_format import MessageToJson import grpc # type: ignore @@ -392,6 +394,42 @@ def subpackages(self) -> Mapping[str, 'API']: ) return answer + def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: + gm = GapicMetadata( + schema="1.0", + comment="This file maps proto services/RPCs to the corresponding library clients/methods", + language="python", + protoPackage=self.naming.proto_package, + libraryPackage=".".join(self.naming.module_namespace + self.naming.versioned_module_name,) + ) + + for service in sorted(self.services.values(), key=lambda s: s.name): + service_desc = gm.get_or_create(s.name) + if "grpc" in self.opts.transport: + for transport, client_name in [ + ("grpc", service.client_name), + ("grpcAsync", service.async_client_name) + ]: + transport = service_desc.clients.get_or_create(transport) + transport.library_client = client_name + for method in sorted(service.methods.values(), key=lambda m: m.name): + method_desc = transport.rpcs.get_or_create(method.name) + method_desc.methods.append(to_snake_case(method.name)) + + if "rest" in self.opts.transport: + transport = service_desc.clients.get_or_create("rest") + transport.library_client + for method in sorted(service.methods.values(), key=lambda m: m.name): + method_desc = transport.rpcs.get_or_create(method.name) + method_desc.methods.append(to_snake_case(method.name)) + + return gm + + + def gapic_metadata_json(self, options: Options) -> str: + return MessageToJson(self.gapic_metadata(options)) + + def requires_package(self, pkg: Tuple[str, ...]) -> bool: return any( message.ident.package == pkg diff --git a/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 index 74ea4d6949..666b131612 100644 --- a/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 +++ b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 @@ -1,41 +1 @@ -{ - "schema": "1.0", - "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", - "language": "python", - "protoPackage": "{{ api.naming.proto_package }}", - "libraryPackage": "{{ '.'.join(api.naming.module_namespace+(api.naming.versioned_module_name,)) }}", - "services": { - {% for service in api.services.values()|sort(attribute='name') -%} - "{{ service.name }}":{ - {% if 'grpc' in opts.transport -%} - "grpc": { - "libraryClient": "{{ service.client_name }}", - "rpcs": { - {% for method in service.methods.values() -%} - "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} - {% endfor -%}{# method -#} - } - }, - "grpcAsync": { - "libraryClient": "{{ service.async_client_name }}", - "rpcs": { - {% for method in service.methods.values() -%} - "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} - {% endfor -%}{# method -#} - } - }{{ "," if 'rest' in opts.transport else "" -}} - {% endif -%} - {% if 'rest' in opts.transport -%} - "rest": { - "libraryClient": "{{ service.client_name }}", - "rpcs": { - {% for method in service.methods.values() -%} - "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} - {% endfor -%}{# method -#} - } - } - {% endif -%}{# opts.transport #} - }{{ "," if not loop.last else "" }} - {% endfor -%}{# service -#} - } -} +{{ api.gapic_metadata_json(opts) }} diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index e91a310ee0..3108120dee 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -22,6 +22,7 @@ from google.api import client_pb2 from google.api import resource_pb2 from google.api_core import exceptions +from google.gapic.metada import gapic_metadata_pb2 from google.longrunning import operations_pb2 from google.protobuf import descriptor_pb2 @@ -260,8 +261,8 @@ def test_proto_oneof(): name='Bar', fields=( make_field_pb2(name='imported_message', number=1, - type_name='.google.dep.ImportedMessage', - oneof_index=0), + type_name='.google.dep.ImportedMessage', + oneof_index=0), make_field_pb2( name='primitive', number=2, type=1, oneof_index=0), ), @@ -1287,3 +1288,190 @@ def test_map_field_name_disambiguation(): # The same module used in the same place should have the same import alias. # Because there's a "mollusc" name used, the import should be disambiguated. assert mollusc_ident == mollusc_map_ident == "am_mollusc.Mollusc" + + +def test_gapic_metadata(): + api = api.API.build( + file_descriptors=[ + descriptor_pb2.FileDescriptorProto( + name="cephalopod.proto", + package="animalia.mollusca.v1", + service=[ + descriptor_pb2.ServiceDescriptorProto( + name="Squid", + method=[ + descriptor_pb2.MethodDescriptorProto(name="Ramshorn"), + descriptor_pb2.MethodDescriptorProto(name="Humboldt"), + descriptor_pb2.MethodDescriptorProto(name="Giant"), + ], + ), + descriptor_pb2.ServiceDescriptorProto( + name="Octopus", + method=[ + descriptor_pb2.MethodDescriptorProto(name="GiantPacific"), + descriptor_pb2.MethodDescriptorProto(name="BlueSpot"), + ] + ), + ], + ) + ] + ) + + opts = Options.build("transport=grpc") + expected = gapic_metadata_pb2.GapicMetadata( + schma="1.0", + comment="This file maps proto services/RPCs to the corresponding library clients/methods", + language="python", + proto_package="animalia.mollusca", + library_package="animalia.mollusca.v1", + services={ + "Octopus": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( + clients={ + "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="OctopusClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="AsyncOctopusClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + } + ), + "Squid": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( + clients={ + "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="SquidClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="AsyncSquidClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + } + ), + } + ) + actual = api.gapic_metadata(opts) + assert expected == actual + expected = MessageToJson(expected) + actual = api.gapic_metadata_json(opts) + assert expected == actual + + opts = Options.build("transport=rest") + expected = gapic_metadata_pb2.GapicMetadata( + schma="1.0", + comment="This file maps proto services/RPCs to the corresponding library clients/methods", + language="python", + proto_package="animalia.mollusca", + library_package="animalia.mollusca.v1", + services={ + "Octopus": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( + clients={ + "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="OctopusClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ) + } + ), + "Squid": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( + clients={ + "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="SquidClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + + } + ), + } + ) + actual = api.gapic_metadata(opts) + assert expected == actual + expected = MessageToJson(expected) + actual = api.gapic_metadata_json(opts) + assert expected == actual + + opts = Options.build("transport=rest+grpc") + expected = gapic_metadata_pb2.GapicMetadata( + schma="1.0", + comment="This file maps proto services/RPCs to the corresponding library clients/methods", + language="python", + proto_package="animalia.mollusca", + library_package="animalia.mollusca.v1", + services={ + "Octopus": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( + clients={ + "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="OctopusClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="AsyncOctopusClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="OctopusClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ) + } + ), + "Squid": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( + clients={ + "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="SquidClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="AsyncSquidClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( + library_client="SquidClient", + rpcs={ + "BlueSpot": ["blue_spot"], + "GiantPacific": ["giant_pacific"], + }, + ), + + } + ), + } + ) + + actual = api.gapic_metadata(opts) + assert expected == actual + expected = MessageToJson(expected) + actual = api.gapic_metadata_json(opts) + assert expected == actual From 0ad702902afc6f9581ed3e5389b3d0d32002744b Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Thu, 25 Feb 2021 16:08:46 -0800 Subject: [PATCH 06/17] fixes --- gapic/schema/api.py | 18 +++-- requirements.txt | 2 +- setup.py | 2 +- tests/unit/schema/test_api.py | 135 +++++++++++++++++++++------------- 4 files changed, 97 insertions(+), 60 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 3ce2ae2e9a..c314942217 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -28,7 +28,7 @@ from google.api_core import exceptions # type: ignore from google.api import resource_pb2 # type: ignore -from google.gapic.metada import gapic_metadata_pb2 # type: ignore +from google.gapic.metadata import gapic_metadata_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import descriptor_pb2 from google.protobuf.json_format import MessageToJson @@ -395,17 +395,19 @@ def subpackages(self) -> Mapping[str, 'API']: return answer def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: - gm = GapicMetadata( + gm = gapic_metadata_pb2.GapicMetadata( schema="1.0", comment="This file maps proto services/RPCs to the corresponding library clients/methods", language="python", - protoPackage=self.naming.proto_package, - libraryPackage=".".join(self.naming.module_namespace + self.naming.versioned_module_name,) + proto_package=self.naming.proto_package, + library_package=".".join( + self.naming.module_namespace + (self.naming.versioned_module_name,) + ), ) for service in sorted(self.services.values(), key=lambda s: s.name): - service_desc = gm.get_or_create(s.name) - if "grpc" in self.opts.transport: + service_desc = gm.services.get_or_create(service.name) + if "grpc" in options.transport: for transport, client_name in [ ("grpc", service.client_name), ("grpcAsync", service.async_client_name) @@ -416,9 +418,9 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: method_desc = transport.rpcs.get_or_create(method.name) method_desc.methods.append(to_snake_case(method.name)) - if "rest" in self.opts.transport: + if "rest" in options.transport: transport = service_desc.clients.get_or_create("rest") - transport.library_client + transport.library_client = service.client_name for method in sorted(service.methods.values(), key=lambda m: m.name): method_desc = transport.rpcs.get_or_create(method.name) method_desc.methods.append(to_snake_case(method.name)) diff --git a/requirements.txt b/requirements.txt index 27dca81d58..a98f18a7c0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ click==7.1.2 google-api-core==1.26.0 -googleapis-common-protos==1.52.0 +googleapis-common-protos==1.53.0 jinja2==2.11.3 MarkupSafe==1.1.1 protobuf==3.15.2 diff --git a/setup.py b/setup.py index 409dea8b65..69bb01d55f 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ install_requires=( "click >= 6.7", "google-api-core >= 1.17.0", - "googleapis-common-protos >= 1.6.0", + "googleapis-common-protos >= 1.53.0", "grpcio >= 1.24.3", "jinja2 >= 2.10", "protobuf >= 3.12.0", diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index 3108120dee..c5190e1cb4 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -22,9 +22,10 @@ from google.api import client_pb2 from google.api import resource_pb2 from google.api_core import exceptions -from google.gapic.metada import gapic_metadata_pb2 +from google.gapic.metadata import gapic_metadata_pb2 from google.longrunning import operations_pb2 from google.protobuf import descriptor_pb2 +from google.protobuf.json_format import MessageToJson from gapic.schema import api from gapic.schema import imp @@ -1291,25 +1292,53 @@ def test_map_field_name_disambiguation(): def test_gapic_metadata(): - api = api.API.build( + api_schema = api.API.build( file_descriptors=[ descriptor_pb2.FileDescriptorProto( name="cephalopod.proto", package="animalia.mollusca.v1", + message_type=[ + descriptor_pb2.DescriptorProto( + name="MolluscRequest", + ), + descriptor_pb2.DescriptorProto( + name="Mollusc", + ), + ], service=[ descriptor_pb2.ServiceDescriptorProto( name="Squid", method=[ - descriptor_pb2.MethodDescriptorProto(name="Ramshorn"), - descriptor_pb2.MethodDescriptorProto(name="Humboldt"), - descriptor_pb2.MethodDescriptorProto(name="Giant"), + descriptor_pb2.MethodDescriptorProto( + name="Ramshorn", + input_type="animalia.mollusca.v1.MolluscRequest", + output_type="animalia.mollusca.v1.Mollusc", + ), + descriptor_pb2.MethodDescriptorProto( + name="Humboldt", + input_type="animalia.mollusca.v1.MolluscRequest", + output_type="animalia.mollusca.v1.Mollusc", + ), + descriptor_pb2.MethodDescriptorProto( + name="Giant", + input_type="animalia.mollusca.v1.MolluscRequest", + output_type="animalia.mollusca.v1.Mollusc", + ), ], ), descriptor_pb2.ServiceDescriptorProto( name="Octopus", method=[ - descriptor_pb2.MethodDescriptorProto(name="GiantPacific"), - descriptor_pb2.MethodDescriptorProto(name="BlueSpot"), + descriptor_pb2.MethodDescriptorProto( + name="GiantPacific", + input_type="animalia.mollusca.v1.MolluscRequest", + output_type="animalia.mollusca.v1.Mollusc", + ), + descriptor_pb2.MethodDescriptorProto( + name="BlueSpot", + input_type="animalia.mollusca.v1.MolluscRequest", + output_type="animalia.mollusca.v1.Mollusc", + ), ] ), ], @@ -1319,26 +1348,26 @@ def test_gapic_metadata(): opts = Options.build("transport=grpc") expected = gapic_metadata_pb2.GapicMetadata( - schma="1.0", + schema="1.0", comment="This file maps proto services/RPCs to the corresponding library clients/methods", language="python", - proto_package="animalia.mollusca", - library_package="animalia.mollusca.v1", + proto_package="animalia.mollusca.v1", + library_package="animalia.mollusca_v1", services={ "Octopus": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( clients={ "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="OctopusClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "BlueSpot": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["blue_spot"]), + "GiantPacific": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant_pacific"]), }, ), "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( - library_client="AsyncOctopusClient", + library_client="OctopusAsyncClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "BlueSpot": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["blue_spot"]), + "GiantPacific": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant_pacific"]), }, ), } @@ -1348,42 +1377,44 @@ def test_gapic_metadata(): "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="SquidClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "Giant": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant"]), + "Humboldt": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["humboldt"]), + "Ramshorn": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["ramshorn"]), }, ), "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( - library_client="AsyncSquidClient", + library_client="SquidAsyncClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "Giant": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant"]), + "Humboldt": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["humboldt"]), + "Ramshorn": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["ramshorn"]), }, ), } ), } ) - actual = api.gapic_metadata(opts) + actual = api_schema.gapic_metadata(opts) assert expected == actual expected = MessageToJson(expected) - actual = api.gapic_metadata_json(opts) + actual = api_schema.gapic_metadata_json(opts) assert expected == actual opts = Options.build("transport=rest") expected = gapic_metadata_pb2.GapicMetadata( - schma="1.0", + schema="1.0", comment="This file maps proto services/RPCs to the corresponding library clients/methods", language="python", - proto_package="animalia.mollusca", - library_package="animalia.mollusca.v1", + proto_package="animalia.mollusca.v1", + library_package="animalia.mollusca_v1", services={ "Octopus": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( clients={ "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="OctopusClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "BlueSpot": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["blue_spot"]), + "GiantPacific": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant_pacific"]), }, ) } @@ -1393,8 +1424,9 @@ def test_gapic_metadata(): "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="SquidClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "Giant": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant"]), + "Humboldt": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["humboldt"]), + "Ramshorn": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["ramshorn"]), }, ), @@ -1402,41 +1434,41 @@ def test_gapic_metadata(): ), } ) - actual = api.gapic_metadata(opts) + actual = api_schema.gapic_metadata(opts) assert expected == actual expected = MessageToJson(expected) - actual = api.gapic_metadata_json(opts) + actual = api_schema.gapic_metadata_json(opts) assert expected == actual opts = Options.build("transport=rest+grpc") expected = gapic_metadata_pb2.GapicMetadata( - schma="1.0", + schema="1.0", comment="This file maps proto services/RPCs to the corresponding library clients/methods", language="python", - proto_package="animalia.mollusca", - library_package="animalia.mollusca.v1", + proto_package="animalia.mollusca.v1", + library_package="animalia.mollusca_v1", services={ "Octopus": gapic_metadata_pb2.GapicMetadata.ServiceForTransport( clients={ "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="OctopusClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "BlueSpot": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["blue_spot"]), + "GiantPacific": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant_pacific"]), }, ), "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( - library_client="AsyncOctopusClient", + library_client="OctopusAsyncClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "BlueSpot": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["blue_spot"]), + "GiantPacific": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant_pacific"]), }, ), "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="OctopusClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "BlueSpot": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["blue_spot"]), + "GiantPacific": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant_pacific"]), }, ) } @@ -1446,22 +1478,25 @@ def test_gapic_metadata(): "grpc": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="SquidClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "Giant": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant"]), + "Humboldt": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["humboldt"]), + "Ramshorn": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["ramshorn"]), }, ), "grpcAsync": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( - library_client="AsyncSquidClient", + library_client="SquidAsyncClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "Giant": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant"]), + "Humboldt": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["humboldt"]), + "Ramshorn": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["ramshorn"]), }, ), "rest": gapic_metadata_pb2.GapicMetadata.ServiceAsClient( library_client="SquidClient", rpcs={ - "BlueSpot": ["blue_spot"], - "GiantPacific": ["giant_pacific"], + "Giant": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["giant"]), + "Humboldt": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["humboldt"]), + "Ramshorn": gapic_metadata_pb2.GapicMetadata.MethodList(methods=["ramshorn"]), }, ), @@ -1470,8 +1505,8 @@ def test_gapic_metadata(): } ) - actual = api.gapic_metadata(opts) + actual = api_schema.gapic_metadata(opts) assert expected == actual expected = MessageToJson(expected) - actual = api.gapic_metadata_json(opts) + actual = api_schema.gapic_metadata_json(opts) assert expected == actual From 52ea8a021620daf1abafa6987c2003eb69af0212 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Thu, 25 Feb 2021 16:18:23 -0800 Subject: [PATCH 07/17] Fix for mypy --- gapic/schema/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index c314942217..3bf7d5bb1b 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -408,11 +408,11 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: for service in sorted(self.services.values(), key=lambda s: s.name): service_desc = gm.services.get_or_create(service.name) if "grpc" in options.transport: - for transport, client_name in [ + for tprt, client_name in [ ("grpc", service.client_name), ("grpcAsync", service.async_client_name) ]: - transport = service_desc.clients.get_or_create(transport) + transport = service_desc.clients.get_or_create(tprt) transport.library_client = client_name for method in sorted(service.methods.values(), key=lambda m: m.name): method_desc = transport.rpcs.get_or_create(method.name) From d03a41f4dec5e91edf5a80f15fb555333c638a71 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Fri, 26 Feb 2021 11:15:09 -0800 Subject: [PATCH 08/17] Style check fix --- gapic/schema/api.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 3bf7d5bb1b..777cac9019 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -28,7 +28,7 @@ from google.api_core import exceptions # type: ignore from google.api import resource_pb2 # type: ignore -from google.gapic.metadata import gapic_metadata_pb2 # type: ignore +from google.gapic.metadata import gapic_metadata_pb2 # type: ignore from google.longrunning import operations_pb2 # type: ignore from google.protobuf import descriptor_pb2 from google.protobuf.json_format import MessageToJson @@ -427,11 +427,9 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: return gm - def gapic_metadata_json(self, options: Options) -> str: return MessageToJson(self.gapic_metadata(options)) - def requires_package(self, pkg: Tuple[str, ...]) -> bool: return any( message.ident.package == pkg From b1a3f1a1b6cb53f03bd53d8e01ffcd69aea533bd Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Fri, 26 Feb 2021 11:38:18 -0800 Subject: [PATCH 09/17] Sort the keys when generating json --- gapic/schema/api.py | 2 +- tests/unit/schema/test_api.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 777cac9019..8273971c22 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -428,7 +428,7 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: return gm def gapic_metadata_json(self, options: Options) -> str: - return MessageToJson(self.gapic_metadata(options)) + return MessageToJson(self.gapic_metadata(options), sort_keys=True) def requires_package(self, pkg: Tuple[str, ...]) -> bool: return any( diff --git a/tests/unit/schema/test_api.py b/tests/unit/schema/test_api.py index c5190e1cb4..1eaa8a57ae 100644 --- a/tests/unit/schema/test_api.py +++ b/tests/unit/schema/test_api.py @@ -1396,7 +1396,7 @@ def test_gapic_metadata(): ) actual = api_schema.gapic_metadata(opts) assert expected == actual - expected = MessageToJson(expected) + expected = MessageToJson(expected, sort_keys=True) actual = api_schema.gapic_metadata_json(opts) assert expected == actual @@ -1436,7 +1436,7 @@ def test_gapic_metadata(): ) actual = api_schema.gapic_metadata(opts) assert expected == actual - expected = MessageToJson(expected) + expected = MessageToJson(expected, sort_keys=True) actual = api_schema.gapic_metadata_json(opts) assert expected == actual @@ -1507,6 +1507,6 @@ def test_gapic_metadata(): actual = api_schema.gapic_metadata(opts) assert expected == actual - expected = MessageToJson(expected) + expected = MessageToJson(expected, sort_keys=True) actual = api_schema.gapic_metadata_json(opts) assert expected == actual From d9133b1ac506f16a5a8df235434e65166f29690f Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Fri, 26 Feb 2021 15:06:10 -0800 Subject: [PATCH 10/17] Fix style check to be more helpful --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index dae6d0bd54..74fb502ec4 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -310,4 +310,4 @@ jobs: python -m pip install autopep8 - name: Check diff run: | - find gapic tests -name "*.py" | xargs autopep8 --in-place --exit-code + find gapic tests -name "*.py" | xargs autopep8 --diff --exit-code From daa5297c1dc469b285fb3c34c38cc869f5f00df4 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Fri, 26 Feb 2021 15:29:52 -0800 Subject: [PATCH 11/17] Style check --- gapic/schema/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 8273971c22..0ec55d45c9 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -401,7 +401,8 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: language="python", proto_package=self.naming.proto_package, library_package=".".join( - self.naming.module_namespace + (self.naming.versioned_module_name,) + self.naming.module_namespace + + (self.naming.versioned_module_name,) ), ) From 853d6a5dd295e0d0d2693a633cc25ea4883c9fc9 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Fri, 26 Feb 2021 15:36:42 -0800 Subject: [PATCH 12/17] Take three --- gapic/schema/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 0ec55d45c9..1612d069d4 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -402,7 +402,7 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: proto_package=self.naming.proto_package, library_package=".".join( self.naming.module_namespace + - (self.naming.versioned_module_name,) + (self.naming.versioned_module_name,) ), ) From 01e6c0e92140eccee2a0d855135333b3452c5e88 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Mon, 1 Mar 2021 17:27:12 -0800 Subject: [PATCH 13/17] Comment out the metadata json generation --- .../templates/%namespace/%name_%version/gapic_metadata.json.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 index 666b131612..edd79fda8a 100644 --- a/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 +++ b/gapic/templates/%namespace/%name_%version/gapic_metadata.json.j2 @@ -1 +1 @@ -{{ api.gapic_metadata_json(opts) }} +{# {{ api.gapic_metadata_json(opts) }} #} {# TODO(dovs): This is temporarily commented out pending the addition of a flag #} From d642b9cd92b702ec4401a464a52bccab63b46b54 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Mon, 1 Mar 2021 17:33:53 -0800 Subject: [PATCH 14/17] Commentary --- gapic/schema/api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index 1612d069d4..d9d02c9c4f 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -408,6 +408,10 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: for service in sorted(self.services.values(), key=lambda s: s.name): service_desc = gm.services.get_or_create(service.name) + + # At least one of "grpc" or "rest" is guaranteed to be present because + # of the way that Options instances are created. + # This assumes the options are generated by the class method factory. if "grpc" in options.transport: for tprt, client_name in [ ("grpc", service.client_name), From 5a40330e54a7fca7c4637487cfaa35d0a048082c Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Mon, 1 Mar 2021 17:41:53 -0800 Subject: [PATCH 15/17] Copy to ads templates as well --- .../%name/%version/gapic_metadata.json.j2 | 34 +------------------ 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 b/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 index 8312268e59..edd79fda8a 100644 --- a/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/gapic_metadata.json.j2 @@ -1,33 +1 @@ -{ - "schema": "1.0", - "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", - "language": "python", - "protoPackage": "{{ api.naming.proto_package }}", - "libraryPackage": "{{ '.'.join(api.naming.module_namespace+(api.naming.versioned_module_name,)) }}", - "services": { - {% for service in api.services.values()|sort(attribute='name') -%} - "{{ service.name }}":{ - {% if 'grpc' in opts.transport -%} - "grpc": { - "libraryClient": "{{ service.client_name }}", - "rpcs": { - {% for method in service.methods.values() -%} - "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} - {% endfor -%}{# method -#} - } - }{{ "," if 'rest' in opts.transport else "" -}} - {% endif -%} - {% if 'rest' in opts.transport -%} - "rest": { - "libraryClient": "{{ service.client_name }}", - "rpcs": { - {% for method in service.methods.values() -%} - "{{ method.name }}": ["{{ method.name|snake_case }}"]{{ "," if not loop.last else "" }} - {% endfor -%}{# method -#} - } - } - {% endif -%}{# opts.transport #} - }{{ "," if not loop.last else "" }} - {% endfor -%}{# service -#} - } -} +{# {{ api.gapic_metadata_json(opts) }} #} {# TODO(dovs): This is temporarily commented out pending the addition of a flag #} From 81ff0fc0913f4e2db74476f39c1586a021241566 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Mon, 1 Mar 2021 17:46:28 -0800 Subject: [PATCH 16/17] Refactor --- gapic/schema/api.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index d9d02c9c4f..b29c7dcd27 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -412,20 +412,17 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: # At least one of "grpc" or "rest" is guaranteed to be present because # of the way that Options instances are created. # This assumes the options are generated by the class method factory. + transports = [] if "grpc" in options.transport: - for tprt, client_name in [ - ("grpc", service.client_name), - ("grpcAsync", service.async_client_name) - ]: - transport = service_desc.clients.get_or_create(tprt) - transport.library_client = client_name - for method in sorted(service.methods.values(), key=lambda m: m.name): - method_desc = transport.rpcs.get_or_create(method.name) - method_desc.methods.append(to_snake_case(method.name)) + transports.append(("grpc", service.client_name)) + transports.append(("grpcAsync", service.async_client_name)) if "rest" in options.transport: - transport = service_desc.clients.get_or_create("rest") - transport.library_client = service.client_name + transports.append(("rest", service.client_name)) + + for tprt, client_name in transports: + transport = service_desc.clients.get_or_create(tprt) + transport.library_client = client_name for method in sorted(service.methods.values(), key=lambda m: m.name): method_desc = transport.rpcs.get_or_create(method.name) method_desc.methods.append(to_snake_case(method.name)) From 70b79cf0ec832a9099cc07453a63427e35a86aa8 Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Tue, 2 Mar 2021 11:33:23 -0800 Subject: [PATCH 17/17] Minor optimization --- gapic/schema/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gapic/schema/api.py b/gapic/schema/api.py index b29c7dcd27..39f0c49045 100644 --- a/gapic/schema/api.py +++ b/gapic/schema/api.py @@ -420,10 +420,11 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata: if "rest" in options.transport: transports.append(("rest", service.client_name)) + methods = sorted(service.methods.values(), key=lambda m: m.name) for tprt, client_name in transports: transport = service_desc.clients.get_or_create(tprt) transport.library_client = client_name - for method in sorted(service.methods.values(), key=lambda m: m.name): + for method in methods: method_desc = transport.rpcs.get_or_create(method.name) method_desc.methods.append(to_snake_case(method.name))