xref: /aosp_15_r20/external/grpc-grpc/src/python/grpcio/grpc/framework/interfaces/face/utilities.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2015 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"""Utilities for RPC Framework's Face interface."""
15
16import collections
17
18# stream is referenced from specification in this module.
19from grpc.framework.common import cardinality
20from grpc.framework.common import style
21from grpc.framework.foundation import stream  # pylint: disable=unused-import
22from grpc.framework.interfaces.face import face
23
24
25class _MethodImplementation(
26    face.MethodImplementation,
27    collections.namedtuple(
28        "_MethodImplementation",
29        [
30            "cardinality",
31            "style",
32            "unary_unary_inline",
33            "unary_stream_inline",
34            "stream_unary_inline",
35            "stream_stream_inline",
36            "unary_unary_event",
37            "unary_stream_event",
38            "stream_unary_event",
39            "stream_stream_event",
40        ],
41    ),
42):
43    pass
44
45
46def unary_unary_inline(behavior):
47    """Creates an face.MethodImplementation for the given behavior.
48
49    Args:
50      behavior: The implementation of a unary-unary RPC method as a callable value
51        that takes a request value and an face.ServicerContext object and
52        returns a response value.
53
54    Returns:
55      An face.MethodImplementation derived from the given behavior.
56    """
57    return _MethodImplementation(
58        cardinality.Cardinality.UNARY_UNARY,
59        style.Service.INLINE,
60        behavior,
61        None,
62        None,
63        None,
64        None,
65        None,
66        None,
67        None,
68    )
69
70
71def unary_stream_inline(behavior):
72    """Creates an face.MethodImplementation for the given behavior.
73
74    Args:
75      behavior: The implementation of a unary-stream RPC method as a callable
76        value that takes a request value and an face.ServicerContext object and
77        returns an iterator of response values.
78
79    Returns:
80      An face.MethodImplementation derived from the given behavior.
81    """
82    return _MethodImplementation(
83        cardinality.Cardinality.UNARY_STREAM,
84        style.Service.INLINE,
85        None,
86        behavior,
87        None,
88        None,
89        None,
90        None,
91        None,
92        None,
93    )
94
95
96def stream_unary_inline(behavior):
97    """Creates an face.MethodImplementation for the given behavior.
98
99    Args:
100      behavior: The implementation of a stream-unary RPC method as a callable
101        value that takes an iterator of request values and an
102        face.ServicerContext object and returns a response value.
103
104    Returns:
105      An face.MethodImplementation derived from the given behavior.
106    """
107    return _MethodImplementation(
108        cardinality.Cardinality.STREAM_UNARY,
109        style.Service.INLINE,
110        None,
111        None,
112        behavior,
113        None,
114        None,
115        None,
116        None,
117        None,
118    )
119
120
121def stream_stream_inline(behavior):
122    """Creates an face.MethodImplementation for the given behavior.
123
124    Args:
125      behavior: The implementation of a stream-stream RPC method as a callable
126        value that takes an iterator of request values and an
127        face.ServicerContext object and returns an iterator of response values.
128
129    Returns:
130      An face.MethodImplementation derived from the given behavior.
131    """
132    return _MethodImplementation(
133        cardinality.Cardinality.STREAM_STREAM,
134        style.Service.INLINE,
135        None,
136        None,
137        None,
138        behavior,
139        None,
140        None,
141        None,
142        None,
143    )
144
145
146def unary_unary_event(behavior):
147    """Creates an face.MethodImplementation for the given behavior.
148
149    Args:
150      behavior: The implementation of a unary-unary RPC method as a callable
151        value that takes a request value, a response callback to which to pass
152        the response value of the RPC, and an face.ServicerContext.
153
154    Returns:
155      An face.MethodImplementation derived from the given behavior.
156    """
157    return _MethodImplementation(
158        cardinality.Cardinality.UNARY_UNARY,
159        style.Service.EVENT,
160        None,
161        None,
162        None,
163        None,
164        behavior,
165        None,
166        None,
167        None,
168    )
169
170
171def unary_stream_event(behavior):
172    """Creates an face.MethodImplementation for the given behavior.
173
174    Args:
175      behavior: The implementation of a unary-stream RPC method as a callable
176        value that takes a request value, a stream.Consumer to which to pass the
177        the response values of the RPC, and an face.ServicerContext.
178
179    Returns:
180      An face.MethodImplementation derived from the given behavior.
181    """
182    return _MethodImplementation(
183        cardinality.Cardinality.UNARY_STREAM,
184        style.Service.EVENT,
185        None,
186        None,
187        None,
188        None,
189        None,
190        behavior,
191        None,
192        None,
193    )
194
195
196def stream_unary_event(behavior):
197    """Creates an face.MethodImplementation for the given behavior.
198
199    Args:
200      behavior: The implementation of a stream-unary RPC method as a callable
201        value that takes a response callback to which to pass the response value
202        of the RPC and an face.ServicerContext and returns a stream.Consumer to
203        which the request values of the RPC should be passed.
204
205    Returns:
206      An face.MethodImplementation derived from the given behavior.
207    """
208    return _MethodImplementation(
209        cardinality.Cardinality.STREAM_UNARY,
210        style.Service.EVENT,
211        None,
212        None,
213        None,
214        None,
215        None,
216        None,
217        behavior,
218        None,
219    )
220
221
222def stream_stream_event(behavior):
223    """Creates an face.MethodImplementation for the given behavior.
224
225    Args:
226      behavior: The implementation of a stream-stream RPC method as a callable
227        value that takes a stream.Consumer to which to pass the response values
228        of the RPC and an face.ServicerContext and returns a stream.Consumer to
229        which the request values of the RPC should be passed.
230
231    Returns:
232      An face.MethodImplementation derived from the given behavior.
233    """
234    return _MethodImplementation(
235        cardinality.Cardinality.STREAM_STREAM,
236        style.Service.EVENT,
237        None,
238        None,
239        None,
240        None,
241        None,
242        None,
243        None,
244        behavior,
245    )
246