|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""AsyncIO helpers for :mod:`grpc` supporting 3.6+. |
| 16 | +
|
| 17 | +Please combine more detailed docstring in grpc_helpers.py to use following |
| 18 | +functions. This module is implementing the same surface with AsyncIO semantics. |
| 19 | +""" |
| 20 | + |
| 21 | +import asyncio |
| 22 | +import functools |
| 23 | + |
| 24 | +import grpc |
| 25 | +from grpc.experimental import aio |
| 26 | + |
| 27 | +from google.api_core import exceptions, grpc_helpers |
| 28 | + |
| 29 | + |
| 30 | +# TODO(lidiz) Support gRPC GCP wrapper |
| 31 | +HAS_GRPC_GCP = False |
| 32 | + |
| 33 | +# NOTE(lidiz) Alternatively, we can hack "__getattribute__" to perform |
| 34 | +# automatic patching for us. But that means the overhead of creating an |
| 35 | +# extra Python function spreads to every single send and receive. |
| 36 | + |
| 37 | + |
| 38 | +class _WrappedCall(aio.Call): |
| 39 | + |
| 40 | + def __init__(self): |
| 41 | + self._call = None |
| 42 | + |
| 43 | + def with_call(self, call): |
| 44 | + """Supplies the call object separately to keep __init__ clean.""" |
| 45 | + self._call = call |
| 46 | + return self |
| 47 | + |
| 48 | + async def initial_metadata(self): |
| 49 | + return await self._call.initial_metadata() |
| 50 | + |
| 51 | + async def trailing_metadata(self): |
| 52 | + return await self._call.trailing_metadata() |
| 53 | + |
| 54 | + async def code(self): |
| 55 | + return await self._call.code() |
| 56 | + |
| 57 | + async def details(self): |
| 58 | + return await self._call.details() |
| 59 | + |
| 60 | + def cancelled(self): |
| 61 | + return self._call.cancelled() |
| 62 | + |
| 63 | + def done(self): |
| 64 | + return self._call.done() |
| 65 | + |
| 66 | + def time_remaining(self): |
| 67 | + return self._call.time_remaining() |
| 68 | + |
| 69 | + def cancel(self): |
| 70 | + return self._call.cancel() |
| 71 | + |
| 72 | + def add_done_callback(self, callback): |
| 73 | + self._call.add_done_callback(callback) |
| 74 | + |
| 75 | + async def wait_for_connection(self): |
| 76 | + try: |
| 77 | + await self._call.wait_for_connection() |
| 78 | + except grpc.RpcError as rpc_error: |
| 79 | + raise exceptions.from_grpc_error(rpc_error) from rpc_error |
| 80 | + |
| 81 | + |
| 82 | +class _WrappedUnaryResponseMixin(_WrappedCall): |
| 83 | + |
| 84 | + def __await__(self): |
| 85 | + try: |
| 86 | + response = yield from self._call.__await__() |
| 87 | + return response |
| 88 | + except grpc.RpcError as rpc_error: |
| 89 | + raise exceptions.from_grpc_error(rpc_error) from rpc_error |
| 90 | + |
| 91 | + |
| 92 | +class _WrappedStreamResponseMixin(_WrappedCall): |
| 93 | + |
| 94 | + def __init__(self): |
| 95 | + self._wrapped_async_generator = None |
| 96 | + |
| 97 | + async def read(self): |
| 98 | + try: |
| 99 | + return await self._call.read() |
| 100 | + except grpc.RpcError as rpc_error: |
| 101 | + raise exceptions.from_grpc_error(rpc_error) from rpc_error |
| 102 | + |
| 103 | + async def _wrapped_aiter(self): |
| 104 | + try: |
| 105 | + # NOTE(lidiz) coverage doesn't understand the exception raised from |
| 106 | + # __anext__ method. It is covered by test case: |
| 107 | + # test_wrap_stream_errors_aiter_non_rpc_error |
| 108 | + async for response in self._call: # pragma: no branch |
| 109 | + yield response |
| 110 | + except grpc.RpcError as rpc_error: |
| 111 | + raise exceptions.from_grpc_error(rpc_error) from rpc_error |
| 112 | + |
| 113 | + def __aiter__(self): |
| 114 | + if not self._wrapped_async_generator: |
| 115 | + self._wrapped_async_generator = self._wrapped_aiter() |
| 116 | + return self._wrapped_async_generator |
| 117 | + |
| 118 | + |
| 119 | +class _WrappedStreamRequestMixin(_WrappedCall): |
| 120 | + |
| 121 | + async def write(self, request): |
| 122 | + try: |
| 123 | + await self._call.write(request) |
| 124 | + except grpc.RpcError as rpc_error: |
| 125 | + raise exceptions.from_grpc_error(rpc_error) from rpc_error |
| 126 | + |
| 127 | + async def done_writing(self): |
| 128 | + try: |
| 129 | + await self._call.done_writing() |
| 130 | + except grpc.RpcError as rpc_error: |
| 131 | + raise exceptions.from_grpc_error(rpc_error) from rpc_error |
| 132 | + |
| 133 | + |
| 134 | +# NOTE(lidiz) Implementing each individual class separately, so we don't |
| 135 | +# expose any API that should not be seen. E.g., __aiter__ in unary-unary |
| 136 | +# RPC, or __await__ in stream-stream RPC. |
| 137 | +class _WrappedUnaryUnaryCall(_WrappedUnaryResponseMixin, aio.UnaryUnaryCall): |
| 138 | + """Wrapped UnaryUnaryCall to map exceptions.""" |
| 139 | + |
| 140 | + |
| 141 | +class _WrappedUnaryStreamCall(_WrappedStreamResponseMixin, aio.UnaryStreamCall): |
| 142 | + """Wrapped UnaryStreamCall to map exceptions.""" |
| 143 | + |
| 144 | + |
| 145 | +class _WrappedStreamUnaryCall(_WrappedUnaryResponseMixin, _WrappedStreamRequestMixin, aio.StreamUnaryCall): |
| 146 | + """Wrapped StreamUnaryCall to map exceptions.""" |
| 147 | + |
| 148 | + |
| 149 | +class _WrappedStreamStreamCall(_WrappedStreamRequestMixin, _WrappedStreamResponseMixin, aio.StreamStreamCall): |
| 150 | + """Wrapped StreamStreamCall to map exceptions.""" |
| 151 | + |
| 152 | + |
| 153 | +def _wrap_unary_errors(callable_): |
| 154 | + """Map errors for Unary-Unary async callables.""" |
| 155 | + grpc_helpers._patch_callable_name(callable_) |
| 156 | + |
| 157 | + @functools.wraps(callable_) |
| 158 | + def error_remapped_callable(*args, **kwargs): |
| 159 | + call = callable_(*args, **kwargs) |
| 160 | + return _WrappedUnaryUnaryCall().with_call(call) |
| 161 | + |
| 162 | + return error_remapped_callable |
| 163 | + |
| 164 | + |
| 165 | +def _wrap_stream_errors(callable_): |
| 166 | + """Map errors for streaming RPC async callables.""" |
| 167 | + grpc_helpers._patch_callable_name(callable_) |
| 168 | + |
| 169 | + @functools.wraps(callable_) |
| 170 | + async def error_remapped_callable(*args, **kwargs): |
| 171 | + call = callable_(*args, **kwargs) |
| 172 | + |
| 173 | + if isinstance(call, aio.UnaryStreamCall): |
| 174 | + call = _WrappedUnaryStreamCall().with_call(call) |
| 175 | + elif isinstance(call, aio.StreamUnaryCall): |
| 176 | + call = _WrappedStreamUnaryCall().with_call(call) |
| 177 | + elif isinstance(call, aio.StreamStreamCall): |
| 178 | + call = _WrappedStreamStreamCall().with_call(call) |
| 179 | + else: |
| 180 | + raise TypeError('Unexpected type of call %s' % type(call)) |
| 181 | + |
| 182 | + await call.wait_for_connection() |
| 183 | + return call |
| 184 | + |
| 185 | + return error_remapped_callable |
| 186 | + |
| 187 | + |
| 188 | +def wrap_errors(callable_): |
| 189 | + """Wrap a gRPC async callable and map :class:`grpc.RpcErrors` to |
| 190 | + friendly error classes. |
| 191 | +
|
| 192 | + Errors raised by the gRPC callable are mapped to the appropriate |
| 193 | + :class:`google.api_core.exceptions.GoogleAPICallError` subclasses. The |
| 194 | + original `grpc.RpcError` (which is usually also a `grpc.Call`) is |
| 195 | + available from the ``response`` property on the mapped exception. This |
| 196 | + is useful for extracting metadata from the original error. |
| 197 | +
|
| 198 | + Args: |
| 199 | + callable_ (Callable): A gRPC callable. |
| 200 | +
|
| 201 | + Returns: Callable: The wrapped gRPC callable. |
| 202 | + """ |
| 203 | + if isinstance(callable_, aio.UnaryUnaryMultiCallable): |
| 204 | + return _wrap_unary_errors(callable_) |
| 205 | + else: |
| 206 | + return _wrap_stream_errors(callable_) |
| 207 | + |
| 208 | + |
| 209 | +def create_channel(target, credentials=None, scopes=None, ssl_credentials=None, **kwargs): |
| 210 | + """Create an AsyncIO secure channel with credentials. |
| 211 | +
|
| 212 | + Args: |
| 213 | + target (str): The target service address in the format 'hostname:port'. |
| 214 | + credentials (google.auth.credentials.Credentials): The credentials. If |
| 215 | + not specified, then this function will attempt to ascertain the |
| 216 | + credentials from the environment using :func:`google.auth.default`. |
| 217 | + scopes (Sequence[str]): A optional list of scopes needed for this |
| 218 | + service. These are only used when credentials are not specified and |
| 219 | + are passed to :func:`google.auth.default`. |
| 220 | + ssl_credentials (grpc.ChannelCredentials): Optional SSL channel |
| 221 | + credentials. This can be used to specify different certificates. |
| 222 | + kwargs: Additional key-word args passed to :func:`aio.secure_channel`. |
| 223 | +
|
| 224 | + Returns: |
| 225 | + aio.Channel: The created channel. |
| 226 | + """ |
| 227 | + composite_credentials = grpc_helpers._create_composite_credentials( |
| 228 | + credentials, scopes, ssl_credentials |
| 229 | + ) |
| 230 | + |
| 231 | + return aio.secure_channel(target, composite_credentials, **kwargs) |
| 232 | + |
| 233 | + |
| 234 | +class FakeUnaryUnaryCall(_WrappedUnaryUnaryCall): |
| 235 | + """Fake implementation for unary-unary RPCs. |
| 236 | +
|
| 237 | + It is a dummy object for response message. Supply the intended response |
| 238 | + upon the initialization, and the coroutine will return the exact response |
| 239 | + message. |
| 240 | + """ |
| 241 | + |
| 242 | + def __init__(self, response=object()): |
| 243 | + self.response = response |
| 244 | + self._future = asyncio.get_event_loop().create_future() |
| 245 | + self._future.set_result(self.response) |
| 246 | + |
| 247 | + def __await__(self): |
| 248 | + response = yield from self._future.__await__() |
| 249 | + return response |
| 250 | + |
| 251 | + |
| 252 | +class FakeStreamUnaryCall(_WrappedStreamUnaryCall): |
| 253 | + """Fake implementation for stream-unary RPCs. |
| 254 | +
|
| 255 | + It is a dummy object for response message. Supply the intended response |
| 256 | + upon the initialization, and the coroutine will return the exact response |
| 257 | + message. |
| 258 | + """ |
| 259 | + |
| 260 | + def __init__(self, response=object()): |
| 261 | + self.response = response |
| 262 | + self._future = asyncio.get_event_loop().create_future() |
| 263 | + self._future.set_result(self.response) |
| 264 | + |
| 265 | + def __await__(self): |
| 266 | + response = yield from self._future.__await__() |
| 267 | + return response |
| 268 | + |
| 269 | + async def wait_for_connection(self): |
| 270 | + pass |
0 commit comments