• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 use with the base interface of RPC Framework."""
15
16import collections
17
18from grpc.framework.interfaces.base import base
19
20
21class _Completion(
22    base.Completion,
23    collections.namedtuple(
24        "_Completion",
25        (
26            "terminal_metadata",
27            "code",
28            "message",
29        ),
30    ),
31):
32    """A trivial implementation of base.Completion."""
33
34
35class _Subscription(
36    base.Subscription,
37    collections.namedtuple(
38        "_Subscription",
39        (
40            "kind",
41            "termination_callback",
42            "allowance",
43            "operator",
44            "protocol_receiver",
45        ),
46    ),
47):
48    """A trivial implementation of base.Subscription."""
49
50
51_NONE_SUBSCRIPTION = _Subscription(
52    base.Subscription.Kind.NONE, None, None, None, None
53)
54
55
56def completion(terminal_metadata, code, message):
57    """Creates a base.Completion aggregating the given operation values.
58
59    Args:
60      terminal_metadata: A terminal metadata value for an operaton.
61      code: A code value for an operation.
62      message: A message value for an operation.
63
64    Returns:
65      A base.Completion aggregating the given operation values.
66    """
67    return _Completion(terminal_metadata, code, message)
68
69
70def full_subscription(operator, protocol_receiver):
71    """Creates a "full" base.Subscription for the given base.Operator.
72
73    Args:
74      operator: A base.Operator to be used in an operation.
75      protocol_receiver: A base.ProtocolReceiver to be used in an operation.
76
77    Returns:
78      A base.Subscription of kind base.Subscription.Kind.FULL wrapping the given
79        base.Operator and base.ProtocolReceiver.
80    """
81    return _Subscription(
82        base.Subscription.Kind.FULL, None, None, operator, protocol_receiver
83    )
84