|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from collections import namedtuple |
| 5 | +from typing import List, Tuple |
| 6 | + |
| 7 | +import grpc |
| 8 | + |
| 9 | + |
| 10 | +class _ClientCallDetails( |
| 11 | + namedtuple( |
| 12 | + '_ClientCallDetails', |
| 13 | + ['method', 'timeout', 'metadata', 'credentials', 'wait_for_ready', 'compression']), |
| 14 | + grpc.ClientCallDetails): |
| 15 | + """This is an implementation of the ClientCallDetails interface needed for interceptors. |
| 16 | + This class takes six named values and inherits the ClientCallDetails from grpc package. |
| 17 | + This class encloses the values that describe a RPC to be invoked. |
| 18 | + """ |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +class DefaultClientInterceptorImpl ( |
| 23 | + grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor, |
| 24 | + grpc.StreamUnaryClientInterceptor, grpc.StreamStreamClientInterceptor): |
| 25 | + """The class implements a UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, |
| 26 | + StreamUnaryClientInterceptor and StreamStreamClientInterceptor from grpc to add an |
| 27 | + interceptor to add additional headers to all calls as needed.""" |
| 28 | + |
| 29 | + def __init__(self, metadata: List[Tuple[str, str]]): |
| 30 | + super().__init__() |
| 31 | + self._metadata = metadata |
| 32 | + |
| 33 | + def _intercept_call( |
| 34 | + self, client_call_details: _ClientCallDetails) -> grpc.ClientCallDetails: |
| 35 | + """Internal intercept_call implementation which adds metadata to grpc metadata in the RPC |
| 36 | + call details.""" |
| 37 | + if self._metadata is None: |
| 38 | + return client_call_details |
| 39 | + |
| 40 | + if client_call_details.metadata is not None: |
| 41 | + metadata = list(client_call_details.metadata) |
| 42 | + else: |
| 43 | + metadata = [] |
| 44 | + |
| 45 | + metadata.extend(self._metadata) |
| 46 | + client_call_details = _ClientCallDetails( |
| 47 | + client_call_details.method, client_call_details.timeout, metadata, |
| 48 | + client_call_details.credentials, client_call_details.wait_for_ready, client_call_details.compression) |
| 49 | + |
| 50 | + return client_call_details |
| 51 | + |
| 52 | + def intercept_unary_unary(self, continuation, client_call_details, request): |
| 53 | + new_client_call_details = self._intercept_call(client_call_details) |
| 54 | + return continuation(new_client_call_details, request) |
| 55 | + |
| 56 | + def intercept_unary_stream(self, continuation, client_call_details, request): |
| 57 | + new_client_call_details = self._intercept_call(client_call_details) |
| 58 | + return continuation(new_client_call_details, request) |
| 59 | + |
| 60 | + def intercept_stream_unary(self, continuation, client_call_details, request): |
| 61 | + new_client_call_details = self._intercept_call(client_call_details) |
| 62 | + return continuation(new_client_call_details, request) |
| 63 | + |
| 64 | + def intercept_stream_stream(self, continuation, client_call_details, request): |
| 65 | + new_client_call_details = self._intercept_call(client_call_details) |
| 66 | + return continuation(new_client_call_details, request) |
0 commit comments