Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / src / python / grpcio / grpc / experimental / __init__.py
1 # Copyright 2018 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 """gRPC's experimental APIs.
15
16 These APIs are subject to be removed during any minor version release.
17 """
18
19 import copy
20 import functools
21 import sys
22 import warnings
23
24 import grpc
25 from grpc._cython import cygrpc as _cygrpc
26
27 _EXPERIMENTAL_APIS_USED = set()
28
29
30 class ChannelOptions(object):
31     """Indicates a channel option unique to gRPC Python.
32
33      This enumeration is part of an EXPERIMENTAL API.
34
35      Attributes:
36        SingleThreadedUnaryStream: Perform unary-stream RPCs on a single thread.
37     """
38     SingleThreadedUnaryStream = "SingleThreadedUnaryStream"
39
40
41 class UsageError(Exception):
42     """Raised by the gRPC library to indicate usage not allowed by the API."""
43
44
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())
49
50
51 def insecure_channel_credentials():
52     """Creates a ChannelCredentials for use with an insecure channel.
53
54     THIS IS AN EXPERIMENTAL API.
55     """
56     return _insecure_channel_credentials
57
58
59 class ExperimentalApiWarning(Warning):
60     """A warning that an API is experimental."""
61
62
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 ".
67                format(api_name) +
68                "removal between minor releases. Proceed with caution.")
69         warnings.warn(msg, ExperimentalApiWarning, stacklevel=2 + stack_offset)
70
71
72 def experimental_api(f):
73
74     @functools.wraps(f)
75     def _wrapper(*args, **kwargs):
76         _warn_experimental(f.__name__, 1)
77         return f(*args, **kwargs)
78
79     return _wrapper
80
81
82 def wrap_server_method_handler(wrapper, handler):
83     """Wraps the server method handler function.
84
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.
88
89     Args:
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.
93
94     Returns:
95         A newly created RpcMethodHandler.
96     """
97     if not handler:
98         return None
99
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))
105         else:
106             return handler._replace(unary_stream=wrapper(handler.unary_stream))
107     else:
108         if not handler.response_streaming:
109             return handler._replace(stream_unary=wrapper(handler.stream_unary))
110         else:
111             return handler._replace(
112                 stream_stream=wrapper(handler.stream_stream))
113
114
115 __all__ = (
116     'ChannelOptions',
117     'ExperimentalApiWarning',
118     'UsageError',
119     'insecure_channel_credentials',
120     'wrap_server_method_handler',
121 )
122
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)