/* * Copyright 2021, gRPC Authors All rights reserved. * * 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 EchoModel import GRPC import NIOCore import SwiftProtobuf /// A client interceptor which delegates the implementation of `send` and `receive` to callbacks. final class DelegatingClientInterceptor< Request: Message, Response: Message >: ClientInterceptor { typealias RequestPart = GRPCClientRequestPart typealias ResponsePart = GRPCClientResponsePart typealias Context = ClientInterceptorContext typealias OnSend = (RequestPart, EventLoopPromise?, Context) -> Void typealias OnReceive = (ResponsePart, Context) -> Void private let onSend: OnSend private let onReceive: OnReceive init( onSend: @escaping OnSend = { part, promise, context in context.send(part, promise: promise) }, onReceive: @escaping OnReceive = { part, context in context.receive(part) } ) { self.onSend = onSend self.onReceive = onReceive } override func send( _ part: GRPCClientRequestPart, promise: EventLoopPromise?, context: ClientInterceptorContext ) { self.onSend(part, promise, context) } override func receive( _ part: GRPCClientResponsePart, context: ClientInterceptorContext ) { self.onReceive(part, context) } } final class DelegatingEchoClientInterceptorFactory: Echo_EchoClientInterceptorFactoryProtocol { typealias OnSend = DelegatingClientInterceptor.OnSend let interceptor: DelegatingClientInterceptor init(onSend: @escaping OnSend) { self.interceptor = DelegatingClientInterceptor(onSend: onSend) } func makeGetInterceptors() -> [ClientInterceptor] { return [self.interceptor] } func makeExpandInterceptors() -> [ClientInterceptor] { return [self.interceptor] } func makeCollectInterceptors() -> [ClientInterceptor] { return [self.interceptor] } func makeUpdateInterceptors() -> [ClientInterceptor] { return [self.interceptor] } }