aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio_testing/grpc_testing/_server/_servicer_context.py
blob: 90eeb130d3034b8d7f2e04882367352fa39381a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import grpc
from grpc_testing import _common


class ServicerContext(grpc.ServicerContext):

    def __init__(self, rpc, time, deadline):
        self._rpc = rpc
        self._time = time
        self._deadline = deadline

    def is_active(self):
        return self._rpc.is_active()

    def time_remaining(self):
        if self._rpc.is_active():
            if self._deadline is None:
                return None
            else:
                return max(0.0, self._deadline - self._time.time())
        else:
            return 0.0

    def cancel(self):
        self._rpc.application_cancel()

    def add_callback(self, callback):
        return self._rpc.add_callback(callback)

    def invocation_metadata(self):
        return self._rpc.invocation_metadata()

    def peer(self):
        raise NotImplementedError()

    def peer_identities(self):
        raise NotImplementedError()

    def peer_identity_key(self):
        raise NotImplementedError()

    def auth_context(self):
        raise NotImplementedError()

    def send_initial_metadata(self, initial_metadata):
        initial_metadata_sent = self._rpc.send_initial_metadata(
            _common.fuss_with_metadata(initial_metadata))
        if not initial_metadata_sent:
            raise ValueError(
                'ServicerContext.send_initial_metadata called too late!')

    def set_trailing_metadata(self, trailing_metadata):
        self._rpc.set_trailing_metadata(
            _common.fuss_with_metadata(trailing_metadata))

    def abort(self, code, details):
        raise NotImplementedError()

    def set_code(self, code):
        self._rpc.set_code(code)

    def set_details(self, details):
        self._rpc.set_details(details)