grpcio-fips 1.53.2__0-cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. grpc/__init__.py +2174 -0
  2. grpc/_auth.py +68 -0
  3. grpc/_channel.py +1767 -0
  4. grpc/_common.py +177 -0
  5. grpc/_compression.py +63 -0
  6. grpc/_cython/__init__.py +13 -0
  7. grpc/_cython/_credentials/roots.pem +4337 -0
  8. grpc/_cython/_cygrpc/__init__.py +13 -0
  9. grpc/_cython/cygrpc.cp310-win_amd64.pyd +0 -0
  10. grpc/_grpcio_metadata.py +1 -0
  11. grpc/_interceptor.py +638 -0
  12. grpc/_plugin_wrapping.py +121 -0
  13. grpc/_runtime_protos.py +159 -0
  14. grpc/_server.py +1141 -0
  15. grpc/_simple_stubs.py +486 -0
  16. grpc/_typing.py +58 -0
  17. grpc/_utilities.py +180 -0
  18. grpc/aio/__init__.py +95 -0
  19. grpc/aio/_base_call.py +248 -0
  20. grpc/aio/_base_channel.py +348 -0
  21. grpc/aio/_base_server.py +369 -0
  22. grpc/aio/_call.py +649 -0
  23. grpc/aio/_channel.py +492 -0
  24. grpc/aio/_interceptor.py +1003 -0
  25. grpc/aio/_metadata.py +120 -0
  26. grpc/aio/_server.py +209 -0
  27. grpc/aio/_typing.py +35 -0
  28. grpc/aio/_utils.py +22 -0
  29. grpc/beta/__init__.py +13 -0
  30. grpc/beta/_client_adaptations.py +706 -0
  31. grpc/beta/_metadata.py +52 -0
  32. grpc/beta/_server_adaptations.py +385 -0
  33. grpc/beta/implementations.py +311 -0
  34. grpc/beta/interfaces.py +163 -0
  35. grpc/beta/utilities.py +149 -0
  36. grpc/experimental/__init__.py +128 -0
  37. grpc/experimental/aio/__init__.py +16 -0
  38. grpc/experimental/gevent.py +27 -0
  39. grpc/experimental/session_cache.py +45 -0
  40. grpc/framework/__init__.py +13 -0
  41. grpc/framework/common/__init__.py +13 -0
  42. grpc/framework/common/cardinality.py +26 -0
  43. grpc/framework/common/style.py +24 -0
  44. grpc/framework/foundation/__init__.py +13 -0
  45. grpc/framework/foundation/abandonment.py +22 -0
  46. grpc/framework/foundation/callable_util.py +94 -0
  47. grpc/framework/foundation/future.py +219 -0
  48. grpc/framework/foundation/logging_pool.py +71 -0
  49. grpc/framework/foundation/stream.py +43 -0
  50. grpc/framework/foundation/stream_util.py +148 -0
  51. grpc/framework/interfaces/__init__.py +13 -0
  52. grpc/framework/interfaces/base/__init__.py +13 -0
  53. grpc/framework/interfaces/base/base.py +325 -0
  54. grpc/framework/interfaces/base/utilities.py +71 -0
  55. grpc/framework/interfaces/face/__init__.py +13 -0
  56. grpc/framework/interfaces/face/face.py +1049 -0
  57. grpc/framework/interfaces/face/utilities.py +168 -0
  58. grpcio_fips-1.53.2.dist-info/LICENSE +610 -0
  59. grpcio_fips-1.53.2.dist-info/METADATA +137 -0
  60. grpcio_fips-1.53.2.dist-info/RECORD +62 -0
  61. grpcio_fips-1.53.2.dist-info/WHEEL +5 -0
  62. grpcio_fips-1.53.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1003 @@
