utilities.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. import collections
  16. # stream is referenced from specification in this module.
  17. from grpc.framework.common import cardinality
  18. from grpc.framework.common import style
  19. from grpc.framework.foundation import stream # pylint: disable=unused-import
  20. from grpc.framework.interfaces.face import face
  21. class _MethodImplementation(face.MethodImplementation,
  22. collections.namedtuple('_MethodImplementation', [
  23. 'cardinality',
  24. 'style',
  25. 'unary_unary_inline',
  26. 'unary_stream_inline',
  27. 'stream_unary_inline',
  28. 'stream_stream_inline',
  29. 'unary_unary_event',
  30. 'unary_stream_event',
  31. 'stream_unary_event',
  32. 'stream_stream_event',
  33. ])):
  34. pass
  35. def unary_unary_inline(behavior):
  36. """Creates an face.MethodImplementation for the given behavior.
  37. Args:
  38. behavior: The implementation of a unary-unary RPC method as a callable value
  39. that takes a request value and an face.ServicerContext object and
  40. returns a response value.
  41. Returns:
  42. An face.MethodImplementation derived from the given behavior.
  43. """
  44. return _MethodImplementation(cardinality.Cardinality.UNARY_UNARY,
  45. style.Service.INLINE, behavior, None, None,
  46. None, None, None, None, None)
  47. def unary_stream_inline(behavior):
  48. """Creates an face.MethodImplementation for the given behavior.
  49. Args:
  50. behavior: The implementation of a unary-stream RPC method as a callable
  51. value that takes a request value and an face.ServicerContext object and
  52. returns an iterator of response values.
  53. Returns:
  54. An face.MethodImplementation derived from the given behavior.
  55. """
  56. return _MethodImplementation(cardinality.Cardinality.UNARY_STREAM,
  57. style.Service.INLINE, None, behavior, None,
  58. None, None, None, None, None)
  59. def stream_unary_inline(behavior):
  60. """Creates an face.MethodImplementation for the given behavior.
  61. Args:
  62. behavior: The implementation of a stream-unary RPC method as a callable
  63. value that takes an iterator of request values and an
  64. face.ServicerContext object and returns a response value.
  65. Returns:
  66. An face.MethodImplementation derived from the given behavior.
  67. """
  68. return _MethodImplementation(cardinality.Cardinality.STREAM_UNARY,
  69. style.Service.INLINE, None, None, behavior,
  70. None, None, None, None, None)
  71. def stream_stream_inline(behavior):
  72. """Creates an face.MethodImplementation for the given behavior.
  73. Args:
  74. behavior: The implementation of a stream-stream RPC method as a callable
  75. value that takes an iterator of request values and an
  76. face.ServicerContext object and returns an iterator of response values.
  77. Returns:
  78. An face.MethodImplementation derived from the given behavior.
  79. """
  80. return _MethodImplementation(cardinality.Cardinality.STREAM_STREAM,
  81. style.Service.INLINE, None, None, None,
  82. behavior, None, None, None, None)
  83. def unary_unary_event(behavior):
  84. """Creates an face.MethodImplementation for the given behavior.
  85. Args:
  86. behavior: The implementation of a unary-unary RPC method as a callable
  87. value that takes a request value, a response callback to which to pass
  88. the response value of the RPC, and an face.ServicerContext.
  89. Returns:
  90. An face.MethodImplementation derived from the given behavior.
  91. """
  92. return _MethodImplementation(cardinality.Cardinality.UNARY_UNARY,
  93. style.Service.EVENT, None, None, None, None,
  94. behavior, None, None, None)
  95. def unary_stream_event(behavior):
  96. """Creates an face.MethodImplementation for the given behavior.
  97. Args:
  98. behavior: The implementation of a unary-stream RPC method as a callable
  99. value that takes a request value, a stream.Consumer to which to pass the
  100. the response values of the RPC, and an face.ServicerContext.
  101. Returns:
  102. An face.MethodImplementation derived from the given behavior.
  103. """
  104. return _MethodImplementation(cardinality.Cardinality.UNARY_STREAM,
  105. style.Service.EVENT, None, None, None, None,
  106. None, behavior, None, None)
  107. def stream_unary_event(behavior):
  108. """Creates an face.MethodImplementation for the given behavior.
  109. Args:
  110. behavior: The implementation of a stream-unary RPC method as a callable
  111. value that takes a response callback to which to pass the response value
  112. of the RPC and an face.ServicerContext and returns a stream.Consumer to
  113. which the request values of the RPC should be passed.
  114. Returns:
  115. An face.MethodImplementation derived from the given behavior.
  116. """
  117. return _MethodImplementation(cardinality.Cardinality.STREAM_UNARY,
  118. style.Service.EVENT, None, None, None, None,
  119. None, None, behavior, None)
  120. def stream_stream_event(behavior):
  121. """Creates an face.MethodImplementation for the given behavior.
  122. Args:
  123. behavior: The implementation of a stream-stream RPC method as a callable
  124. value that takes a stream.Consumer to which to pass the response values
  125. of the RPC and an face.ServicerContext and returns a stream.Consumer to
  126. which the request values of the RPC should be passed.
  127. Returns:
  128. An face.MethodImplementation derived from the given behavior.
  129. """
  130. return _MethodImplementation(cardinality.Cardinality.STREAM_STREAM,
  131. style.Service.EVENT, None, None, None, None,
  132. None, None, None, behavior)