1515"""Helpers for wrapping low-level gRPC methods with common functionality.
1616
1717This is used by gapic clients to provide common error mapping, retry, timeout,
18- pagination, and long-running operations to gRPC methods.
18+ compression, pagination, and long-running operations to gRPC methods.
1919"""
2020
2121import enum
@@ -38,7 +38,7 @@ class _MethodDefault(enum.Enum):
3838
3939
4040DEFAULT = _MethodDefault ._DEFAULT_VALUE
41- """Sentinel value indicating that a retry or timeout argument was unspecified,
41+ """Sentinel value indicating that a retry, timeout, or compression argument was unspecified,
4242so the default should be used."""
4343
4444
@@ -72,27 +72,43 @@ class _GapicCallable(object):
7272 after its start, not to be confused with deadline). If ``None``,
7373 this callable will not specify a timeout argument to the low-level
7474 RPC method.
75+ compression (grpc.Compression): The default compression for the callable.
76+ If ``None``, this callable will not specify a compression argument
77+ to the low-level RPC method.
7578 metadata (Sequence[Tuple[str, str]]): Additional metadata that is
7679 provided to the RPC method on every invocation. This is merged with
7780 any metadata specified during invocation. If ``None``, no
7881 additional metadata will be passed to the RPC method.
7982 """
8083
81- def __init__ (self , target , retry , timeout , metadata = None ):
84+ def __init__ (
85+ self ,
86+ target ,
87+ retry ,
88+ timeout ,
89+ compression ,
90+ metadata = None ,
91+ ):
8292 self ._target = target
8393 self ._retry = retry
8494 self ._timeout = timeout
95+ self ._compression = compression
8596 self ._metadata = metadata
8697
87- def __call__ (self , * args , timeout = DEFAULT , retry = DEFAULT , ** kwargs ):
88- """Invoke the low-level RPC with retry, timeout, and metadata."""
98+ def __call__ (
99+ self , * args , timeout = DEFAULT , retry = DEFAULT , compression = DEFAULT , ** kwargs
100+ ):
101+ """Invoke the low-level RPC with retry, timeout, compression, and metadata."""
89102
90103 if retry is DEFAULT :
91104 retry = self ._retry
92105
93106 if timeout is DEFAULT :
94107 timeout = self ._timeout
95108
109+ if compression is DEFAULT :
110+ compression = self ._compression
111+
96112 if isinstance (timeout , (int , float )):
97113 timeout = TimeToDeadlineTimeout (timeout = timeout )
98114
@@ -109,6 +125,8 @@ def __call__(self, *args, timeout=DEFAULT, retry=DEFAULT, **kwargs):
109125 metadata = list (metadata )
110126 metadata .extend (self ._metadata )
111127 kwargs ["metadata" ] = metadata
128+ if self ._compression is not None :
129+ kwargs ["compression" ] = compression
112130
113131 return wrapped_func (* args , ** kwargs )
114132
@@ -117,19 +135,21 @@ def wrap_method(
117135 func ,
118136 default_retry = None ,
119137 default_timeout = None ,
138+ default_compression = None ,
120139 client_info = client_info .DEFAULT_CLIENT_INFO ,
121140):
122141 """Wrap an RPC method with common behavior.
123142
124- This applies common error wrapping, retry, and timeout behavior a function.
125- The wrapped function will take optional ``retry`` and ``timeout ``
143+ This applies common error wrapping, retry, timeout, and compression behavior to a function.
144+ The wrapped function will take optional ``retry``, ``timeout``, and ``compression ``
126145 arguments.
127146
128147 For example::
129148
130149 import google.api_core.gapic_v1.method
131150 from google.api_core import retry
132151 from google.api_core import timeout
152+ from grpc import Compression
133153
134154 # The original RPC method.
135155 def get_topic(name, timeout=None):
@@ -138,6 +158,7 @@ def get_topic(name, timeout=None):
138158
139159 default_retry = retry.Retry(deadline=60)
140160 default_timeout = timeout.Timeout(deadline=60)
161+ default_compression = Compression.NoCompression
141162 wrapped_get_topic = google.api_core.gapic_v1.method.wrap_method(
142163 get_topic, default_retry)
143164
@@ -186,6 +207,9 @@ def get_topic(name, timeout=None):
186207 default_timeout (Optional[google.api_core.Timeout]): The default
187208 timeout strategy. Can also be specified as an int or float. If
188209 ``None``, the method will not have timeout specified by default.
210+ default_compression (Optional[grpc.Compression]): The default
211+ grpc.Compression. If ``None``, the method will not have
212+ compression specified by default.
189213 client_info
190214 (Optional[google.api_core.gapic_v1.client_info.ClientInfo]):
191215 Client information used to create a user-agent string that's
@@ -194,19 +218,23 @@ def get_topic(name, timeout=None):
194218 metadata will be provided to the RPC method.
195219
196220 Returns:
197- Callable: A new callable that takes optional ``retry`` and ``timeout``
198- arguments and applies the common error mapping, retry, timeout,
221+ Callable: A new callable that takes optional ``retry``, ``timeout``,
222+ and ``compression``
223+ arguments and applies the common error mapping, retry, timeout, compression,
199224 and metadata behavior to the low-level RPC method.
200225 """
201226 func = grpc_helpers .wrap_errors (func )
202-
203227 if client_info is not None :
204228 user_agent_metadata = [client_info .to_grpc_metadata ()]
205229 else :
206230 user_agent_metadata = None
207231
208232 return functools .wraps (func )(
209233 _GapicCallable (
210- func , default_retry , default_timeout , metadata = user_agent_metadata
234+ func ,
235+ default_retry ,
236+ default_timeout ,
237+ default_compression ,
238+ metadata = user_agent_metadata ,
211239 )
212240 )
0 commit comments