_base_call.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright 2019 The 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. """Abstract base classes for client-side Call objects.
  15. Call objects represents the RPC itself, and offer methods to access / modify
  16. its information. They also offer methods to manipulate the life-cycle of the
  17. RPC, e.g. cancellation.
  18. """
  19. from abc import ABCMeta
  20. from abc import abstractmethod
  21. from typing import AsyncIterable, Awaitable, Generic, Optional, Union
  22. import grpc
  23. from ._metadata import Metadata
  24. from ._typing import DoneCallbackType
  25. from ._typing import EOFType
  26. from ._typing import RequestType
  27. from ._typing import ResponseType
  28. __all__ = 'RpcContext', 'Call', 'UnaryUnaryCall', 'UnaryStreamCall'
  29. class RpcContext(metaclass=ABCMeta):
  30. """Provides RPC-related information and action."""
  31. @abstractmethod
  32. def cancelled(self) -> bool:
  33. """Return True if the RPC is cancelled.
  34. The RPC is cancelled when the cancellation was requested with cancel().
  35. Returns:
  36. A bool indicates whether the RPC is cancelled or not.
  37. """
  38. @abstractmethod
  39. def done(self) -> bool:
  40. """Return True if the RPC is done.
  41. An RPC is done if the RPC is completed, cancelled or aborted.
  42. Returns:
  43. A bool indicates if the RPC is done.
  44. """
  45. @abstractmethod
  46. def time_remaining(self) -> Optional[float]:
  47. """Describes the length of allowed time remaining for the RPC.
  48. Returns:
  49. A nonnegative float indicating the length of allowed time in seconds
  50. remaining for the RPC to complete before it is considered to have
  51. timed out, or None if no deadline was specified for the RPC.
  52. """
  53. @abstractmethod
  54. def cancel(self) -> bool:
  55. """Cancels the RPC.
  56. Idempotent and has no effect if the RPC has already terminated.
  57. Returns:
  58. A bool indicates if the cancellation is performed or not.
  59. """
  60. @abstractmethod
  61. def add_done_callback(self, callback: DoneCallbackType) -> None:
  62. """Registers a callback to be called on RPC termination.
  63. Args:
  64. callback: A callable object will be called with the call object as
  65. its only argument.
  66. """
  67. class Call(RpcContext, metaclass=ABCMeta):
  68. """The abstract base class of an RPC on the client-side."""
  69. @abstractmethod
  70. async def initial_metadata(self) -> Metadata:
  71. """Accesses the initial metadata sent by the server.
  72. Returns:
  73. The initial :term:`metadata`.
  74. """
  75. @abstractmethod
  76. async def trailing_metadata(self) -> Metadata:
  77. """Accesses the trailing metadata sent by the server.
  78. Returns:
  79. The trailing :term:`metadata`.
  80. """
  81. @abstractmethod
  82. async def code(self) -> grpc.StatusCode:
  83. """Accesses the status code sent by the server.
  84. Returns:
  85. The StatusCode value for the RPC.
  86. """
  87. @abstractmethod
  88. async def details(self) -> str:
  89. """Accesses the details sent by the server.
  90. Returns:
  91. The details string of the RPC.
  92. """
  93. @abstractmethod
  94. async def wait_for_connection(self) -> None:
  95. """Waits until connected to peer and raises aio.AioRpcError if failed.
  96. This is an EXPERIMENTAL method.
  97. This method ensures the RPC has been successfully connected. Otherwise,
  98. an AioRpcError will be raised to explain the reason of the connection
  99. failure.
  100. This method is recommended for building retry mechanisms.
  101. """
  102. class UnaryUnaryCall(Generic[RequestType, ResponseType],
  103. Call,
  104. metaclass=ABCMeta):
  105. """The abstract base class of an unary-unary RPC on the client-side."""
  106. @abstractmethod
  107. def __await__(self) -> Awaitable[ResponseType]:
  108. """Await the response message to be ready.
  109. Returns:
  110. The response message of the RPC.
  111. """
  112. class UnaryStreamCall(Generic[RequestType, ResponseType],
  113. Call,
  114. metaclass=ABCMeta):
  115. @abstractmethod
  116. def __aiter__(self) -> AsyncIterable[ResponseType]:
  117. """Returns the async iterable representation that yields messages.
  118. Under the hood, it is calling the "read" method.
  119. Returns:
  120. An async iterable object that yields messages.
  121. """
  122. @abstractmethod
  123. async def read(self) -> Union[EOFType, ResponseType]:
  124. """Reads one message from the stream.
  125. Read operations must be serialized when called from multiple
  126. coroutines.
  127. Returns:
  128. A response message, or an `grpc.aio.EOF` to indicate the end of the
  129. stream.
  130. """
  131. class StreamUnaryCall(Generic[RequestType, ResponseType],
  132. Call,
  133. metaclass=ABCMeta):
  134. @abstractmethod
  135. async def write(self, request: RequestType) -> None:
  136. """Writes one message to the stream.
  137. Raises:
  138. An RpcError exception if the write failed.
  139. """
  140. @abstractmethod
  141. async def done_writing(self) -> None:
  142. """Notifies server that the client is done sending messages.
  143. After done_writing is called, any additional invocation to the write
  144. function will fail. This function is idempotent.
  145. """
  146. @abstractmethod
  147. def __await__(self) -> Awaitable[ResponseType]:
  148. """Await the response message to be ready.
  149. Returns:
  150. The response message of the stream.
  151. """
  152. class StreamStreamCall(Generic[RequestType, ResponseType],
  153. Call,
  154. metaclass=ABCMeta):
  155. @abstractmethod
  156. def __aiter__(self) -> AsyncIterable[ResponseType]:
  157. """Returns the async iterable representation that yields messages.
  158. Under the hood, it is calling the "read" method.
  159. Returns:
  160. An async iterable object that yields messages.
  161. """
  162. @abstractmethod
  163. async def read(self) -> Union[EOFType, ResponseType]:
  164. """Reads one message from the stream.
  165. Read operations must be serialized when called from multiple
  166. coroutines.
  167. Returns:
  168. A response message, or an `grpc.aio.EOF` to indicate the end of the
  169. stream.
  170. """
  171. @abstractmethod
  172. async def write(self, request: RequestType) -> None:
  173. """Writes one message to the stream.
  174. Raises:
  175. An RpcError exception if the write failed.
  176. """
  177. @abstractmethod
  178. async def done_writing(self) -> None:
  179. """Notifies server that the client is done sending messages.
  180. After done_writing is called, any additional invocation to the write
  181. function will fail. This function is idempotent.
  182. """