/* * Copyright 2020, 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 NIO import SwiftProtobuf // We can't use a 'where' clause on 'init's to constrain the generic requirements of a type. Instead // we'll use static methods on this factory. public enum CallHandlerFactory { public typealias UnaryContext = UnaryResponseCallContext public typealias UnaryEventObserver = (Request) -> EventLoopFuture public static func makeUnary( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (UnaryContext) -> UnaryEventObserver ) -> UnaryCallHandler, ProtobufSerializer> { return UnaryCallHandler( serializer: ProtobufSerializer(), deserializer: ProtobufDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public static func makeUnary( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (UnaryContext) -> UnaryEventObserver ) -> UnaryCallHandler, GRPCPayloadSerializer> { return UnaryCallHandler( serializer: GRPCPayloadSerializer(), deserializer: GRPCPayloadDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public typealias ClientStreamingContext = UnaryResponseCallContext public typealias ClientStreamingEventObserver = EventLoopFuture<(StreamEvent) -> Void> public static func makeClientStreaming( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (ClientStreamingContext) -> ClientStreamingEventObserver ) -> ClientStreamingCallHandler, ProtobufSerializer> { return ClientStreamingCallHandler( serializer: ProtobufSerializer(), deserializer: ProtobufDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public static func makeClientStreaming( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (ClientStreamingContext) -> ClientStreamingEventObserver ) -> ClientStreamingCallHandler< GRPCPayloadDeserializer, GRPCPayloadSerializer > { return ClientStreamingCallHandler( serializer: GRPCPayloadSerializer(), deserializer: GRPCPayloadDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public typealias ServerStreamingContext = StreamingResponseCallContext public typealias ServerStreamingEventObserver = (Request) -> EventLoopFuture public static func makeServerStreaming( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (ServerStreamingContext) -> ServerStreamingEventObserver ) -> ServerStreamingCallHandler, ProtobufSerializer> { return ServerStreamingCallHandler( serializer: ProtobufSerializer(), deserializer: ProtobufDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public static func makeServerStreaming( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (ServerStreamingContext) -> ServerStreamingEventObserver ) -> ServerStreamingCallHandler< GRPCPayloadDeserializer, GRPCPayloadSerializer > { return ServerStreamingCallHandler( serializer: GRPCPayloadSerializer(), deserializer: GRPCPayloadDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public typealias BidirectionalStreamingContext = StreamingResponseCallContext public typealias BidirectionalStreamingEventObserver = EventLoopFuture<(StreamEvent) -> Void> public static func makeBidirectionalStreaming( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (BidirectionalStreamingContext) -> BidirectionalStreamingEventObserver ) -> BidirectionalStreamingCallHandler< ProtobufDeserializer, ProtobufSerializer > { return BidirectionalStreamingCallHandler( serializer: ProtobufSerializer(), deserializer: ProtobufDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } public static func makeBidirectionalStreaming( callHandlerContext: CallHandlerContext, interceptors: [ServerInterceptor] = [], eventObserverFactory: @escaping (BidirectionalStreamingContext) -> BidirectionalStreamingEventObserver ) -> BidirectionalStreamingCallHandler< GRPCPayloadDeserializer, GRPCPayloadSerializer > { return BidirectionalStreamingCallHandler( serializer: GRPCPayloadSerializer(), deserializer: GRPCPayloadDeserializer(), callHandlerContext: callHandlerContext, interceptors: interceptors, eventObserverFactory: eventObserverFactory ) } }