Skip to content

Commit 2177ea8

Browse files
[feat] Make get_transport_class more convenient. (#87)
This adds the `get_transport_class` method to clients, which require significantly less plumbing to import.
1 parent e0ac5b0 commit 2177ea8

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

packages/gapic-generator/gapic/templates/$namespace/$name_$version/$sub/services/$service/client.py.j2

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from google.auth import credentials
1212
{{ import_ }}
1313
{% endfor %}
1414
from ...{{ '.' * api.subpackage_view|length }}utils import dispatch
15-
from .transports import get_transport_class
15+
from .transports import _transport_registry
1616
from .transports import {{ service.name }}Transport
1717

1818

@@ -46,7 +46,7 @@ class {{ service.name }}:
4646
'provide its credentials directly.')
4747
self._transport = transport
4848
else:
49-
Transport = get_transport_class(transport)
49+
Transport = self.get_transport_class(transport)
5050
self._transport = Transport(credentials=credentials, host=host)
5151

5252
{% for method in service.methods.values() -%}
@@ -156,6 +156,26 @@ class {{ service.name }}:
156156
{% endfor %}
157157
{% endfor -%}
158158

159+
@classmethod
160+
def get_transport_class(cls,
161+
label: str = None) -> {{ service.name }}Transport:
162+
"""Return an appropriate transport class.
163+
164+
Args:
165+
label (str): The name of the desired transport. If none is
166+
provided, then the first transport in the registry is used.
167+
168+
Returns:
169+
Type[{{ service.name }}Transport]: The transport class to use.
170+
"""
171+
# If a specific transport is requested, return that one.
172+
if label:
173+
return _transport_registry[label]
174+
175+
# No transport is requested; return the default (that is, the first one
176+
# in the dictionary).
177+
return next(iter(_transport_registry.values()))
178+
159179
@property
160180
def client_info(self) -> gapic_v1.client_info.ClientInfo:
161181
"""Return information about this client (for metrics).

packages/gapic-generator/gapic/templates/$namespace/$name_$version/$sub/services/$service/transports/__init__.py.j2

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,22 @@ from .base import {{ service.name }}Transport
88
from .http import {{ service.name }}HttpTransport
99

1010
# Compile a registry of transports.
11-
_registry = collections.OrderedDict()
11+
_transport_registry = collections.OrderedDict()
1212

1313
# gRPC is not guaranteed to be available, because `grpcio` may or may not
1414
# be installed. If it is available, however, it should be the "first in"
1515
# (default).
1616
try:
1717
from .grpc import {{ service.name }}GrpcTransport
18-
_registry['grpc'] = {{ service.name }}GrpcTransport
18+
_transport_registry['grpc'] = {{ service.name }}GrpcTransport
1919
except ImportError:
2020
pass
2121

2222
# Always provide an HTTP/1.1 transport.
23-
_registry['http'] = {{ service.name }}HttpTransport
24-
25-
26-
# Provide a function for the client to get the appropriate transport.
27-
def get_transport_class(
28-
label: str = None,
29-
) -> typing.Type[{{ service.name }}Transport]:
30-
"""Return an appropriate transport class.
31-
32-
Args:
33-
label (str): The name of the desired transport. If none is provided,
34-
then the first transport in the registry is used.
35-
36-
Returns:
37-
Type[{{ service.name }}Transport]: The transport class to use.
38-
"""
39-
# If a specific transport is requested, return that one.
40-
if label:
41-
return _registry[label]
42-
43-
# No transport is requested; return the default (that is, the first one
44-
# in the dictionary).
45-
return next(iter(_registry.values()))
23+
_transport_registry['http'] = {{ service.name }}HttpTransport
4624

4725

4826
__all__ = (
4927
'{{ service.name }}Transport',
50-
'get_transport_class',
5128
)
5229
{% endblock %}

0 commit comments

Comments
 (0)