1# Copyright 2017 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"""Interceptor that adds headers to outgoing requests.""" 15 16import collections 17 18import grpc 19import generic_client_interceptor 20 21 22class _ClientCallDetails( 23 collections.namedtuple( 24 '_ClientCallDetails', 25 ('method', 'timeout', 'metadata', 'credentials')), 26 grpc.ClientCallDetails): 27 pass 28 29 30def header_adder_interceptor(header, value): 31 32 def intercept_call(client_call_details, request_iterator, request_streaming, 33 response_streaming): 34 metadata = [] 35 if client_call_details.metadata is not None: 36 metadata = list(client_call_details.metadata) 37 metadata.append(( 38 header, 39 value, 40 )) 41 client_call_details = _ClientCallDetails( 42 client_call_details.method, client_call_details.timeout, metadata, 43 client_call_details.credentials) 44 return client_call_details, request_iterator, None 45 46 return generic_client_interceptor.create(intercept_call) 47