File size: 7,818 Bytes
375a1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright 2019 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Abstract base classes for client-side Call objects.



Call objects represents the RPC itself, and offer methods to access / modify

its information. They also offer methods to manipulate the life-cycle of the

RPC, e.g. cancellation.

"""

from abc import ABCMeta
from abc import abstractmethod
from typing import Any, AsyncIterator, Generator, Generic, Optional, Union

import grpc

from ._metadata import Metadata
from ._typing import DoneCallbackType
from ._typing import EOFType
from ._typing import RequestType
from ._typing import ResponseType

__all__ = "RpcContext", "Call", "UnaryUnaryCall", "UnaryStreamCall"


class RpcContext(metaclass=ABCMeta):
    """Provides RPC-related information and action."""

    @abstractmethod
    def cancelled(self) -> bool:
        """Return True if the RPC is cancelled.



        The RPC is cancelled when the cancellation was requested with cancel().



        Returns:

          A bool indicates whether the RPC is cancelled or not.

        """

    @abstractmethod
    def done(self) -> bool:
        """Return True if the RPC is done.



        An RPC is done if the RPC is completed, cancelled or aborted.



        Returns:

          A bool indicates if the RPC is done.

        """

    @abstractmethod
    def time_remaining(self) -> Optional[float]:
        """Describes the length of allowed time remaining for the RPC.



        Returns:

          A nonnegative float indicating the length of allowed time in seconds

          remaining for the RPC to complete before it is considered to have

          timed out, or None if no deadline was specified for the RPC.

        """

    @abstractmethod
    def cancel(self) -> bool:
        """Cancels the RPC.



        Idempotent and has no effect if the RPC has already terminated.



        Returns:

          A bool indicates if the cancellation is performed or not.

        """

    @abstractmethod
    def add_done_callback(self, callback: DoneCallbackType) -> None:
        """Registers a callback to be called on RPC termination.



        Args:

          callback: A callable object will be called with the call object as

          its only argument.

        """


class Call(RpcContext, metaclass=ABCMeta):
    """The abstract base class of an RPC on the client-side."""

    @abstractmethod
    async def initial_metadata(self) -> Metadata:
        """Accesses the initial metadata sent by the server.



        Returns:

          The initial :term:`metadata`.

        """

    @abstractmethod
    async def trailing_metadata(self) -> Metadata:
        """Accesses the trailing metadata sent by the server.



        Returns:

          The trailing :term:`metadata`.

        """

    @abstractmethod
    async def code(self) -> grpc.StatusCode:
        """Accesses the status code sent by the server.



        Returns:

          The StatusCode value for the RPC.

        """

    @abstractmethod
    async def details(self) -> str:
        """Accesses the details sent by the server.



        Returns:

          The details string of the RPC.

        """

    @abstractmethod
    async def wait_for_connection(self) -> None:
        """Waits until connected to peer and raises aio.AioRpcError if failed.



        This is an EXPERIMENTAL method.



        This method ensures the RPC has been successfully connected. Otherwise,

        an AioRpcError will be raised to explain the reason of the connection

        failure.



        This method is recommended for building retry mechanisms.

        """


class UnaryUnaryCall(
    Generic[RequestType, ResponseType], Call, metaclass=ABCMeta
):
    """The abstract base class of an unary-unary RPC on the client-side."""

    @abstractmethod
    def __await__(self) -> Generator[Any, None, ResponseType]:
        """Await the response message to be ready.



        Returns:

          The response message of the RPC.

        """


class UnaryStreamCall(
    Generic[RequestType, ResponseType], Call, metaclass=ABCMeta
):
    @abstractmethod
    def __aiter__(self) -> AsyncIterator[ResponseType]:
        """Returns the async iterator representation that yields messages.



        Under the hood, it is calling the "read" method.



        Returns:

          An async iterator object that yields messages.

        """

    @abstractmethod
    async def read(self) -> Union[EOFType, ResponseType]:
        """Reads one message from the stream.



        Read operations must be serialized when called from multiple

        coroutines.



        Note that the iterator and read/write APIs may not be mixed on

        a single RPC.



        Returns:

          A response message, or an `grpc.aio.EOF` to indicate the end of the

          stream.

        """


class StreamUnaryCall(
    Generic[RequestType, ResponseType], Call, metaclass=ABCMeta
):
    @abstractmethod
    async def write(self, request: RequestType) -> None:
        """Writes one message to the stream.



        Note that the iterator and read/write APIs may not be mixed on

        a single RPC.



        Raises:

          An RpcError exception if the write failed.

        """

    @abstractmethod
    async def done_writing(self) -> None:
        """Notifies server that the client is done sending messages.



        After done_writing is called, any additional invocation to the write

        function will fail. This function is idempotent.

        """

    @abstractmethod
    def __await__(self) -> Generator[Any, None, ResponseType]:
        """Await the response message to be ready.



        Returns:

          The response message of the stream.

        """


class StreamStreamCall(
    Generic[RequestType, ResponseType], Call, metaclass=ABCMeta
):
    @abstractmethod
    def __aiter__(self) -> AsyncIterator[ResponseType]:
        """Returns the async iterator representation that yields messages.



        Under the hood, it is calling the "read" method.



        Returns:

          An async iterator object that yields messages.

        """

    @abstractmethod
    async def read(self) -> Union[EOFType, ResponseType]:
        """Reads one message from the stream.



        Read operations must be serialized when called from multiple

        coroutines.



        Note that the iterator and read/write APIs may not be mixed on

        a single RPC.



        Returns:

          A response message, or an `grpc.aio.EOF` to indicate the end of the

          stream.

        """

    @abstractmethod
    async def write(self, request: RequestType) -> None:
        """Writes one message to the stream.



        Note that the iterator and read/write APIs may not be mixed on

        a single RPC.



        Raises:

          An RpcError exception if the write failed.

        """

    @abstractmethod
    async def done_writing(self) -> None:
        """Notifies server that the client is done sending messages.



        After done_writing is called, any additional invocation to the write

        function will fail. This function is idempotent.

        """