1
+ # Copyright 2019 gRPC authors.
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
+ """Interceptors implementation of gRPC Asyncio Python."""
15
+ from abc import ABCMeta
16
+ from abc import abstractmethod
17
+ import asyncio
18
+ import collections
19
+ import functools
20
+ from typing import (AsyncIterable, Awaitable, Callable, Iterator, Optional,
21
+ Sequence, Union)
22
+
23
+ import grpc
24
+ from grpc._cython import cygrpc
25
+
26
+ from . import _base_call
27
+ from ._call import AioRpcError
28
+ from ._call import StreamStreamCall
29
+ from ._call import StreamUnaryCall
30
+ from ._call import UnaryStreamCall
31
+ from ._call import UnaryUnaryCall
32
+ from ._call import _API_STYLE_ERROR
33
+ from ._call import _RPC_ALREADY_FINISHED_DETAILS
34
+ from ._call import _RPC_HALF_CLOSED_DETAILS
35
+ from ._metadata import Metadata
36
+ from ._typing import DeserializingFunction
37
+ from ._typing import DoneCallbackType
38
+ from ._typing import RequestIterableType
39
+ from ._typing import RequestType
40
+ from ._typing import ResponseIterableType
41
+ from ._typing import ResponseType
42
+ from ._typing import SerializingFunction
43
+ from ._utils import _timeout_to_deadline
44
+
45
+ _LOCAL_CANCELLATION_DETAILS = 'Locally cancelled by application!'
46
+
47
+
48
+ class ServerInterceptor(metaclass=ABCMeta):
49
+ """Affords intercepting incoming RPCs on the service-side.
50
+
51
+ This is an EXPERIMENTAL API.
52
+ """
53
+
54
+ @abstractmethod
55
+ async def intercept_service(
56
+ self, continuation: Callable[[grpc.HandlerCallDetails],
57
+ Awaitable[grpc.RpcMethodHandler]],
58
+ handler_call_details: grpc.HandlerCallDetails
59
+ ) -> grpc.RpcMethodHandler:
60
+ """Intercepts incoming RPCs before handing them over to a handler.
61
+
62
+ Args:
63
+ continuation: A function that takes a HandlerCallDetails and
64
+ proceeds to invoke the next interceptor in the chain, if any,
65
+ or the RPC handler lookup logic, with the call details passed
66
+ as an argument, and returns an RpcMethodHandler instance if
67
+ the RPC is considered serviced, or None otherwise.
68
+ handler_call_details: A HandlerCallDetails describing the RPC.
69
+
70
+ Returns:
71
+ An RpcMethodHandler with which the RPC may be serviced if the
72
+ interceptor chooses to service this RPC, or None otherwise.
73
+ """
74
+
75
+
76
+ class ClientCallDetails(
77
+ collections.namedtuple(
78
+ 'ClientCallDetails',
79
+ ('method', 'timeout', 'metadata', 'credentials', 'wait_for_ready')),
80
+ grpc.ClientCallDetails):
81
+ """Describes an RPC to be invoked.
82
+
83
+ This is an EXPERIMENTAL API.
84
+
85
+ Args:
86
+ method: The method name of the RPC.
87
+ timeout: An optional duration of time in seconds to allow for the RPC.
88
+ metadata: Optional metadata to be transmitted to the service-side of
89
+ the RPC.
90
+ credentials: An optional CallCredentials for the RPC.
91
+ wait_for_ready: An optional flag to enable :term:`wait_for_ready` mechanism.
92
+ """
93
+
94
+ method: str
95
+ timeout: Optional[float]
96
+ metadata: Optional[Metadata]
97
+ credentials: Optional[grpc.CallCredentials]
98
+ wait_for_ready: Optional[bool]
99
+
100
+
101
+ class ClientInterceptor(metaclass=ABCMeta):
102
+ """Base class used for all Aio Client Interceptor classes"""
103
+
104
+
105
+ class UnaryUnaryClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
106
+ """Affords intercepting unary-unary invocations."""
107
+
108
+ @abstractmethod
109
+ async def intercept_unary_unary(
110
+ self, continuation: Callable[[ClientCallDetails, RequestType],
111
+ UnaryUnaryCall],
112
+ client_call_details: ClientCallDetails,
113
+ request: RequestType) -> Union[UnaryUnaryCall, ResponseType]:
114
+ """Intercepts a unary-unary invocation asynchronously.
115
+
116
+ Args:
117
+ continuation: A coroutine that proceeds with the invocation by
118
+ executing the next interceptor in the chain or invoking the
119
+ actual RPC on the underlying Channel. It is the interceptor's
120
+ responsibility to call it if it decides to move the RPC forward.
121
+ The interceptor can use
122
+ `call = await continuation(client_call_details, request)`
123
+ to continue with the RPC. `continuation` returns the call to the
124
+ RPC.
125
+ client_call_details: A ClientCallDetails object describing the
126
+ outgoing RPC.
127
+ request: The request value for the RPC.
128
+
129
+ Returns:
130
+ An object with the RPC response.
131
+
132
+ Raises:
133
+ AioRpcError: Indicating that the RPC terminated with non-OK status.
134
+ asyncio.CancelledError: Indicating that the RPC was canceled.
135
+ """
136
+
137
+
138
+ class UnaryStreamClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
139
+ """Affords intercepting unary-stream invocations."""
140
+
141
+ @abstractmethod
142
+ async def intercept_unary_stream(
143
+ self, continuation: Callable[[ClientCallDetails, RequestType],
144
+ UnaryStreamCall],
145
+ client_call_details: ClientCallDetails, request: RequestType
146
+ ) -> Union[ResponseIterableType, UnaryStreamCall]:
147
+ """Intercepts a unary-stream invocation asynchronously.
148
+
149
+ The function could return the call object or an asynchronous
150
+ iterator, in case of being an asyncrhonous iterator this will
151
+ become the source of the reads done by the caller.
152
+
153
+ Args:
154
+ continuation: A coroutine that proceeds with the invocation by
155
+ executing the next interceptor in the chain or invoking the
156
+ actual RPC on the underlying Channel. It is the interceptor's
157
+ responsibility to call it if it decides to move the RPC forward.
158
+ The interceptor can use
159
+ `call = await continuation(client_call_details, request)`
160
+ to continue with the RPC. `continuation` returns the call to the
161
+ RPC.
162
+ client_call_details: A ClientCallDetails object describing the
163
+ outgoing RPC.
164
+ request: The request value for the RPC.
165
+
166
+ Returns:
167
+ The RPC Call or an asynchronous iterator.
168
+
169
+ Raises:
170
+ AioRpcError: Indicating that the RPC terminated with non-OK status.
171
+ asyncio.CancelledError: Indicating that the RPC was canceled.
172
+ """
173
+
174
+
175
+ class StreamUnaryClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
176
+ """Affords intercepting stream-unary invocations."""
177
+
178
+ @abstractmethod
179
+ async def intercept_stream_unary(
180
+ self,
181
+ continuation: Callable[[ClientCallDetails, RequestType],
182
+ StreamUnaryCall],
183
+ client_call_details: ClientCallDetails,
184
+ request_iterator: RequestIterableType,
185
+ ) -> StreamUnaryCall:
186
+ """Intercepts a stream-unary invocation asynchronously.
187
+
188
+ Within the interceptor the usage of the call methods like `write` or
189
+ even awaiting the call should be done carefully, since the caller
190
+ could be expecting an untouched call, for example for start writing
191
+ messages to it.
192
+
193
+ Args:
194
+ continuation: A coroutine that proceeds with the invocation by
195
+ executing the next interceptor in the chain or invoking the
196
+ actual RPC on the underlying Channel. It is the interceptor's
197
+ responsibility to call it if it decides to move the RPC forward.
198
+ The interceptor can use
199
+ `call = await continuation(client_call_details, request_iterator)`
200
+ to continue with the RPC. `continuation` returns the call to the
201
+ RPC.
202
+ client_call_details: A ClientCallDetails object describing the
203
+ outgoing RPC.
204
+ request_iterator: The request iterator that will produce requests
205
+ for the RPC.
206
+
207
+ Returns:
208
+ The RPC Call.
209
+
210
+ Raises:
211
+ AioRpcError: Indicating that the RPC terminated with non-OK status.
212
+ asyncio.CancelledError: Indicating that the RPC was canceled.
213
+ """
214
+
215
+
216
+ class StreamStreamClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
217
+ """Affords intercepting stream-stream invocations."""
218
+
219
+ @abstractmethod
220
+ async def intercept_stream_stream(
221
+ self,
222
+ continuation: Callable[[ClientCallDetails, RequestType],
223
+ StreamStreamCall],
224
+ client_call_details: ClientCallDetails,
225
+ request_iterator: RequestIterableType,
226
+ ) -> Union[ResponseIterableType, StreamStreamCall]:
227
+ """Intercepts a stream-stream invocation asynchronously.
228
+
229
+ Within the interceptor the usage of the call methods like `write` or
230
+ even awaiting the call should be done carefully, since the caller
231
+ could be expecting an untouched call, for example for start writing
232
+ messages to it.
233
+
234
+ The function could return the call object or an asynchronous
235
+ iterator, in case of being an asyncrhonous iterator this will
236
+ become the source of the reads done by the caller.
237
+
238
+ Args:
239
+ continuation: A coroutine that proceeds with the invocation by
240
+ executing the next interceptor in the chain or invoking the
241
+ actual RPC on the underlying Channel. It is the interceptor's
242
+ responsibility to call it if it decides to move the RPC forward.
243
+ The interceptor can use
244
+ `call = await continuation(client_call_details, request_iterator)`
245
+ to continue with the RPC. `continuation` returns the call to the
246
+ RPC.
247
+ client_call_details: A ClientCallDetails object describing the
248
+ outgoing RPC.
249
+ request_iterator: The request iterator that will produce requests
250
+ for the RPC.
251
+
252
+ Returns:
253
+ The RPC Call or an asynchronous iterator.
254
+
255
+ Raises:
256
+ AioRpcError: Indicating that the RPC terminated with non-OK status.
257
+ asyncio.CancelledError: Indicating that the RPC was canceled.
258
+ """
259
+
260
+
261
+ class InterceptedCall:
262
+ """Base implementation for all intercepted call arities.
263
+
264
+ Interceptors might have some work to do before the RPC invocation with
265
+ the capacity of changing the invocation parameters, and some work to do
266
+ after the RPC invocation with the capacity for accessing to the wrapped
267
+ `UnaryUnaryCall`.
268
+
269
+ It handles also early and later cancellations, when the RPC has not even
270
+ started and the execution is still held by the interceptors or when the
271
+ RPC has finished but again the execution is still held by the interceptors.
272
+
273
+ Once the RPC is finally executed, all methods are finally done against the
274
+ intercepted call, being at the same time the same call returned to the
275
+ interceptors.
276
+
277
+ As a base class for all of the interceptors implements the logic around
278
+ final status, metadata and cancellation.
279
+ """
280
+
281
+ _interceptors_task: asyncio.Task
282
+ _pending_add_done_callbacks: Sequence[DoneCallbackType]
283
+
284
+ def __init__(self, interceptors_task: asyncio.Task) -> None:
285
+ self._interceptors_task = interceptors_task
286
+ self._pending_add_done_callbacks = []
287
+ self._interceptors_task.add_done_callback(
288
+ self._fire_or_add_pending_done_callbacks)
289
+
290
+ def __del__(self):
291
+ self.cancel()
292
+
293
+ def _fire_or_add_pending_done_callbacks(
294
+ self, interceptors_task: asyncio.Task) -> None:
295
+
296
+ if not self._pending_add_done_callbacks:
297
+ return
298
+
299
+ call_completed = False
300
+
301
+ try:
302
+ call = interceptors_task.result()
303
+ if call.done():
304
+ call_completed = True
305
+ except (AioRpcError, asyncio.CancelledError):
306
+ call_completed = True
307
+
308
+ if call_completed:
309
+ for callback in self._pending_add_done_callbacks:
310
+ callback(self)
311
+ else:
312
+ for callback in self._pending_add_done_callbacks:
313
+ callback = functools.partial(self._wrap_add_done_callback,
314
+ callback)
315
+ call.add_done_callback(callback)
316
+
317
+ self._pending_add_done_callbacks = []
318
+
319
+ def _wrap_add_done_callback(self, callback: DoneCallbackType,
320
+ unused_call: _base_call.Call) -> None:
321
+ callback(self)
322
+
323
+ def cancel(self) -> bool:
324
+ if not self._interceptors_task.done():
325
+ # There is no yet the intercepted call available,
326
+ # Trying to cancel it by using the generic Asyncio
327
+ # cancellation method.
328
+ return self._interceptors_task.cancel()
329
+
330
+ try:
331
+ call = self._interceptors_task.result()
332
+ except AioRpcError:
333
+ return False
334
+ except asyncio.CancelledError:
335
+ return False
336
+
337
+ return call.cancel()
338
+
339
+ def cancelled(self) -> bool:
340
+ if not self._interceptors_task.done():
341
+ return False
342
+
343
+ try:
344
+ call = self._interceptors_task.result()
345
+ except AioRpcError as err:
346
+ return err.code() == grpc.StatusCode.CANCELLED
347
+ except asyncio.CancelledError:
348
+ return True
349
+
350
+ return call.cancelled()
351
+
352
+ def done(self) -> bool:
353
+ if not self._interceptors_task.done():
354
+ return False
355
+
356
+ try:
357
+ call = self._interceptors_task.result()
358
+ except (AioRpcError, asyncio.CancelledError):
359
+ return True
360
+
361
+ return call.done()
362
+
363
+ def add_done_callback(self, callback: DoneCallbackType) -> None:
364
+ if not self._interceptors_task.done():
365
+ self._pending_add_done_callbacks.append(callback)
366
+ return
367
+
368
+ try:
369
+ call = self._interceptors_task.result()
370
+ except (AioRpcError, asyncio.CancelledError):
371
+ callback(self)
372
+ return
373
+
374
+ if call.done():
375
+ callback(self)
376
+ else:
377
+ callback = functools.partial(self._wrap_add_done_callback, callback)
378
+ call.add_done_callback(callback)
379
+
380
+ def time_remaining(self) -> Optional[float]:
381
+ raise NotImplementedError()
382
+
383
+ async def initial_metadata(self) -> Optional[Metadata]:
384
+ try:
385
+ call = await self._interceptors_task
386
+ except AioRpcError as err:
387
+ return err.initial_metadata()
388
+ except asyncio.CancelledError:
389
+ return None
390
+
391
+ return await call.initial_metadata()
392
+
393
+ async def trailing_metadata(self) -> Optional[Metadata]:
394
+ try:
395
+ call = await self._interceptors_task
396
+ except AioRpcError as err:
397
+ return err.trailing_metadata()
398
+ except asyncio.CancelledError:
399
+ return None
400
+
401
+ return await call.trailing_metadata()
402
+
403
+ async def code(self) -> grpc.StatusCode:
404
+ try:
405
+ call = await self._interceptors_task
406
+ except AioRpcError as err:
407
+ return err.code()
408
+ except asyncio.CancelledError:
409
+ return grpc.StatusCode.CANCELLED
410
+
411
+ return await call.code()
412
+
413
+ async def details(self) -> str:
414
+ try:
415
+ call = await self._interceptors_task
416
+ except AioRpcError as err:
417
+ return err.details()
418
+ except asyncio.CancelledError:
419
+ return _LOCAL_CANCELLATION_DETAILS
420
+
421
+ return await call.details()
422
+
423
+ async def debug_error_string(self) -> Optional[str]:
424
+ try:
425
+ call = await self._interceptors_task
426
+ except AioRpcError as err:
427
+ return err.debug_error_string()
428
+ except asyncio.CancelledError:
429
+ return ''
430
+
431
+ return await call.debug_error_string()
432
+
433
+ async def wait_for_connection(self) -> None:
434
+ call = await self._interceptors_task
435
+ return await call.wait_for_connection()
436
+
437
+
438
+ class _InterceptedUnaryResponseMixin:
439
+
440
+ def __await__(self):
441
+ call = yield from self._interceptors_task.__await__()
442
+ response = yield from call.__await__()
443
+ return response
444
+
445
+
446
+ class _InterceptedStreamResponseMixin:
447
+ _response_aiter: Optional[AsyncIterable[ResponseType]]
448
+
449
+ def _init_stream_response_mixin(self) -> None:
450
+ # Is initalized later, otherwise if the iterator is not finnally
451
+ # consumed a logging warning is emmited by Asyncio.
452
+ self._response_aiter = None
453
+
454
+ async def _wait_for_interceptor_task_response_iterator(
455
+ self) -> ResponseType:
456
+ call = await self._interceptors_task
457
+ async for response in call:
458
+ yield response
459
+
460
+ def __aiter__(self) -> AsyncIterable[ResponseType]:
461
+ if self._response_aiter is None:
462
+ self._response_aiter = self._wait_for_interceptor_task_response_iterator(
463
+ )
464
+ return self._response_aiter
465
+
466
+ async def read(self) -> ResponseType:
467
+ if self._response_aiter is None:
468
+ self._response_aiter = self._wait_for_interceptor_task_response_iterator(
469
+ )
470
+ return await self._response_aiter.asend(None)
471
+
472
+
473
+ class _InterceptedStreamRequestMixin:
474
+
475
+ _write_to_iterator_async_gen: Optional[AsyncIterable[RequestType]]
476
+ _write_to_iterator_queue: Optional[asyncio.Queue]
477
+ _status_code_task: Optional[asyncio.Task]
478
+
479
+ _FINISH_ITERATOR_SENTINEL = object()
480
+
481
+ def _init_stream_request_mixin(
482
+ self, request_iterator: Optional[RequestIterableType]
483
+ ) -> RequestIterableType:
484
+
485
+ if request_iterator is None:
486
+ # We provide our own request iterator which is a proxy
487
+ # of the futures writes that will be done by the caller.
488
+ self._write_to_iterator_queue = asyncio.Queue(maxsize=1)
489
+ self._write_to_iterator_async_gen = self._proxy_writes_as_request_iterator(
490
+ )
491
+ self._status_code_task = None
492
+ request_iterator = self._write_to_iterator_async_gen
493
+ else:
494
+ self._write_to_iterator_queue = None
495
+
496
+ return request_iterator
497
+
498
+ async def _proxy_writes_as_request_iterator(self):
499
+ await self._interceptors_task
500
+
501
+ while True:
502
+ value = await self._write_to_iterator_queue.get()
503
+ if value is _InterceptedStreamRequestMixin._FINISH_ITERATOR_SENTINEL:
504
+ break
505
+ yield value
506
+
507
+ async def _write_to_iterator_queue_interruptible(self, request: RequestType,
508
+ call: InterceptedCall):
509
+ # Write the specified 'request' to the request iterator queue using the
510
+ # specified 'call' to allow for interruption of the write in the case
511
+ # of abrupt termination of the call.
512
+ if self._status_code_task is None:
513
+ self._status_code_task = self._loop.create_task(call.code())
514
+
515
+ await asyncio.wait(
516
+ (self._loop.create_task(self._write_to_iterator_queue.put(request)),
517
+ self._status_code_task),
518
+ return_when=asyncio.FIRST_COMPLETED)
519
+
520
+ async def write(self, request: RequestType) -> None:
521
+ # If no queue was created it means that requests
522
+ # should be expected through an iterators provided
523
+ # by the caller.
524
+ if self._write_to_iterator_queue is None:
525
+ raise cygrpc.UsageError(_API_STYLE_ERROR)
526
+
527
+ try:
528
+ call = await self._interceptors_task
529
+ except (asyncio.CancelledError, AioRpcError):
530
+ raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
531
+
532
+ if call.done():
533
+ raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
534
+ elif call._done_writing_flag:
535
+ raise asyncio.InvalidStateError(_RPC_HALF_CLOSED_DETAILS)
536
+
537
+ await self._write_to_iterator_queue_interruptible(request, call)
538
+
539
+ if call.done():
540
+ raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
541
+
542
+ async def done_writing(self) -> None:
543
+ """Signal peer that client is done writing.
544
+
545
+ This method is idempotent.
546
+ """
547
+ # If no queue was created it means that requests
548
+ # should be expected through an iterators provided
549
+ # by the caller.
550
+ if self._write_to_iterator_queue is None:
551
+ raise cygrpc.UsageError(_API_STYLE_ERROR)
552
+
553
+ try:
554
+ call = await self._interceptors_task
555
+ except asyncio.CancelledError:
556
+ raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
557
+
558
+ await self._write_to_iterator_queue_interruptible(
559
+ _InterceptedStreamRequestMixin._FINISH_ITERATOR_SENTINEL, call)
560
+
561
+
562
+ class InterceptedUnaryUnaryCall(_InterceptedUnaryResponseMixin, InterceptedCall,
563
+ _base_call.UnaryUnaryCall):
564
+ """Used for running a `UnaryUnaryCall` wrapped by interceptors.
565
+
566
+ For the `__await__` method is it is proxied to the intercepted call only when
567
+ the interceptor task is finished.
568
+ """
569
+
570
+ _loop: asyncio.AbstractEventLoop
571
+ _channel: cygrpc.AioChannel
572
+
573
+ # pylint: disable=too-many-arguments
574
+ def __init__(self, interceptors: Sequence[UnaryUnaryClientInterceptor],
575
+ request: RequestType, timeout: Optional[float],
576
+ metadata: Metadata,
577
+ credentials: Optional[grpc.CallCredentials],
578
+ wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
579
+ method: bytes, request_serializer: SerializingFunction,
580
+ response_deserializer: DeserializingFunction,
581
+ loop: asyncio.AbstractEventLoop) -> None:
582
+ self._loop = loop
583
+ self._channel = channel
584
+ interceptors_task = loop.create_task(
585
+ self._invoke(interceptors, method, timeout, metadata, credentials,
586
+ wait_for_ready, request, request_serializer,
587
+ response_deserializer))
588
+ super().__init__(interceptors_task)
589
+
590
+ # pylint: disable=too-many-arguments
591
+ async def _invoke(
592
+ self, interceptors: Sequence[UnaryUnaryClientInterceptor],
593
+ method: bytes, timeout: Optional[float],
594
+ metadata: Optional[Metadata],
595
+ credentials: Optional[grpc.CallCredentials],
596
+ wait_for_ready: Optional[bool], request: RequestType,
597
+ request_serializer: SerializingFunction,
598
+ response_deserializer: DeserializingFunction) -> UnaryUnaryCall:
599
+ """Run the RPC call wrapped in interceptors"""
600
+
601
+ async def _run_interceptor(
602
+ interceptors: Iterator[UnaryUnaryClientInterceptor],
603
+ client_call_details: ClientCallDetails,
604
+ request: RequestType) -> _base_call.UnaryUnaryCall:
605
+
606
+ interceptor = next(interceptors, None)
607
+
608
+ if interceptor:
609
+ continuation = functools.partial(_run_interceptor, interceptors)
610
+
611
+ call_or_response = await interceptor.intercept_unary_unary(
612
+ continuation, client_call_details, request)
613
+
614
+ if isinstance(call_or_response, _base_call.UnaryUnaryCall):
615
+ return call_or_response
616
+ else:
617
+ return UnaryUnaryCallResponse(call_or_response)
618
+
619
+ else:
620
+ return UnaryUnaryCall(
621
+ request, _timeout_to_deadline(client_call_details.timeout),
622
+ client_call_details.metadata,
623
+ client_call_details.credentials,
624
+ client_call_details.wait_for_ready, self._channel,
625
+ client_call_details.method, request_serializer,
626
+ response_deserializer, self._loop)
627
+
628
+ client_call_details = ClientCallDetails(method, timeout, metadata,
629
+ credentials, wait_for_ready)
630
+ return await _run_interceptor(iter(interceptors), client_call_details,
631
+ request)
632
+
633
+ def time_remaining(self) -> Optional[float]:
634
+ raise NotImplementedError()
635
+
636
+
637
+ class InterceptedUnaryStreamCall(_InterceptedStreamResponseMixin,
638
+ InterceptedCall, _base_call.UnaryStreamCall):
639
+ """Used for running a `UnaryStreamCall` wrapped by interceptors."""
640
+
641
+ _loop: asyncio.AbstractEventLoop
642
+ _channel: cygrpc.AioChannel
643
+ _last_returned_call_from_interceptors = Optional[_base_call.UnaryStreamCall]
644
+
645
+ # pylint: disable=too-many-arguments
646
+ def __init__(self, interceptors: Sequence[UnaryStreamClientInterceptor],
647
+ request: RequestType, timeout: Optional[float],
648
+ metadata: Metadata,
649
+ credentials: Optional[grpc.CallCredentials],
650
+ wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
651
+ method: bytes, request_serializer: SerializingFunction,
652
+ response_deserializer: DeserializingFunction,
653
+ loop: asyncio.AbstractEventLoop) -> None:
654
+ self._loop = loop
655
+ self._channel = channel
656
+ self._init_stream_response_mixin()
657
+ self._last_returned_call_from_interceptors = None
658
+ interceptors_task = loop.create_task(
659
+ self._invoke(interceptors, method, timeout, metadata, credentials,
660
+ wait_for_ready, request, request_serializer,
661
+ response_deserializer))
662
+ super().__init__(interceptors_task)
663
+
664
+ # pylint: disable=too-many-arguments
665
+ async def _invoke(
666
+ self, interceptors: Sequence[UnaryUnaryClientInterceptor],
667
+ method: bytes, timeout: Optional[float],
668
+ metadata: Optional[Metadata],
669
+ credentials: Optional[grpc.CallCredentials],
670
+ wait_for_ready: Optional[bool], request: RequestType,
671
+ request_serializer: SerializingFunction,
672
+ response_deserializer: DeserializingFunction) -> UnaryStreamCall:
673
+ """Run the RPC call wrapped in interceptors"""
674
+
675
+ async def _run_interceptor(
676
+ interceptors: Iterator[UnaryStreamClientInterceptor],
677
+ client_call_details: ClientCallDetails,
678
+ request: RequestType,
679
+ ) -> _base_call.UnaryUnaryCall:
680
+
681
+ interceptor = next(interceptors, None)
682
+
683
+ if interceptor:
684
+ continuation = functools.partial(_run_interceptor, interceptors)
685
+
686
+ call_or_response_iterator = await interceptor.intercept_unary_stream(
687
+ continuation, client_call_details, request)
688
+
689
+ if isinstance(call_or_response_iterator,
690
+ _base_call.UnaryStreamCall):
691
+ self._last_returned_call_from_interceptors = call_or_response_iterator
692
+ else:
693
+ self._last_returned_call_from_interceptors = UnaryStreamCallResponseIterator(
694
+ self._last_returned_call_from_interceptors,
695
+ call_or_response_iterator)
696
+ return self._last_returned_call_from_interceptors
697
+ else:
698
+ self._last_returned_call_from_interceptors = UnaryStreamCall(
699
+ request, _timeout_to_deadline(client_call_details.timeout),
700
+ client_call_details.metadata,
701
+ client_call_details.credentials,
702
+ client_call_details.wait_for_ready, self._channel,
703
+ client_call_details.method, request_serializer,
704
+ response_deserializer, self._loop)
705
+
706
+ return self._last_returned_call_from_interceptors
707
+
708
+ client_call_details = ClientCallDetails(method, timeout, metadata,
709
+ credentials, wait_for_ready)
710
+ return await _run_interceptor(iter(interceptors), client_call_details,
711
+ request)
712
+
713
+ def time_remaining(self) -> Optional[float]:
714
+ raise NotImplementedError()
715
+
716
+
717
+ class InterceptedStreamUnaryCall(_InterceptedUnaryResponseMixin,
718
+ _InterceptedStreamRequestMixin,
719
+ InterceptedCall, _base_call.StreamUnaryCall):
720
+ """Used for running a `StreamUnaryCall` wrapped by interceptors.
721
+
722
+ For the `__await__` method is it is proxied to the intercepted call only when
723
+ the interceptor task is finished.
724
+ """
725
+
726
+ _loop: asyncio.AbstractEventLoop
727
+ _channel: cygrpc.AioChannel
728
+
729
+ # pylint: disable=too-many-arguments
730
+ def __init__(self, interceptors: Sequence[StreamUnaryClientInterceptor],
731
+ request_iterator: Optional[RequestIterableType],
732
+ timeout: Optional[float], metadata: Metadata,
733
+ credentials: Optional[grpc.CallCredentials],
734
+ wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
735
+ method: bytes, request_serializer: SerializingFunction,
736
+ response_deserializer: DeserializingFunction,
737
+ loop: asyncio.AbstractEventLoop) -> None:
738
+ self._loop = loop
739
+ self._channel = channel
740
+ request_iterator = self._init_stream_request_mixin(request_iterator)
741
+ interceptors_task = loop.create_task(
742
+ self._invoke(interceptors, method, timeout, metadata, credentials,
743
+ wait_for_ready, request_iterator, request_serializer,
744
+ response_deserializer))
745
+ super().__init__(interceptors_task)
746
+
747
+ # pylint: disable=too-many-arguments
748
+ async def _invoke(
749
+ self, interceptors: Sequence[StreamUnaryClientInterceptor],
750
+ method: bytes, timeout: Optional[float],
751
+ metadata: Optional[Metadata],
752
+ credentials: Optional[grpc.CallCredentials],
753
+ wait_for_ready: Optional[bool],
754
+ request_iterator: RequestIterableType,
755
+ request_serializer: SerializingFunction,
756
+ response_deserializer: DeserializingFunction) -> StreamUnaryCall:
757
+ """Run the RPC call wrapped in interceptors"""
758
+
759
+ async def _run_interceptor(
760
+ interceptors: Iterator[UnaryUnaryClientInterceptor],
761
+ client_call_details: ClientCallDetails,
762
+ request_iterator: RequestIterableType
763
+ ) -> _base_call.StreamUnaryCall:
764
+
765
+ interceptor = next(interceptors, None)
766
+
767
+ if interceptor:
768
+ continuation = functools.partial(_run_interceptor, interceptors)
769
+
770
+ return await interceptor.intercept_stream_unary(
771
+ continuation, client_call_details, request_iterator)
772
+ else:
773
+ return StreamUnaryCall(
774
+ request_iterator,
775
+ _timeout_to_deadline(client_call_details.timeout),
776
+ client_call_details.metadata,
777
+ client_call_details.credentials,
778
+ client_call_details.wait_for_ready, self._channel,
779
+ client_call_details.method, request_serializer,
780
+ response_deserializer, self._loop)
781
+
782
+ client_call_details = ClientCallDetails(method, timeout, metadata,
783
+ credentials, wait_for_ready)
784
+ return await _run_interceptor(iter(interceptors), client_call_details,
785
+ request_iterator)
786
+
787
+ def time_remaining(self) -> Optional[float]:
788
+ raise NotImplementedError()
789
+
790
+
791
+ class InterceptedStreamStreamCall(_InterceptedStreamResponseMixin,
792
+ _InterceptedStreamRequestMixin,
793
+ InterceptedCall, _base_call.StreamStreamCall):
794
+ """Used for running a `StreamStreamCall` wrapped by interceptors."""
795
+
796
+ _loop: asyncio.AbstractEventLoop
797
+ _channel: cygrpc.AioChannel
798
+ _last_returned_call_from_interceptors = Optional[_base_call.UnaryStreamCall]
799
+
800
+ # pylint: disable=too-many-arguments
801
+ def __init__(self, interceptors: Sequence[StreamStreamClientInterceptor],
802
+ request_iterator: Optional[RequestIterableType],
803
+ timeout: Optional[float], metadata: Metadata,
804
+ credentials: Optional[grpc.CallCredentials],
805
+ wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
806
+ method: bytes, request_serializer: SerializingFunction,
807
+ response_deserializer: DeserializingFunction,
808
+ loop: asyncio.AbstractEventLoop) -> None:
809
+ self._loop = loop
810
+ self._channel = channel
811
+ self._init_stream_response_mixin()
812
+ request_iterator = self._init_stream_request_mixin(request_iterator)
813
+ self._last_returned_call_from_interceptors = None
814
+ interceptors_task = loop.create_task(
815
+ self._invoke(interceptors, method, timeout, metadata, credentials,
816
+ wait_for_ready, request_iterator, request_serializer,
817
+ response_deserializer))
818
+ super().__init__(interceptors_task)
819
+
820
+ # pylint: disable=too-many-arguments
821
+ async def _invoke(
822
+ self, interceptors: Sequence[StreamStreamClientInterceptor],
823
+ method: bytes, timeout: Optional[float],
824
+ metadata: Optional[Metadata],
825
+ credentials: Optional[grpc.CallCredentials],
826
+ wait_for_ready: Optional[bool],
827
+ request_iterator: RequestIterableType,
828
+ request_serializer: SerializingFunction,
829
+ response_deserializer: DeserializingFunction) -> StreamStreamCall:
830
+ """Run the RPC call wrapped in interceptors"""
831
+
832
+ async def _run_interceptor(
833
+ interceptors: Iterator[StreamStreamClientInterceptor],
834
+ client_call_details: ClientCallDetails,
835
+ request_iterator: RequestIterableType
836
+ ) -> _base_call.StreamStreamCall:
837
+
838
+ interceptor = next(interceptors, None)
839
+
840
+ if interceptor:
841
+ continuation = functools.partial(_run_interceptor, interceptors)
842
+
843
+ call_or_response_iterator = await interceptor.intercept_stream_stream(
844
+ continuation, client_call_details, request_iterator)
845
+
846
+ if isinstance(call_or_response_iterator,
847
+ _base_call.StreamStreamCall):
848
+ self._last_returned_call_from_interceptors = call_or_response_iterator
849
+ else:
850
+ self._last_returned_call_from_interceptors = StreamStreamCallResponseIterator(
851
+ self._last_returned_call_from_interceptors,
852
+ call_or_response_iterator)
853
+ return self._last_returned_call_from_interceptors
854
+ else:
855
+ self._last_returned_call_from_interceptors = StreamStreamCall(
856
+ request_iterator,
857
+ _timeout_to_deadline(client_call_details.timeout),
858
+ client_call_details.metadata,
859
+ client_call_details.credentials,
860
+ client_call_details.wait_for_ready, self._channel,
861
+ client_call_details.method, request_serializer,
862
+ response_deserializer, self._loop)
863
+ return self._last_returned_call_from_interceptors
864
+
865
+ client_call_details = ClientCallDetails(method, timeout, metadata,
866
+ credentials, wait_for_ready)
867
+ return await _run_interceptor(iter(interceptors), client_call_details,
868
+ request_iterator)
869
+
870
+ def time_remaining(self) -> Optional[float]:
871
+ raise NotImplementedError()
872
+
873
+
874
+ class UnaryUnaryCallResponse(_base_call.UnaryUnaryCall):
875
+ """Final UnaryUnaryCall class finished with a response."""
876
+ _response: ResponseType
877
+
878
+ def __init__(self, response: ResponseType) -> None:
879
+ self._response = response
880
+
881
+ def cancel(self) -> bool:
882
+ return False
883
+
884
+ def cancelled(self) -> bool:
885
+ return False
886
+
887
+ def done(self) -> bool:
888
+ return True
889
+
890
+ def add_done_callback(self, unused_callback) -> None:
891
+ raise NotImplementedError()
892
+
893
+ def time_remaining(self) -> Optional[float]:
894
+ raise NotImplementedError()
895
+
896
+ async def initial_metadata(self) -> Optional[Metadata]:
897
+ return None
898
+
899
+ async def trailing_metadata(self) -> Optional[Metadata]:
900
+ return None
901
+
902
+ async def code(self) -> grpc.StatusCode:
903
+ return grpc.StatusCode.OK
904
+
905
+ async def details(self) -> str:
906
+ return ''
907
+
908
+ async def debug_error_string(self) -> Optional[str]:
909
+ return None
910
+
911
+ def __await__(self):
912
+ if False: # pylint: disable=using-constant-test
913
+ # This code path is never used, but a yield statement is needed
914
+ # for telling the interpreter that __await__ is a generator.
915
+ yield None
916
+ return self._response
917
+
918
+ async def wait_for_connection(self) -> None:
919
+ pass
920
+
921
+
922
+ class _StreamCallResponseIterator:
923
+
924
+ _call: Union[_base_call.UnaryStreamCall, _base_call.StreamStreamCall]
925
+ _response_iterator: AsyncIterable[ResponseType]
926
+
927
+ def __init__(self, call: Union[_base_call.UnaryStreamCall,
928
+ _base_call.StreamStreamCall],
929
+ response_iterator: AsyncIterable[ResponseType]) -> None:
930
+ self._response_iterator = response_iterator
931
+ self._call = call
932
+
933
+ def cancel(self) -> bool:
934
+ return self._call.cancel()
935
+
936
+ def cancelled(self) -> bool:
937
+ return self._call.cancelled()
938
+
939
+ def done(self) -> bool:
940
+ return self._call.done()
941
+
942
+ def add_done_callback(self, callback) -> None:
943
+ self._call.add_done_callback(callback)
944
+
945
+ def time_remaining(self) -> Optional[float]:
946
+ return self._call.time_remaining()
947
+
948
+ async def initial_metadata(self) -> Optional[Metadata]:
949
+ return await self._call.initial_metadata()
950
+
951
+ async def trailing_metadata(self) -> Optional[Metadata]:
952
+ return await self._call.trailing_metadata()
953
+
954
+ async def code(self) -> grpc.StatusCode:
955
+ return await self._call.code()
956
+
957
+ async def details(self) -> str:
958
+ return await self._call.details()
959
+
960
+ async def debug_error_string(self) -> Optional[str]:
961
+ return await self._call.debug_error_string()
962
+
963
+ def __aiter__(self):
964
+ return self._response_iterator.__aiter__()
965
+
966
+ async def wait_for_connection(self) -> None:
967
+ return await self._call.wait_for_connection()
968
+
969
+
970
+ class UnaryStreamCallResponseIterator(_StreamCallResponseIterator,
971
+ _base_call.UnaryStreamCall):
972
+ """UnaryStreamCall class wich uses an alternative response iterator."""
973
+
974
+ async def read(self) -> ResponseType:
975
+ # Behind the scenes everyting goes through the
976
+ # async iterator. So this path should not be reached.
977
+ raise NotImplementedError()
978
+
979
+
980
+ class StreamStreamCallResponseIterator(_StreamCallResponseIterator,
981
+ _base_call.StreamStreamCall):
982
+ """StreamStreamCall class wich uses an alternative response iterator."""
983
+
984
+ async def read(self) -> ResponseType:
985
+ # Behind the scenes everyting goes through the
986
+ # async iterator. So this path should not be reached.
987
+ raise NotImplementedError()
988
+
989
+ async def write(self, request: RequestType) -> None:
990
+ # Behind the scenes everyting goes through the
991
+ # async iterator provided by the InterceptedStreamStreamCall.
992
+ # So this path should not be reached.
993
+ raise NotImplementedError()
994
+
995
+ async def done_writing(self) -> None:
996
+ # Behind the scenes everyting goes through the
997
+ # async iterator provided by the InterceptedStreamStreamCall.
998
+ # So this path should not be reached.
999
+ raise NotImplementedError()
1000
+
1001
+ @property
1002
+ def _done_writing_flag(self) -> bool:
1003
+ return self._call._done_writing_flag