callouts/python/extproc/example/add_header/service_callout_example.py (23 lines of code) (raw):

# Copyright 2024 Google LLC. # # 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 logging from grpc import ServicerContext from envoy.service.ext_proc.v3 import external_processor_pb2 as service_pb2 from extproc.service import callout_server from extproc.service import callout_tools class CalloutServerExample(callout_server.CalloutServer): """Example callout server. For request header callouts we provide a mutation to add a header '{header-request: request}', remove a header 'foo', and to clear the route cache. On response header callouts, we respond with a mutation to add the header '{header-response: response}'. """ def on_request_headers( self, headers: service_pb2.HttpHeaders, context: ServicerContext ) -> service_pb2.HeadersResponse: """Custom processor on request headers. Args: headers (service_pb2.HttpHeaders): The HTTP headers received in the request. context (ServicerContext): The context object for the gRPC service. Returns: service_pb2.HeadersResponse: The response containing the mutations to be applied to the request headers. """ return callout_tools.add_header_mutation( add=[('header-request', 'request')], clear_route_cache=True ) def on_response_headers( self, headers: service_pb2.HttpHeaders, context: ServicerContext ) -> service_pb2.HeadersResponse: """Custom processor on response headers. Args: headers (service_pb2.HttpHeaders): The HTTP headers received in the response. context (ServicerContext): The context object for the gRPC service. Returns: service_pb2.HeadersResponse: The response containing the mutations to be applied to the response headers. """ return callout_tools.add_header_mutation( add=[('header-response', 'response')], remove=['foo'] ) if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) # Run the gRPC service CalloutServerExample().run()