_interceptor.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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, List,
  21. Optional, Sequence, Union)
  22. import grpc
  23. from grpc._cython import cygrpc
  24. from . import _base_call
  25. from ._call import AioRpcError
  26. from ._call import StreamStreamCall
  27. from ._call import StreamUnaryCall
  28. from ._call import UnaryStreamCall
  29. from ._call import UnaryUnaryCall
  30. from ._call import _API_STYLE_ERROR
  31. from ._call import _RPC_ALREADY_FINISHED_DETAILS
  32. from ._call import _RPC_HALF_CLOSED_DETAILS
  33. from ._metadata import Metadata
  34. from ._typing import DeserializingFunction
  35. from ._typing import DoneCallbackType
  36. from ._typing import RequestIterableType
  37. from ._typing import RequestType
  38. from ._typing import ResponseIterableType
  39. from ._typing import ResponseType
  40. from ._typing import SerializingFunction
  41. from ._utils import _timeout_to_deadline
  42. _LOCAL_CANCELLATION_DETAILS = 'Locally cancelled by application!'
  43. class ServerInterceptor(metaclass=ABCMeta):
  44. """Affords intercepting incoming RPCs on the service-side.
  45. This is an EXPERIMENTAL API.
  46. """
  47. @abstractmethod
  48. async def intercept_service(
  49. self, continuation: Callable[[grpc.HandlerCallDetails],
  50. Awaitable[grpc.RpcMethodHandler]],
  51. handler_call_details: grpc.HandlerCallDetails
  52. ) -> grpc.RpcMethodHandler:
  53. """Intercepts incoming RPCs before handing them over to a handler.
  54. Args:
  55. continuation: A function that takes a HandlerCallDetails and
  56. proceeds to invoke the next interceptor in the chain, if any,
  57. or the RPC handler lookup logic, with the call details passed
  58. as an argument, and returns an RpcMethodHandler instance if
  59. the RPC is considered serviced, or None otherwise.
  60. handler_call_details: A HandlerCallDetails describing the RPC.
  61. Returns:
  62. An RpcMethodHandler with which the RPC may be serviced if the
  63. interceptor chooses to service this RPC, or None otherwise.
  64. """
  65. class ClientCallDetails(
  66. collections.namedtuple(
  67. 'ClientCallDetails',
  68. ('method', 'timeout', 'metadata', 'credentials', 'wait_for_ready')),
  69. grpc.ClientCallDetails):
  70. """Describes an RPC to be invoked.
  71. This is an EXPERIMENTAL API.
  72. Args:
  73. method: The method name of the RPC.
  74. timeout: An optional duration of time in seconds to allow for the RPC.
  75. metadata: Optional metadata to be transmitted to the service-side of
  76. the RPC.
  77. credentials: An optional CallCredentials for the RPC.
  78. wait_for_ready: An optional flag to enable :term:`wait_for_ready` mechanism.
  79. """
  80. method: str
  81. timeout: Optional[float]
  82. metadata: Optional[Metadata]
  83. credentials: Optional[grpc.CallCredentials]
  84. wait_for_ready: Optional[bool]
  85. class ClientInterceptor(metaclass=ABCMeta):
  86. """Base class used for all Aio Client Interceptor classes"""
  87. class UnaryUnaryClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  88. """Affords intercepting unary-unary invocations."""
  89. @abstractmethod
  90. async def intercept_unary_unary(
  91. self, continuation: Callable[[ClientCallDetails, RequestType],
  92. UnaryUnaryCall],
  93. client_call_details: ClientCallDetails,
  94. request: RequestType) -> Union[UnaryUnaryCall, ResponseType]:
  95. """Intercepts a unary-unary invocation asynchronously.
  96. Args:
  97. continuation: A coroutine that proceeds with the invocation by
  98. executing the next interceptor in the chain or invoking the
  99. actual RPC on the underlying Channel. It is the interceptor's
  100. responsibility to call it if it decides to move the RPC forward.
  101. The interceptor can use
  102. `call = await continuation(client_call_details, request)`
  103. to continue with the RPC. `continuation` returns the call to the
  104. RPC.
  105. client_call_details: A ClientCallDetails object describing the
  106. outgoing RPC.
  107. request: The request value for the RPC.
  108. Returns:
  109. An object with the RPC response.
  110. Raises:
  111. AioRpcError: Indicating that the RPC terminated with non-OK status.
  112. asyncio.CancelledError: Indicating that the RPC was canceled.
  113. """
  114. class UnaryStreamClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  115. """Affords intercepting unary-stream invocations."""
  116. @abstractmethod
  117. async def intercept_unary_stream(
  118. self, continuation: Callable[[ClientCallDetails, RequestType],
  119. UnaryStreamCall],
  120. client_call_details: ClientCallDetails, request: RequestType
  121. ) -> Union[ResponseIterableType, UnaryStreamCall]:
  122. """Intercepts a unary-stream invocation asynchronously.
  123. The function could return the call object or an asynchronous
  124. iterator, in case of being an asyncrhonous iterator this will
  125. become the source of the reads done by the caller.
  126. Args:
  127. continuation: A coroutine that proceeds with the invocation by
  128. executing the next interceptor in the chain or invoking the
  129. actual RPC on the underlying Channel. It is the interceptor's
  130. responsibility to call it if it decides to move the RPC forward.
  131. The interceptor can use
  132. `call = await continuation(client_call_details, request)`
  133. to continue with the RPC. `continuation` returns the call to the
  134. RPC.
  135. client_call_details: A ClientCallDetails object describing the
  136. outgoing RPC.
  137. request: The request value for the RPC.
  138. Returns:
  139. The RPC Call or an asynchronous iterator.
  140. Raises:
  141. AioRpcError: Indicating that the RPC terminated with non-OK status.
  142. asyncio.CancelledError: Indicating that the RPC was canceled.
  143. """
  144. class StreamUnaryClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  145. """Affords intercepting stream-unary invocations."""
  146. @abstractmethod
  147. async def intercept_stream_unary(
  148. self,
  149. continuation: Callable[[ClientCallDetails, RequestType],
  150. StreamUnaryCall],
  151. client_call_details: ClientCallDetails,
  152. request_iterator: RequestIterableType,
  153. ) -> StreamUnaryCall:
  154. """Intercepts a stream-unary invocation asynchronously.
  155. Within the interceptor the usage of the call methods like `write` or
  156. even awaiting the call should be done carefully, since the caller
  157. could be expecting an untouched call, for example for start writing
  158. messages to it.
  159. Args:
  160. continuation: A coroutine that proceeds with the invocation by
  161. executing the next interceptor in the chain or invoking the
  162. actual RPC on the underlying Channel. It is the interceptor's
  163. responsibility to call it if it decides to move the RPC forward.
  164. The interceptor can use
  165. `call = await continuation(client_call_details, request_iterator)`
  166. to continue with the RPC. `continuation` returns the call to the
  167. RPC.
  168. client_call_details: A ClientCallDetails object describing the
  169. outgoing RPC.
  170. request_iterator: The request iterator that will produce requests
  171. for the RPC.
  172. Returns:
  173. The RPC Call.
  174. Raises:
  175. AioRpcError: Indicating that the RPC terminated with non-OK status.
  176. asyncio.CancelledError: Indicating that the RPC was canceled.
  177. """
  178. class StreamStreamClientInterceptor(ClientInterceptor, metaclass=ABCMeta):
  179. """Affords intercepting stream-stream invocations."""
  180. @abstractmethod
  181. async def intercept_stream_stream(
  182. self,
  183. continuation: Callable[[ClientCallDetails, RequestType],
  184. StreamStreamCall],
  185. client_call_details: ClientCallDetails,
  186. request_iterator: RequestIterableType,
  187. ) -> Union[ResponseIterableType, StreamStreamCall]:
  188. """Intercepts a stream-stream invocation asynchronously.
  189. Within the interceptor the usage of the call methods like `write` or
  190. even awaiting the call should be done carefully, since the caller
  191. could be expecting an untouched call, for example for start writing
  192. messages to it.
  193. The function could return the call object or an asynchronous
  194. iterator, in case of being an asyncrhonous iterator this will
  195. become the source of the reads done by the caller.
  196. Args:
  197. continuation: A coroutine that proceeds with the invocation by
  198. executing the next interceptor in the chain or invoking the
  199. actual RPC on the underlying Channel. It is the interceptor's
  200. responsibility to call it if it decides to move the RPC forward.
  201. The interceptor can use
  202. `call = await continuation(client_call_details, request_iterator)`
  203. to continue with the RPC. `continuation` returns the call to the
  204. RPC.
  205. client_call_details: A ClientCallDetails object describing the
  206. outgoing RPC.
  207. request_iterator: The request iterator that will produce requests
  208. for the RPC.
  209. Returns:
  210. The RPC Call or an asynchronous iterator.
  211. Raises:
  212. AioRpcError: Indicating that the RPC terminated with non-OK status.
  213. asyncio.CancelledError: Indicating that the RPC was canceled.
  214. """
  215. class InterceptedCall:
  216. """Base implementation for all intercepted call arities.
  217. Interceptors might have some work to do before the RPC invocation with
  218. the capacity of changing the invocation parameters, and some work to do
  219. after the RPC invocation with the capacity for accessing to the wrapped
  220. `UnaryUnaryCall`.
  221. It handles also early and later cancellations, when the RPC has not even
  222. started and the execution is still held by the interceptors or when the
  223. RPC has finished but again the execution is still held by the interceptors.
  224. Once the RPC is finally executed, all methods are finally done against the
  225. intercepted call, being at the same time the same call returned to the
  226. interceptors.
  227. As a base class for all of the interceptors implements the logic around
  228. final status, metadata and cancellation.
  229. """
  230. _interceptors_task: asyncio.Task
  231. _pending_add_done_callbacks: Sequence[DoneCallbackType]
  232. def __init__(self, interceptors_task: asyncio.Task) -> None:
  233. self._interceptors_task = interceptors_task
  234. self._pending_add_done_callbacks = []
  235. self._interceptors_task.add_done_callback(
  236. self._fire_or_add_pending_done_callbacks)
  237. def __del__(self):
  238. self.cancel()
  239. def _fire_or_add_pending_done_callbacks(
  240. self, interceptors_task: asyncio.Task) -> None:
  241. if not self._pending_add_done_callbacks:
  242. return
  243. call_completed = False
  244. try:
  245. call = interceptors_task.result()
  246. if call.done():
  247. call_completed = True
  248. except (AioRpcError, asyncio.CancelledError):
  249. call_completed = True
  250. if call_completed:
  251. for callback in self._pending_add_done_callbacks:
  252. callback(self)
  253. else:
  254. for callback in self._pending_add_done_callbacks:
  255. callback = functools.partial(self._wrap_add_done_callback,
  256. callback)
  257. call.add_done_callback(callback)
  258. self._pending_add_done_callbacks = []
  259. def _wrap_add_done_callback(self, callback: DoneCallbackType,
  260. unused_call: _base_call.Call) -> None:
  261. callback(self)
  262. def cancel(self) -> bool:
  263. if not self._interceptors_task.done():
  264. # There is no yet the intercepted call available,
  265. # Trying to cancel it by using the generic Asyncio
  266. # cancellation method.
  267. return self._interceptors_task.cancel()
  268. try:
  269. call = self._interceptors_task.result()
  270. except AioRpcError:
  271. return False
  272. except asyncio.CancelledError:
  273. return False
  274. return call.cancel()
  275. def cancelled(self) -> bool:
  276. if not self._interceptors_task.done():
  277. return False
  278. try:
  279. call = self._interceptors_task.result()
  280. except AioRpcError as err:
  281. return err.code() == grpc.StatusCode.CANCELLED
  282. except asyncio.CancelledError:
  283. return True
  284. return call.cancelled()
  285. def done(self) -> bool:
  286. if not self._interceptors_task.done():
  287. return False
  288. try:
  289. call = self._interceptors_task.result()
  290. except (AioRpcError, asyncio.CancelledError):
  291. return True
  292. return call.done()
  293. def add_done_callback(self, callback: DoneCallbackType) -> None:
  294. if not self._interceptors_task.done():
  295. self._pending_add_done_callbacks.append(callback)
  296. return
  297. try:
  298. call = self._interceptors_task.result()
  299. except (AioRpcError, asyncio.CancelledError):
  300. callback(self)
  301. return
  302. if call.done():
  303. callback(self)
  304. else:
  305. callback = functools.partial(self._wrap_add_done_callback, callback)
  306. call.add_done_callback(callback)
  307. def time_remaining(self) -> Optional[float]:
  308. raise NotImplementedError()
  309. async def initial_metadata(self) -> Optional[Metadata]:
  310. try:
  311. call = await self._interceptors_task
  312. except AioRpcError as err:
  313. return err.initial_metadata()
  314. except asyncio.CancelledError:
  315. return None
  316. return await call.initial_metadata()
  317. async def trailing_metadata(self) -> Optional[Metadata]:
  318. try:
  319. call = await self._interceptors_task
  320. except AioRpcError as err:
  321. return err.trailing_metadata()
  322. except asyncio.CancelledError:
  323. return None
  324. return await call.trailing_metadata()
  325. async def code(self) -> grpc.StatusCode:
  326. try:
  327. call = await self._interceptors_task
  328. except AioRpcError as err:
  329. return err.code()
  330. except asyncio.CancelledError:
  331. return grpc.StatusCode.CANCELLED
  332. return await call.code()
  333. async def details(self) -> str:
  334. try:
  335. call = await self._interceptors_task
  336. except AioRpcError as err:
  337. return err.details()
  338. except asyncio.CancelledError:
  339. return _LOCAL_CANCELLATION_DETAILS
  340. return await call.details()
  341. async def debug_error_string(self) -> Optional[str]:
  342. try:
  343. call = await self._interceptors_task
  344. except AioRpcError as err:
  345. return err.debug_error_string()
  346. except asyncio.CancelledError:
  347. return ''
  348. return await call.debug_error_string()
  349. async def wait_for_connection(self) -> None:
  350. call = await self._interceptors_task
  351. return await call.wait_for_connection()
  352. class _InterceptedUnaryResponseMixin:
  353. def __await__(self):
  354. call = yield from self._interceptors_task.__await__()
  355. response = yield from call.__await__()
  356. return response
  357. class _InterceptedStreamResponseMixin:
  358. _response_aiter: Optional[AsyncIterable[ResponseType]]
  359. def _init_stream_response_mixin(self) -> None:
  360. # Is initalized later, otherwise if the iterator is not finnally
  361. # consumed a logging warning is emmited by Asyncio.
  362. self._response_aiter = None
  363. async def _wait_for_interceptor_task_response_iterator(
  364. self) -> ResponseType:
  365. call = await self._interceptors_task
  366. async for response in call:
  367. yield response
  368. def __aiter__(self) -> AsyncIterable[ResponseType]:
  369. if self._response_aiter is None:
  370. self._response_aiter = self._wait_for_interceptor_task_response_iterator(
  371. )
  372. return self._response_aiter
  373. async def read(self) -> ResponseType:
  374. if self._response_aiter is None:
  375. self._response_aiter = self._wait_for_interceptor_task_response_iterator(
  376. )
  377. return await self._response_aiter.asend(None)
  378. class _InterceptedStreamRequestMixin:
  379. _write_to_iterator_async_gen: Optional[AsyncIterable[RequestType]]
  380. _write_to_iterator_queue: Optional[asyncio.Queue]
  381. _status_code_task: Optional[asyncio.Task]
  382. _FINISH_ITERATOR_SENTINEL = object()
  383. def _init_stream_request_mixin(
  384. self, request_iterator: Optional[RequestIterableType]
  385. ) -> RequestIterableType:
  386. if request_iterator is None:
  387. # We provide our own request iterator which is a proxy
  388. # of the futures writes that will be done by the caller.
  389. self._write_to_iterator_queue = asyncio.Queue(maxsize=1)
  390. self._write_to_iterator_async_gen = self._proxy_writes_as_request_iterator(
  391. )
  392. self._status_code_task = None
  393. request_iterator = self._write_to_iterator_async_gen
  394. else:
  395. self._write_to_iterator_queue = None
  396. return request_iterator
  397. async def _proxy_writes_as_request_iterator(self):
  398. await self._interceptors_task
  399. while True:
  400. value = await self._write_to_iterator_queue.get()
  401. if value is _InterceptedStreamRequestMixin._FINISH_ITERATOR_SENTINEL:
  402. break
  403. yield value
  404. async def _write_to_iterator_queue_interruptible(self, request: RequestType,
  405. call: InterceptedCall):
  406. # Write the specified 'request' to the request iterator queue using the
  407. # specified 'call' to allow for interruption of the write in the case
  408. # of abrupt termination of the call.
  409. if self._status_code_task is None:
  410. self._status_code_task = self._loop.create_task(call.code())
  411. await asyncio.wait(
  412. (self._loop.create_task(self._write_to_iterator_queue.put(request)),
  413. self._status_code_task),
  414. return_when=asyncio.FIRST_COMPLETED)
  415. async def write(self, request: RequestType) -> None:
  416. # If no queue was created it means that requests
  417. # should be expected through an iterators provided
  418. # by the caller.
  419. if self._write_to_iterator_queue is None:
  420. raise cygrpc.UsageError(_API_STYLE_ERROR)
  421. try:
  422. call = await self._interceptors_task
  423. except (asyncio.CancelledError, AioRpcError):
  424. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  425. if call.done():
  426. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  427. elif call._done_writing_flag:
  428. raise asyncio.InvalidStateError(_RPC_HALF_CLOSED_DETAILS)
  429. await self._write_to_iterator_queue_interruptible(request, call)
  430. if call.done():
  431. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  432. async def done_writing(self) -> None:
  433. """Signal peer that client is done writing.
  434. This method is idempotent.
  435. """
  436. # If no queue was created it means that requests
  437. # should be expected through an iterators provided
  438. # by the caller.
  439. if self._write_to_iterator_queue is None:
  440. raise cygrpc.UsageError(_API_STYLE_ERROR)
  441. try:
  442. call = await self._interceptors_task
  443. except asyncio.CancelledError:
  444. raise asyncio.InvalidStateError(_RPC_ALREADY_FINISHED_DETAILS)
  445. await self._write_to_iterator_queue_interruptible(
  446. _InterceptedStreamRequestMixin._FINISH_ITERATOR_SENTINEL, call)
  447. class InterceptedUnaryUnaryCall(_InterceptedUnaryResponseMixin, InterceptedCall,
  448. _base_call.UnaryUnaryCall):
  449. """Used for running a `UnaryUnaryCall` wrapped by interceptors.
  450. For the `__await__` method is it is proxied to the intercepted call only when
  451. the interceptor task is finished.
  452. """
  453. _loop: asyncio.AbstractEventLoop
  454. _channel: cygrpc.AioChannel
  455. # pylint: disable=too-many-arguments
  456. def __init__(self, interceptors: Sequence[UnaryUnaryClientInterceptor],
  457. request: RequestType, timeout: Optional[float],
  458. metadata: Metadata,
  459. credentials: Optional[grpc.CallCredentials],
  460. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  461. method: bytes, request_serializer: SerializingFunction,
  462. response_deserializer: DeserializingFunction,
  463. loop: asyncio.AbstractEventLoop) -> None:
  464. self._loop = loop
  465. self._channel = channel
  466. interceptors_task = loop.create_task(
  467. self._invoke(interceptors, method, timeout, metadata, credentials,
  468. wait_for_ready, request, request_serializer,
  469. response_deserializer))
  470. super().__init__(interceptors_task)
  471. # pylint: disable=too-many-arguments
  472. async def _invoke(
  473. self, interceptors: Sequence[UnaryUnaryClientInterceptor],
  474. method: bytes, timeout: Optional[float],
  475. metadata: Optional[Metadata],
  476. credentials: Optional[grpc.CallCredentials],
  477. wait_for_ready: Optional[bool], request: RequestType,
  478. request_serializer: SerializingFunction,
  479. response_deserializer: DeserializingFunction) -> UnaryUnaryCall:
  480. """Run the RPC call wrapped in interceptors"""
  481. async def _run_interceptor(
  482. interceptors: List[UnaryUnaryClientInterceptor],
  483. client_call_details: ClientCallDetails,
  484. request: RequestType) -> _base_call.UnaryUnaryCall:
  485. if interceptors:
  486. continuation = functools.partial(_run_interceptor,
  487. interceptors[1:])
  488. call_or_response = await interceptors[0].intercept_unary_unary(
  489. continuation, client_call_details, request)
  490. if isinstance(call_or_response, _base_call.UnaryUnaryCall):
  491. return call_or_response
  492. else:
  493. return UnaryUnaryCallResponse(call_or_response)
  494. else:
  495. return UnaryUnaryCall(
  496. request, _timeout_to_deadline(client_call_details.timeout),
  497. client_call_details.metadata,
  498. client_call_details.credentials,
  499. client_call_details.wait_for_ready, self._channel,
  500. client_call_details.method, request_serializer,
  501. response_deserializer, self._loop)
  502. client_call_details = ClientCallDetails(method, timeout, metadata,
  503. credentials, wait_for_ready)
  504. return await _run_interceptor(list(interceptors), client_call_details,
  505. request)
  506. def time_remaining(self) -> Optional[float]:
  507. raise NotImplementedError()
  508. class InterceptedUnaryStreamCall(_InterceptedStreamResponseMixin,
  509. InterceptedCall, _base_call.UnaryStreamCall):
  510. """Used for running a `UnaryStreamCall` wrapped by interceptors."""
  511. _loop: asyncio.AbstractEventLoop
  512. _channel: cygrpc.AioChannel
  513. _last_returned_call_from_interceptors = Optional[_base_call.UnaryStreamCall]
  514. # pylint: disable=too-many-arguments
  515. def __init__(self, interceptors: Sequence[UnaryStreamClientInterceptor],
  516. request: RequestType, timeout: Optional[float],
  517. metadata: Metadata,
  518. credentials: Optional[grpc.CallCredentials],
  519. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  520. method: bytes, request_serializer: SerializingFunction,
  521. response_deserializer: DeserializingFunction,
  522. loop: asyncio.AbstractEventLoop) -> None:
  523. self._loop = loop
  524. self._channel = channel
  525. self._init_stream_response_mixin()
  526. self._last_returned_call_from_interceptors = None
  527. interceptors_task = loop.create_task(
  528. self._invoke(interceptors, method, timeout, metadata, credentials,
  529. wait_for_ready, request, request_serializer,
  530. response_deserializer))
  531. super().__init__(interceptors_task)
  532. # pylint: disable=too-many-arguments
  533. async def _invoke(
  534. self, interceptors: Sequence[UnaryUnaryClientInterceptor],
  535. method: bytes, timeout: Optional[float],
  536. metadata: Optional[Metadata],
  537. credentials: Optional[grpc.CallCredentials],
  538. wait_for_ready: Optional[bool], request: RequestType,
  539. request_serializer: SerializingFunction,
  540. response_deserializer: DeserializingFunction) -> UnaryStreamCall:
  541. """Run the RPC call wrapped in interceptors"""
  542. async def _run_interceptor(
  543. interceptors: List[UnaryStreamClientInterceptor],
  544. client_call_details: ClientCallDetails,
  545. request: RequestType,
  546. ) -> _base_call.UnaryUnaryCall:
  547. if interceptors:
  548. continuation = functools.partial(_run_interceptor,
  549. interceptors[1:])
  550. call_or_response_iterator = await interceptors[
  551. 0].intercept_unary_stream(continuation, client_call_details,
  552. request)
  553. if isinstance(call_or_response_iterator,
  554. _base_call.UnaryStreamCall):
  555. self._last_returned_call_from_interceptors = call_or_response_iterator
  556. else:
  557. self._last_returned_call_from_interceptors = UnaryStreamCallResponseIterator(
  558. self._last_returned_call_from_interceptors,
  559. call_or_response_iterator)
  560. return self._last_returned_call_from_interceptors
  561. else:
  562. self._last_returned_call_from_interceptors = UnaryStreamCall(
  563. request, _timeout_to_deadline(client_call_details.timeout),
  564. client_call_details.metadata,
  565. client_call_details.credentials,
  566. client_call_details.wait_for_ready, self._channel,
  567. client_call_details.method, request_serializer,
  568. response_deserializer, self._loop)
  569. return self._last_returned_call_from_interceptors
  570. client_call_details = ClientCallDetails(method, timeout, metadata,
  571. credentials, wait_for_ready)
  572. return await _run_interceptor(list(interceptors), client_call_details,
  573. request)
  574. def time_remaining(self) -> Optional[float]:
  575. raise NotImplementedError()
  576. class InterceptedStreamUnaryCall(_InterceptedUnaryResponseMixin,
  577. _InterceptedStreamRequestMixin,
  578. InterceptedCall, _base_call.StreamUnaryCall):
  579. """Used for running a `StreamUnaryCall` wrapped by interceptors.
  580. For the `__await__` method is it is proxied to the intercepted call only when
  581. the interceptor task is finished.
  582. """
  583. _loop: asyncio.AbstractEventLoop
  584. _channel: cygrpc.AioChannel
  585. # pylint: disable=too-many-arguments
  586. def __init__(self, interceptors: Sequence[StreamUnaryClientInterceptor],
  587. request_iterator: Optional[RequestIterableType],
  588. timeout: Optional[float], metadata: Metadata,
  589. credentials: Optional[grpc.CallCredentials],
  590. wait_for_ready: Optional[bool], channel: cygrpc.AioChannel,
  591. method: bytes, request_serializer: SerializingFunction,
  592. response_deserializer: DeserializingFunction,
  593. loop: asyncio.AbstractEventLoop) -> None:
  594. self._loop = loop
  595. self._channel = channel
  596. request_iterator = self._init_stream_request_mixin(request_iterator)
  597. interceptors_task = loop.create_task(
  598. self._invoke(interceptors, method, timeout, metadata, credentials,
  599. wait_for_ready, request_iterator, request_serializer,
  600. response_deserializer))
  601. super().__init__(interceptors_task)
  602. # pylint: disable=too-many-arguments
  603. async def _invoke(
  604. self, interceptors: Sequence[StreamUnaryClientInterceptor],
  605. method: bytes, timeout: Optional[float],
  606. metadata: Optional[Metadata],
  607. credentials: Optional[grpc.CallCredentials],
  608. wait_for_ready: Optional[bool],
  609. request_iterator: RequestIterableType,
  610. request_serializer: SerializingFunction,
  611. response_deserializer: DeserializingFunction) -> StreamUnaryCall:
  612. """Run the RPC call wrapped in interceptors"""
  613. async def _run_interceptor(
  614. interceptors: Iterator[UnaryUnaryClientInterceptor],
  615. client_call_details: ClientCallDetails,
  616. request_iterator: RequestIterableType
  617. ) -> _base_call.StreamUnaryCall:
  618. if interceptors:
  619. continuation = functools.partial(_run_interceptor,
  620. interceptors[1:])
  621. return await interceptors[0].intercept_stream_unary(
  622. continuation, client_call_details, request_iterator)
  623. else:
  624. return StreamUnaryCall(
  625. request_iterator,
  626. _timeout_to_deadline(client_call_details.timeout),
  627. client_call_details.metadata,
  628. client_call_details.credentials,
  629. client_call_details.wait_for_ready, self._channel,
  630. client_call_details.method, request_serializer,
  631. response_deserializer, self._loop)
  632. client_call_details = ClientCallDetails(method, timeout, metadata,
  633. credentials, wait_for_ready)
  634. return await _run_interceptor(list(interceptors), client_call_details,
  635. request_iterator)
  636. def time_remaining(self) -> Optional[float]:
  637. raise NotImplementedError()
  638. class InterceptedStreamStreamCall(_InterceptedStreamResponseMixin,
  639. _InterceptedStreamRequestMixin,
  640. InterceptedCall, _base_call.StreamStreamCall):
  641. """Used for running a `StreamStreamCall` wrapped by interceptors."""
  642. _loop: asyncio.AbstractEventLoop
  643. _channel: cygrpc.AioChannel
  644. _last_returned_call_from_interceptors = Optional[_base_call.UnaryStreamCall]
  645. # pylint: disable=too-many-arguments
  646. def __init__(self, interceptors: Sequence[StreamStreamClientInterceptor],
  647. request_iterator: Optional[RequestIterableType],
  648. timeout: Optional[float], 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. request_iterator = self._init_stream_request_mixin(request_iterator)
  658. self._last_returned_call_from_interceptors = None
  659. interceptors_task = loop.create_task(
  660. self._invoke(interceptors, method, timeout, metadata, credentials,
  661. wait_for_ready, request_iterator, request_serializer,
  662. response_deserializer))
  663. super().__init__(interceptors_task)
  664. # pylint: disable=too-many-arguments
  665. async def _invoke(
  666. self, interceptors: Sequence[StreamStreamClientInterceptor],
  667. method: bytes, timeout: Optional[float],
  668. metadata: Optional[Metadata],
  669. credentials: Optional[grpc.CallCredentials],
  670. wait_for_ready: Optional[bool],
  671. request_iterator: RequestIterableType,
  672. request_serializer: SerializingFunction,
  673. response_deserializer: DeserializingFunction) -> StreamStreamCall:
  674. """Run the RPC call wrapped in interceptors"""
  675. async def _run_interceptor(
  676. interceptors: List[StreamStreamClientInterceptor],
  677. client_call_details: ClientCallDetails,
  678. request_iterator: RequestIterableType
  679. ) -> _base_call.StreamStreamCall:
  680. if interceptors:
  681. continuation = functools.partial(_run_interceptor,
  682. interceptors[1:])
  683. call_or_response_iterator = await interceptors[
  684. 0].intercept_stream_stream(continuation,
  685. client_call_details,
  686. request_iterator)
  687. if isinstance(call_or_response_iterator,
  688. _base_call.StreamStreamCall):
  689. self._last_returned_call_from_interceptors = call_or_response_iterator
  690. else:
  691. self._last_returned_call_from_interceptors = StreamStreamCallResponseIterator(
  692. self._last_returned_call_from_interceptors,
  693. call_or_response_iterator)
  694. return self._last_returned_call_from_interceptors
  695. else:
  696. self._last_returned_call_from_interceptors = StreamStreamCall(
  697. request_iterator,
  698. _timeout_to_deadline(client_call_details.timeout),
  699. client_call_details.metadata,
  700. client_call_details.credentials,
  701. client_call_details.wait_for_ready, self._channel,
  702. client_call_details.method, request_serializer,
  703. response_deserializer, self._loop)
  704. return self._last_returned_call_from_interceptors
  705. client_call_details = ClientCallDetails(method, timeout, metadata,
  706. credentials, wait_for_ready)
  707. return await _run_interceptor(list(interceptors), client_call_details,
  708. request_iterator)
  709. def time_remaining(self) -> Optional[float]:
  710. raise NotImplementedError()
  711. class UnaryUnaryCallResponse(_base_call.UnaryUnaryCall):
  712. """Final UnaryUnaryCall class finished with a response."""
  713. _response: ResponseType
  714. def __init__(self, response: ResponseType) -> None:
  715. self._response = response
  716. def cancel(self) -> bool:
  717. return False
  718. def cancelled(self) -> bool:
  719. return False
  720. def done(self) -> bool:
  721. return True
  722. def add_done_callback(self, unused_callback) -> None:
  723. raise NotImplementedError()
  724. def time_remaining(self) -> Optional[float]:
  725. raise NotImplementedError()
  726. async def initial_metadata(self) -> Optional[Metadata]:
  727. return None
  728. async def trailing_metadata(self) -> Optional[Metadata]:
  729. return None
  730. async def code(self) -> grpc.StatusCode:
  731. return grpc.StatusCode.OK
  732. async def details(self) -> str:
  733. return ''
  734. async def debug_error_string(self) -> Optional[str]:
  735. return None
  736. def __await__(self):
  737. if False: # pylint: disable=using-constant-test
  738. # This code path is never used, but a yield statement is needed
  739. # for telling the interpreter that __await__ is a generator.
  740. yield None
  741. return self._response
  742. async def wait_for_connection(self) -> None:
  743. pass
  744. class _StreamCallResponseIterator:
  745. _call: Union[_base_call.UnaryStreamCall, _base_call.StreamStreamCall]
  746. _response_iterator: AsyncIterable[ResponseType]
  747. def __init__(self, call: Union[_base_call.UnaryStreamCall,
  748. _base_call.StreamStreamCall],
  749. response_iterator: AsyncIterable[ResponseType]) -> None:
  750. self._response_iterator = response_iterator
  751. self._call = call
  752. def cancel(self) -> bool:
  753. return self._call.cancel()
  754. def cancelled(self) -> bool:
  755. return self._call.cancelled()
  756. def done(self) -> bool:
  757. return self._call.done()
  758. def add_done_callback(self, callback) -> None:
  759. self._call.add_done_callback(callback)
  760. def time_remaining(self) -> Optional[float]:
  761. return self._call.time_remaining()
  762. async def initial_metadata(self) -> Optional[Metadata]:
  763. return await self._call.initial_metadata()
  764. async def trailing_metadata(self) -> Optional[Metadata]:
  765. return await self._call.trailing_metadata()
  766. async def code(self) -> grpc.StatusCode:
  767. return await self._call.code()
  768. async def details(self) -> str:
  769. return await self._call.details()
  770. async def debug_error_string(self) -> Optional[str]:
  771. return await self._call.debug_error_string()
  772. def __aiter__(self):
  773. return self._response_iterator.__aiter__()
  774. async def wait_for_connection(self) -> None:
  775. return await self._call.wait_for_connection()
  776. class UnaryStreamCallResponseIterator(_StreamCallResponseIterator,
  777. _base_call.UnaryStreamCall):
  778. """UnaryStreamCall class wich uses an alternative response iterator."""
  779. async def read(self) -> ResponseType:
  780. # Behind the scenes everyting goes through the
  781. # async iterator. So this path should not be reached.
  782. raise NotImplementedError()
  783. class StreamStreamCallResponseIterator(_StreamCallResponseIterator,
  784. _base_call.StreamStreamCall):
  785. """StreamStreamCall class wich uses an alternative response iterator."""
  786. async def read(self) -> ResponseType:
  787. # Behind the scenes everyting goes through the
  788. # async iterator. So this path should not be reached.
  789. raise NotImplementedError()
  790. async def write(self, request: RequestType) -> None:
  791. # Behind the scenes everyting goes through the
  792. # async iterator provided by the InterceptedStreamStreamCall.
  793. # So this path should not be reached.
  794. raise NotImplementedError()
  795. async def done_writing(self) -> None:
  796. # Behind the scenes everyting goes through the
  797. # async iterator provided by the InterceptedStreamStreamCall.
  798. # So this path should not be reached.
  799. raise NotImplementedError()
  800. @property
  801. def _done_writing_flag(self) -> bool:
  802. return self._call._done_writing_flag