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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:26