Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/grpc/aio/_base_call.py: 100%
57 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
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.
16Call objects represents the RPC itself, and offer methods to access / modify
17its information. They also offer methods to manipulate the life-cycle of the
18RPC, e.g. cancellation.
19"""
21from abc import ABCMeta
22from abc import abstractmethod
23from typing import AsyncIterable, Awaitable, Generic, Optional, Union
25import grpc
27from ._metadata import Metadata
28from ._typing import DoneCallbackType
29from ._typing import EOFType
30from ._typing import RequestType
31from ._typing import ResponseType
33__all__ = 'RpcContext', 'Call', 'UnaryUnaryCall', 'UnaryStreamCall'
36class RpcContext(metaclass=ABCMeta):
37 """Provides RPC-related information and action."""
39 @abstractmethod
40 def cancelled(self) -> bool:
41 """Return True if the RPC is cancelled.
43 The RPC is cancelled when the cancellation was requested with cancel().
45 Returns:
46 A bool indicates whether the RPC is cancelled or not.
47 """
49 @abstractmethod
50 def done(self) -> bool:
51 """Return True if the RPC is done.
53 An RPC is done if the RPC is completed, cancelled or aborted.
55 Returns:
56 A bool indicates if the RPC is done.
57 """
59 @abstractmethod
60 def time_remaining(self) -> Optional[float]:
61 """Describes the length of allowed time remaining for the RPC.
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 """
69 @abstractmethod
70 def cancel(self) -> bool:
71 """Cancels the RPC.
73 Idempotent and has no effect if the RPC has already terminated.
75 Returns:
76 A bool indicates if the cancellation is performed or not.
77 """
79 @abstractmethod
80 def add_done_callback(self, callback: DoneCallbackType) -> None:
81 """Registers a callback to be called on RPC termination.
83 Args:
84 callback: A callable object will be called with the call object as
85 its only argument.
86 """
89class Call(RpcContext, metaclass=ABCMeta):
90 """The abstract base class of an RPC on the client-side."""
92 @abstractmethod
93 async def initial_metadata(self) -> Metadata:
94 """Accesses the initial metadata sent by the server.
96 Returns:
97 The initial :term:`metadata`.
98 """
100 @abstractmethod
101 async def trailing_metadata(self) -> Metadata:
102 """Accesses the trailing metadata sent by the server.
104 Returns:
105 The trailing :term:`metadata`.
106 """
108 @abstractmethod
109 async def code(self) -> grpc.StatusCode:
110 """Accesses the status code sent by the server.
112 Returns:
113 The StatusCode value for the RPC.
114 """
116 @abstractmethod
117 async def details(self) -> str:
118 """Accesses the details sent by the server.
120 Returns:
121 The details string of the RPC.
122 """
124 @abstractmethod
125 async def wait_for_connection(self) -> None:
126 """Waits until connected to peer and raises aio.AioRpcError if failed.
128 This is an EXPERIMENTAL method.
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.
134 This method is recommended for building retry mechanisms.
135 """
138class UnaryUnaryCall(Generic[RequestType, ResponseType],
139 Call,
140 metaclass=ABCMeta):
141 """The abstract base class of an unary-unary RPC on the client-side."""
143 @abstractmethod
144 def __await__(self) -> Awaitable[ResponseType]:
145 """Await the response message to be ready.
147 Returns:
148 The response message of the RPC.
149 """
152class UnaryStreamCall(Generic[RequestType, ResponseType],
153 Call,
154 metaclass=ABCMeta):
156 @abstractmethod
157 def __aiter__(self) -> AsyncIterable[ResponseType]:
158 """Returns the async iterable representation that yields messages.
160 Under the hood, it is calling the "read" method.
162 Returns:
163 An async iterable object that yields messages.
164 """
166 @abstractmethod
167 async def read(self) -> Union[EOFType, ResponseType]:
168 """Reads one message from the stream.
170 Read operations must be serialized when called from multiple
171 coroutines.
173 Returns:
174 A response message, or an `grpc.aio.EOF` to indicate the end of the
175 stream.
176 """
179class StreamUnaryCall(Generic[RequestType, ResponseType],
180 Call,
181 metaclass=ABCMeta):
183 @abstractmethod
184 async def write(self, request: RequestType) -> None:
185 """Writes one message to the stream.
187 Raises:
188 An RpcError exception if the write failed.
189 """
191 @abstractmethod
192 async def done_writing(self) -> None:
193 """Notifies server that the client is done sending messages.
195 After done_writing is called, any additional invocation to the write
196 function will fail. This function is idempotent.
197 """
199 @abstractmethod
200 def __await__(self) -> Awaitable[ResponseType]:
201 """Await the response message to be ready.
203 Returns:
204 The response message of the stream.
205 """
208class StreamStreamCall(Generic[RequestType, ResponseType],
209 Call,
210 metaclass=ABCMeta):
212 @abstractmethod
213 def __aiter__(self) -> AsyncIterable[ResponseType]:
214 """Returns the async iterable representation that yields messages.
216 Under the hood, it is calling the "read" method.
218 Returns:
219 An async iterable object that yields messages.
220 """
222 @abstractmethod
223 async def read(self) -> Union[EOFType, ResponseType]:
224 """Reads one message from the stream.
226 Read operations must be serialized when called from multiple
227 coroutines.
229 Returns:
230 A response message, or an `grpc.aio.EOF` to indicate the end of the
231 stream.
232 """
234 @abstractmethod
235 async def write(self, request: RequestType) -> None:
236 """Writes one message to the stream.
238 Raises:
239 An RpcError exception if the write failed.
240 """
242 @abstractmethod
243 async def done_writing(self) -> None:
244 """Notifies server that the client is done sending messages.
246 After done_writing is called, any additional invocation to the write
247 function will fail. This function is idempotent.
248 """