implementations.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # Copyright 2015-2016 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. """Entry points into the Beta API of gRPC Python."""
  15. # threading is referenced from specification in this module.
  16. import threading # pylint: disable=unused-import
  17. # interfaces, cardinality, and face are referenced from specification in this
  18. # module.
  19. import grpc
  20. from grpc import _auth
  21. from grpc.beta import _client_adaptations
  22. from grpc.beta import _metadata
  23. from grpc.beta import _server_adaptations
  24. from grpc.beta import interfaces # pylint: disable=unused-import
  25. from grpc.framework.common import cardinality # pylint: disable=unused-import
  26. from grpc.framework.interfaces.face import \
  27. face # pylint: disable=unused-import
  28. # pylint: disable=too-many-arguments
  29. ChannelCredentials = grpc.ChannelCredentials
  30. ssl_channel_credentials = grpc.ssl_channel_credentials
  31. CallCredentials = grpc.CallCredentials
  32. def metadata_call_credentials(metadata_plugin, name=None):
  33. def plugin(context, callback):
  34. def wrapped_callback(beta_metadata, error):
  35. callback(_metadata.unbeta(beta_metadata), error)
  36. metadata_plugin(context, wrapped_callback)
  37. return grpc.metadata_call_credentials(plugin, name=name)
  38. def google_call_credentials(credentials):
  39. """Construct CallCredentials from GoogleCredentials.
  40. Args:
  41. credentials: A GoogleCredentials object from the oauth2client library.
  42. Returns:
  43. A CallCredentials object for use in a GRPCCallOptions object.
  44. """
  45. return metadata_call_credentials(_auth.GoogleCallCredentials(credentials))
  46. access_token_call_credentials = grpc.access_token_call_credentials
  47. composite_call_credentials = grpc.composite_call_credentials
  48. composite_channel_credentials = grpc.composite_channel_credentials
  49. class Channel(object):
  50. """A channel to a remote host through which RPCs may be conducted.
  51. Only the "subscribe" and "unsubscribe" methods are supported for application
  52. use. This class' instance constructor and all other attributes are
  53. unsupported.
  54. """
  55. def __init__(self, channel):
  56. self._channel = channel
  57. def subscribe(self, callback, try_to_connect=None):
  58. """Subscribes to this Channel's connectivity.
  59. Args:
  60. callback: A callable to be invoked and passed an
  61. interfaces.ChannelConnectivity identifying this Channel's connectivity.
  62. The callable will be invoked immediately upon subscription and again for
  63. every change to this Channel's connectivity thereafter until it is
  64. unsubscribed.
  65. try_to_connect: A boolean indicating whether or not this Channel should
  66. attempt to connect if it is not already connected and ready to conduct
  67. RPCs.
  68. """
  69. self._channel.subscribe(callback, try_to_connect=try_to_connect)
  70. def unsubscribe(self, callback):
  71. """Unsubscribes a callback from this Channel's connectivity.
  72. Args:
  73. callback: A callable previously registered with this Channel from having
  74. been passed to its "subscribe" method.
  75. """
  76. self._channel.unsubscribe(callback)
  77. def insecure_channel(host, port):
  78. """Creates an insecure Channel to a remote host.
  79. Args:
  80. host: The name of the remote host to which to connect.
  81. port: The port of the remote host to which to connect.
  82. If None only the 'host' part will be used.
  83. Returns:
  84. A Channel to the remote host through which RPCs may be conducted.
  85. """
  86. channel = grpc.insecure_channel(host if port is None else '%s:%d' %
  87. (host, port))
  88. return Channel(channel)
  89. def secure_channel(host, port, channel_credentials):
  90. """Creates a secure Channel to a remote host.
  91. Args:
  92. host: The name of the remote host to which to connect.
  93. port: The port of the remote host to which to connect.
  94. If None only the 'host' part will be used.
  95. channel_credentials: A ChannelCredentials.
  96. Returns:
  97. A secure Channel to the remote host through which RPCs may be conducted.
  98. """
  99. channel = grpc.secure_channel(
  100. host if port is None else '%s:%d' % (host, port), channel_credentials)
  101. return Channel(channel)
  102. class StubOptions(object):
  103. """A value encapsulating the various options for creation of a Stub.
  104. This class and its instances have no supported interface - it exists to define
  105. the type of its instances and its instances exist to be passed to other
  106. functions.
  107. """
  108. def __init__(self, host, request_serializers, response_deserializers,
  109. metadata_transformer, thread_pool, thread_pool_size):
  110. self.host = host
  111. self.request_serializers = request_serializers
  112. self.response_deserializers = response_deserializers
  113. self.metadata_transformer = metadata_transformer
  114. self.thread_pool = thread_pool
  115. self.thread_pool_size = thread_pool_size
  116. _EMPTY_STUB_OPTIONS = StubOptions(None, None, None, None, None, None)
  117. def stub_options(host=None,
  118. request_serializers=None,
  119. response_deserializers=None,
  120. metadata_transformer=None,
  121. thread_pool=None,
  122. thread_pool_size=None):
  123. """Creates a StubOptions value to be passed at stub creation.
  124. All parameters are optional and should always be passed by keyword.
  125. Args:
  126. host: A host string to set on RPC calls.
  127. request_serializers: A dictionary from service name-method name pair to
  128. request serialization behavior.
  129. response_deserializers: A dictionary from service name-method name pair to
  130. response deserialization behavior.
  131. metadata_transformer: A callable that given a metadata object produces
  132. another metadata object to be used in the underlying communication on the
  133. wire.
  134. thread_pool: A thread pool to use in stubs.
  135. thread_pool_size: The size of thread pool to create for use in stubs;
  136. ignored if thread_pool has been passed.
  137. Returns:
  138. A StubOptions value created from the passed parameters.
  139. """
  140. return StubOptions(host, request_serializers, response_deserializers,
  141. metadata_transformer, thread_pool, thread_pool_size)
  142. def generic_stub(channel, options=None):
  143. """Creates a face.GenericStub on which RPCs can be made.
  144. Args:
  145. channel: A Channel for use by the created stub.
  146. options: A StubOptions customizing the created stub.
  147. Returns:
  148. A face.GenericStub on which RPCs can be made.
  149. """
  150. effective_options = _EMPTY_STUB_OPTIONS if options is None else options
  151. return _client_adaptations.generic_stub(
  152. channel._channel, # pylint: disable=protected-access
  153. effective_options.host,
  154. effective_options.metadata_transformer,
  155. effective_options.request_serializers,
  156. effective_options.response_deserializers)
  157. def dynamic_stub(channel, service, cardinalities, options=None):
  158. """Creates a face.DynamicStub with which RPCs can be invoked.
  159. Args:
  160. channel: A Channel for the returned face.DynamicStub to use.
  161. service: The package-qualified full name of the service.
  162. cardinalities: A dictionary from RPC method name to cardinality.Cardinality
  163. value identifying the cardinality of the RPC method.
  164. options: An optional StubOptions value further customizing the functionality
  165. of the returned face.DynamicStub.
  166. Returns:
  167. A face.DynamicStub with which RPCs can be invoked.
  168. """
  169. effective_options = _EMPTY_STUB_OPTIONS if options is None else options
  170. return _client_adaptations.dynamic_stub(
  171. channel._channel, # pylint: disable=protected-access
  172. service,
  173. cardinalities,
  174. effective_options.host,
  175. effective_options.metadata_transformer,
  176. effective_options.request_serializers,
  177. effective_options.response_deserializers)
  178. ServerCredentials = grpc.ServerCredentials
  179. ssl_server_credentials = grpc.ssl_server_credentials
  180. class ServerOptions(object):
  181. """A value encapsulating the various options for creation of a Server.
  182. This class and its instances have no supported interface - it exists to define
  183. the type of its instances and its instances exist to be passed to other
  184. functions.
  185. """
  186. def __init__(self, multi_method_implementation, request_deserializers,
  187. response_serializers, thread_pool, thread_pool_size,
  188. default_timeout, maximum_timeout):
  189. self.multi_method_implementation = multi_method_implementation
  190. self.request_deserializers = request_deserializers
  191. self.response_serializers = response_serializers
  192. self.thread_pool = thread_pool
  193. self.thread_pool_size = thread_pool_size
  194. self.default_timeout = default_timeout
  195. self.maximum_timeout = maximum_timeout
  196. _EMPTY_SERVER_OPTIONS = ServerOptions(None, None, None, None, None, None, None)
  197. def server_options(multi_method_implementation=None,
  198. request_deserializers=None,
  199. response_serializers=None,
  200. thread_pool=None,
  201. thread_pool_size=None,
  202. default_timeout=None,
  203. maximum_timeout=None):
  204. """Creates a ServerOptions value to be passed at server creation.
  205. All parameters are optional and should always be passed by keyword.
  206. Args:
  207. multi_method_implementation: A face.MultiMethodImplementation to be called
  208. to service an RPC if the server has no specific method implementation for
  209. the name of the RPC for which service was requested.
  210. request_deserializers: A dictionary from service name-method name pair to
  211. request deserialization behavior.
  212. response_serializers: A dictionary from service name-method name pair to
  213. response serialization behavior.
  214. thread_pool: A thread pool to use in stubs.
  215. thread_pool_size: The size of thread pool to create for use in stubs;
  216. ignored if thread_pool has been passed.
  217. default_timeout: A duration in seconds to allow for RPC service when
  218. servicing RPCs that did not include a timeout value when invoked.
  219. maximum_timeout: A duration in seconds to allow for RPC service when
  220. servicing RPCs no matter what timeout value was passed when the RPC was
  221. invoked.
  222. Returns:
  223. A StubOptions value created from the passed parameters.
  224. """
  225. return ServerOptions(multi_method_implementation, request_deserializers,
  226. response_serializers, thread_pool, thread_pool_size,
  227. default_timeout, maximum_timeout)
  228. def server(service_implementations, options=None):
  229. """Creates an interfaces.Server with which RPCs can be serviced.
  230. Args:
  231. service_implementations: A dictionary from service name-method name pair to
  232. face.MethodImplementation.
  233. options: An optional ServerOptions value further customizing the
  234. functionality of the returned Server.
  235. Returns:
  236. An interfaces.Server with which RPCs can be serviced.
  237. """
  238. effective_options = _EMPTY_SERVER_OPTIONS if options is None else options
  239. return _server_adaptations.server(
  240. service_implementations, effective_options.multi_method_implementation,
  241. effective_options.request_deserializers,
  242. effective_options.response_serializers, effective_options.thread_pool,
  243. effective_options.thread_pool_size)