15
15
"""Helpers for wrapping low-level gRPC methods with common functionality.
16
16
17
17
This 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.
19
19
"""
20
20
21
21
import enum
@@ -38,7 +38,7 @@ class _MethodDefault(enum.Enum):
38
38
39
39
40
40
DEFAULT = _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,
42
42
so the default should be used."""
43
43
44
44
@@ -72,27 +72,43 @@ class _GapicCallable(object):
72
72
after its start, not to be confused with deadline). If ``None``,
73
73
this callable will not specify a timeout argument to the low-level
74
74
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.
75
78
metadata (Sequence[Tuple[str, str]]): Additional metadata that is
76
79
provided to the RPC method on every invocation. This is merged with
77
80
any metadata specified during invocation. If ``None``, no
78
81
additional metadata will be passed to the RPC method.
79
82
"""
80
83
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
+ ):
82
92
self ._target = target
83
93
self ._retry = retry
84
94
self ._timeout = timeout
95
+ self ._compression = compression
85
96
self ._metadata = metadata
86
97
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."""
89
102
90
103
if retry is DEFAULT :
91
104
retry = self ._retry
92
105
93
106
if timeout is DEFAULT :
94
107
timeout = self ._timeout
95
108
109
+ if compression is DEFAULT :
110
+ compression = self ._compression
111
+
96
112
if isinstance (timeout , (int , float )):
97
113
timeout = TimeToDeadlineTimeout (timeout = timeout )
98
114
@@ -109,6 +125,8 @@ def __call__(self, *args, timeout=DEFAULT, retry=DEFAULT, **kwargs):
109
125
metadata = list (metadata )
110
126
metadata .extend (self ._metadata )
111
127
kwargs ["metadata" ] = metadata
128
+ if self ._compression is not None :
129
+ kwargs ["compression" ] = compression
112
130
113
131
return wrapped_func (* args , ** kwargs )
114
132
@@ -117,19 +135,21 @@ def wrap_method(
117
135
func ,
118
136
default_retry = None ,
119
137
default_timeout = None ,
138
+ default_compression = None ,
120
139
client_info = client_info .DEFAULT_CLIENT_INFO ,
121
140
):
122
141
"""Wrap an RPC method with common behavior.
123
142
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 ``
126
145
arguments.
127
146
128
147
For example::
129
148
130
149
import google.api_core.gapic_v1.method
131
150
from google.api_core import retry
132
151
from google.api_core import timeout
152
+ from grpc import Compression
133
153
134
154
# The original RPC method.
135
155
def get_topic(name, timeout=None):
@@ -138,6 +158,7 @@ def get_topic(name, timeout=None):
138
158
139
159
default_retry = retry.Retry(deadline=60)
140
160
default_timeout = timeout.Timeout(deadline=60)
161
+ default_compression = Compression.NoCompression
141
162
wrapped_get_topic = google.api_core.gapic_v1.method.wrap_method(
142
163
get_topic, default_retry)
143
164
@@ -186,6 +207,9 @@ def get_topic(name, timeout=None):
186
207
default_timeout (Optional[google.api_core.Timeout]): The default
187
208
timeout strategy. Can also be specified as an int or float. If
188
209
``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.
189
213
client_info
190
214
(Optional[google.api_core.gapic_v1.client_info.ClientInfo]):
191
215
Client information used to create a user-agent string that's
@@ -194,19 +218,23 @@ def get_topic(name, timeout=None):
194
218
metadata will be provided to the RPC method.
195
219
196
220
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,
199
224
and metadata behavior to the low-level RPC method.
200
225
"""
201
226
func = grpc_helpers .wrap_errors (func )
202
-
203
227
if client_info is not None :
204
228
user_agent_metadata = [client_info .to_grpc_metadata ()]
205
229
else :
206
230
user_agent_metadata = None
207
231
208
232
return functools .wraps (func )(
209
233
_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 ,
211
239
)
212
240
)
0 commit comments