OLD | NEW |
(Empty) | |
| 1 # Copyright 2015, Google Inc. |
| 2 # All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 """Utilities for RPC framework's face layer.""" |
| 31 |
| 32 import collections |
| 33 |
| 34 from grpc.framework.common import cardinality |
| 35 from grpc.framework.common import style |
| 36 from grpc.framework.face import interfaces |
| 37 from grpc.framework.foundation import stream |
| 38 |
| 39 |
| 40 class _MethodImplementation( |
| 41 interfaces.MethodImplementation, |
| 42 collections.namedtuple( |
| 43 '_MethodImplementation', |
| 44 ['cardinality', 'style', 'unary_unary_inline', 'unary_stream_inline', |
| 45 'stream_unary_inline', 'stream_stream_inline', 'unary_unary_event', |
| 46 'unary_stream_event', 'stream_unary_event', 'stream_stream_event',])): |
| 47 pass |
| 48 |
| 49 |
| 50 def unary_unary_inline(behavior): |
| 51 """Creates an interfaces.MethodImplementation for the given behavior. |
| 52 |
| 53 Args: |
| 54 behavior: The implementation of a unary-unary RPC method as a callable value |
| 55 that takes a request value and an interfaces.RpcContext object and |
| 56 returns a response value. |
| 57 |
| 58 Returns: |
| 59 An interfaces.MethodImplementation derived from the given behavior. |
| 60 """ |
| 61 return _MethodImplementation( |
| 62 cardinality.Cardinality.UNARY_UNARY, style.Service.INLINE, behavior, |
| 63 None, None, None, None, None, None, None) |
| 64 |
| 65 |
| 66 def unary_stream_inline(behavior): |
| 67 """Creates an interfaces.MethodImplementation for the given behavior. |
| 68 |
| 69 Args: |
| 70 behavior: The implementation of a unary-stream RPC method as a callable |
| 71 value that takes a request value and an interfaces.RpcContext object and |
| 72 returns an iterator of response values. |
| 73 |
| 74 Returns: |
| 75 An interfaces.MethodImplementation derived from the given behavior. |
| 76 """ |
| 77 return _MethodImplementation( |
| 78 cardinality.Cardinality.UNARY_STREAM, style.Service.INLINE, None, |
| 79 behavior, None, None, None, None, None, None) |
| 80 |
| 81 |
| 82 def stream_unary_inline(behavior): |
| 83 """Creates an interfaces.MethodImplementation for the given behavior. |
| 84 |
| 85 Args: |
| 86 behavior: The implementation of a stream-unary RPC method as a callable |
| 87 value that takes an iterator of request values and an |
| 88 interfaces.RpcContext object and returns a response value. |
| 89 |
| 90 Returns: |
| 91 An interfaces.MethodImplementation derived from the given behavior. |
| 92 """ |
| 93 return _MethodImplementation( |
| 94 cardinality.Cardinality.STREAM_UNARY, style.Service.INLINE, None, None, |
| 95 behavior, None, None, None, None, None) |
| 96 |
| 97 |
| 98 def stream_stream_inline(behavior): |
| 99 """Creates an interfaces.MethodImplementation for the given behavior. |
| 100 |
| 101 Args: |
| 102 behavior: The implementation of a stream-stream RPC method as a callable |
| 103 value that takes an iterator of request values and an |
| 104 interfaces.RpcContext object and returns an iterator of response values. |
| 105 |
| 106 Returns: |
| 107 An interfaces.MethodImplementation derived from the given behavior. |
| 108 """ |
| 109 return _MethodImplementation( |
| 110 cardinality.Cardinality.STREAM_STREAM, style.Service.INLINE, None, None, |
| 111 None, behavior, None, None, None, None) |
| 112 |
| 113 |
| 114 def unary_unary_event(behavior): |
| 115 """Creates an interfaces.MethodImplementation for the given behavior. |
| 116 |
| 117 Args: |
| 118 behavior: The implementation of a unary-unary RPC method as a callable |
| 119 value that takes a request value, a response callback to which to pass |
| 120 the response value of the RPC, and an interfaces.RpcContext. |
| 121 |
| 122 Returns: |
| 123 An interfaces.MethodImplementation derived from the given behavior. |
| 124 """ |
| 125 return _MethodImplementation( |
| 126 cardinality.Cardinality.UNARY_UNARY, style.Service.EVENT, None, None, |
| 127 None, None, behavior, None, None, None) |
| 128 |
| 129 |
| 130 def unary_stream_event(behavior): |
| 131 """Creates an interfaces.MethodImplementation for the given behavior. |
| 132 |
| 133 Args: |
| 134 behavior: The implementation of a unary-stream RPC method as a callable |
| 135 value that takes a request value, a stream.Consumer to which to pass the |
| 136 the response values of the RPC, and an interfaces.RpcContext. |
| 137 |
| 138 Returns: |
| 139 An interfaces.MethodImplementation derived from the given behavior. |
| 140 """ |
| 141 return _MethodImplementation( |
| 142 cardinality.Cardinality.UNARY_STREAM, style.Service.EVENT, None, None, |
| 143 None, None, None, behavior, None, None) |
| 144 |
| 145 |
| 146 def stream_unary_event(behavior): |
| 147 """Creates an interfaces.MethodImplementation for the given behavior. |
| 148 |
| 149 Args: |
| 150 behavior: The implementation of a stream-unary RPC method as a callable |
| 151 value that takes a response callback to which to pass the response value |
| 152 of the RPC and an interfaces.RpcContext and returns a stream.Consumer to |
| 153 which the request values of the RPC should be passed. |
| 154 |
| 155 Returns: |
| 156 An interfaces.MethodImplementation derived from the given behavior. |
| 157 """ |
| 158 return _MethodImplementation( |
| 159 cardinality.Cardinality.STREAM_UNARY, style.Service.EVENT, None, None, |
| 160 None, None, None, None, behavior, None) |
| 161 |
| 162 |
| 163 def stream_stream_event(behavior): |
| 164 """Creates an interfaces.MethodImplementation for the given behavior. |
| 165 |
| 166 Args: |
| 167 behavior: The implementation of a stream-stream RPC method as a callable |
| 168 value that takes a stream.Consumer to which to pass the response values |
| 169 of the RPC and an interfaces.RpcContext and returns a stream.Consumer to |
| 170 which the request values of the RPC should be passed. |
| 171 |
| 172 Returns: |
| 173 An interfaces.MethodImplementation derived from the given behavior. |
| 174 """ |
| 175 return _MethodImplementation( |
| 176 cardinality.Cardinality.STREAM_STREAM, style.Service.EVENT, None, None, |
| 177 None, None, None, None, None, behavior) |
OLD | NEW |