1 # Copyright 2018 gRPC authors.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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 """gRPC's experimental APIs.
16 These APIs are subject to be removed during any minor version release.
25 from grpc._cython import cygrpc as _cygrpc
27 _EXPERIMENTAL_APIS_USED = set()
30 class ChannelOptions(object):
31 """Indicates a channel option unique to gRPC Python.
33 This enumeration is part of an EXPERIMENTAL API.
36 SingleThreadedUnaryStream: Perform unary-stream RPCs on a single thread.
38 SingleThreadedUnaryStream = "SingleThreadedUnaryStream"
41 class UsageError(Exception):
42 """Raised by the gRPC library to indicate usage not allowed by the API."""
45 # It's important that there be a single insecure credentials object so that its
46 # hash is deterministic and can be used for indexing in the simple stubs cache.
47 _insecure_channel_credentials = grpc.ChannelCredentials(
48 _cygrpc.channel_credentials_insecure())
51 def insecure_channel_credentials():
52 """Creates a ChannelCredentials for use with an insecure channel.
54 THIS IS AN EXPERIMENTAL API.
56 return _insecure_channel_credentials
59 class ExperimentalApiWarning(Warning):
60 """A warning that an API is experimental."""
63 def _warn_experimental(api_name, stack_offset):
64 if api_name not in _EXPERIMENTAL_APIS_USED:
65 _EXPERIMENTAL_APIS_USED.add(api_name)
66 msg = ("'{}' is an experimental API. It is subject to change or ".
68 "removal between minor releases. Proceed with caution.")
69 warnings.warn(msg, ExperimentalApiWarning, stacklevel=2 + stack_offset)
72 def experimental_api(f):
75 def _wrapper(*args, **kwargs):
76 _warn_experimental(f.__name__, 1)
77 return f(*args, **kwargs)
82 def wrap_server_method_handler(wrapper, handler):
83 """Wraps the server method handler function.
85 The server implementation requires all server handlers being wrapped as
86 RpcMethodHandler objects. This helper function ease the pain of writing
87 server handler wrappers.
90 wrapper: A wrapper function that takes in a method handler behavior
91 (the actual function) and returns a wrapped function.
92 handler: A RpcMethodHandler object to be wrapped.
95 A newly created RpcMethodHandler.
100 if not handler.request_streaming:
101 if not handler.response_streaming:
102 # NOTE(lidiz) _replace is a public API:
103 # https://docs.python.org/dev/library/collections.html
104 return handler._replace(unary_unary=wrapper(handler.unary_unary))
106 return handler._replace(unary_stream=wrapper(handler.unary_stream))
108 if not handler.response_streaming:
109 return handler._replace(stream_unary=wrapper(handler.stream_unary))
111 return handler._replace(
112 stream_stream=wrapper(handler.stream_stream))
117 'ExperimentalApiWarning',
119 'insecure_channel_credentials',
120 'wrap_server_method_handler',
123 if sys.version_info > (3, 6):
124 from grpc._simple_stubs import unary_unary, unary_stream, stream_unary, stream_stream
125 __all__ = __all__ + (unary_unary, unary_stream, stream_unary, stream_stream)