diff --git a/Sources/Examples/EchoNIO/main.swift b/Sources/Examples/EchoNIO/main.swift index a23957923..6a6d8b342 100644 --- a/Sources/Examples/EchoNIO/main.swift +++ b/Sources/Examples/EchoNIO/main.swift @@ -29,11 +29,17 @@ let messageOption = Option("message", default: "Testing 1 2 3", description: "message to send") -func makeEchoClient(address: String, port: Int) throws -> EchoClient { +/// Create en `EchoClient` and wait for it to initialize. Returns nil if initialisation fails. +func makeEchoClient(address: String, port: Int) -> EchoClient? { let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - return try GRPCClient.start(host: address, port: port, eventLoopGroup: eventLoopGroup) - .map { client in EchoClient(client: client) } - .wait() + do { + return try GRPCClient.start(host: address, port: port, eventLoopGroup: eventLoopGroup) + .map { client in EchoClient(client: client) } + .wait() + } catch { + print("Unable to create an EchoClient: \(error)") + return nil + } } Group { @@ -66,7 +72,7 @@ Group { description: "Perform a unary get()." ) { address, port, message in print("calling get") - let echo = try! makeEchoClient(address: address, port: port) + guard let echo = makeEchoClient(address: address, port: port) else { return } var requestMessage = Echo_EchoRequest() requestMessage.text = message @@ -77,7 +83,17 @@ Group { print("get received: \(response.text)") } - _ = try! get.response.wait() + get.response.whenFailure { error in + print("get response failed with error: \(error)") + } + + // wait() on the status to stop the program from exiting. + do { + let status = try get.status.wait() + print("get completed with status: \(status)") + } catch { + print("get status failed with error: \(error)") + } } $0.command( @@ -88,7 +104,7 @@ Group { description: "Perform a server-streaming expand()." ) { address, port, message in print("calling expand") - let echo = try! makeEchoClient(address: address, port: port) + guard let echo = makeEchoClient(address: address, port: port) else { return } var requestMessage = Echo_EchoRequest() requestMessage.text = message @@ -98,7 +114,13 @@ Group { print("expand received: \(response.text)") } - _ = try! expand.status.wait() + // wait() on the status to stop the program from exiting. + do { + let status = try expand.status.wait() + print("expand completed with status: \(status)") + } catch { + print("expand status failed with error: \(error)") + } } $0.command( @@ -109,7 +131,7 @@ Group { description: "Perform a client-streaming collect()." ) { address, port, message in print("calling collect") - let echo = try! makeEchoClient(address: address, port: port) + guard let echo = makeEchoClient(address: address, port: port) else { return } let collect = echo.collect() @@ -125,7 +147,17 @@ Group { print("collect received: \(resposne.text)") } - _ = try! collect.status.wait() + collect.response.whenFailure { error in + print("collect response failed with error: \(error)") + } + + // wait() on the status to stop the program from exiting. + do { + let status = try collect.status.wait() + print("collect completed with status: \(status)") + } catch { + print("collect status failed with error: \(error)") + } } $0.command( @@ -136,7 +168,7 @@ Group { description: "Perform a bidirectional-streaming update()." ) { address, port, message in print("calling update") - let echo = try! makeEchoClient(address: address, port: port) + guard let echo = makeEchoClient(address: address, port: port) else { return } let update = echo.update { response in print("update received: \(response.text)") @@ -150,6 +182,12 @@ Group { } update.send(.end) - _ = try! update.status.wait() + // wait() on the status to stop the program from exiting. + do { + let status = try update.status.wait() + print("update completed with status: \(status)") + } catch { + print("update status failed with error: \(error)") + } } }.run() diff --git a/Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift b/Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift index f3e8d2905..35878f77e 100644 --- a/Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift +++ b/Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift @@ -27,7 +27,7 @@ public protocol ClientCall { var subchannel: EventLoopFuture { get } /// Initial response metadata. - var metadata: EventLoopFuture { get } + var initialMetadata: EventLoopFuture { get } /// Response status. var status: EventLoopFuture { get } @@ -78,7 +78,7 @@ public protocol UnaryResponseClientCall: ClientCall { public class BaseClientCall: ClientCall { public let subchannel: EventLoopFuture - public let metadata: EventLoopFuture + public let initialMetadata: EventLoopFuture public let status: EventLoopFuture /// Sets up a gRPC call. @@ -113,7 +113,7 @@ public class BaseClientCall: } self.subchannel = subchannelPromise.futureResult - self.metadata = metadataPromise.futureResult + self.initialMetadata = metadataPromise.futureResult self.status = statusPromise.futureResult } @@ -121,6 +121,8 @@ public class BaseClientCall: var requestHead = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .POST, uri: path) requestHead.headers.add(name: "host", value: host) requestHead.headers.add(name: "content-type", value: "application/grpc") + requestHead.headers.add(name: "te", value: "trailers") + requestHead.headers.add(name: "user-agent", value: "grpc-swift-nio") return requestHead } } diff --git a/Sources/SwiftGRPCNIO/ClientCalls/ServerStreamingClientCall.swift b/Sources/SwiftGRPCNIO/ClientCalls/ServerStreamingClientCall.swift index 1e1435708..3edd87686 100644 --- a/Sources/SwiftGRPCNIO/ClientCalls/ServerStreamingClientCall.swift +++ b/Sources/SwiftGRPCNIO/ClientCalls/ServerStreamingClientCall.swift @@ -18,7 +18,6 @@ import SwiftProtobuf import NIO public class ServerStreamingClientCall: BaseClientCall { - public init(client: GRPCClient, path: String, request: RequestMessage, handler: @escaping (ResponseMessage) -> Void) { super.init(channel: client.channel, multiplexer: client.multiplexer, responseHandler: .callback(handler: handler)) diff --git a/Sources/SwiftGRPCNIO/ClientCalls/UnaryClientCall.swift b/Sources/SwiftGRPCNIO/ClientCalls/UnaryClientCall.swift index 341355851..a1086c4d9 100644 --- a/Sources/SwiftGRPCNIO/ClientCalls/UnaryClientCall.swift +++ b/Sources/SwiftGRPCNIO/ClientCalls/UnaryClientCall.swift @@ -17,20 +17,20 @@ import Foundation import SwiftProtobuf import NIO -public class UnaryClientCall: BaseClientCall, UnaryResponseClientCall { - public let response: EventLoopFuture +public class UnaryClientCall: BaseClientCall, UnaryResponseClientCall { + public let response: EventLoopFuture - public init(client: GRPCClient, path: String, request: Request) { - let responsePromise: EventLoopPromise = client.channel.eventLoop.newPromise() + public init(client: GRPCClient, path: String, request: RequestMessage) { + let responsePromise: EventLoopPromise = client.channel.eventLoop.newPromise() self.response = responsePromise.futureResult super.init(channel: client.channel, multiplexer: client.multiplexer, responseHandler: .fulfill(promise: responsePromise)) let requestHead = makeRequestHead(path: path, host: client.host) subchannel.whenSuccess { channel in - channel.write(GRPCClientRequestPart.head(requestHead), promise: nil) - channel.write(GRPCClientRequestPart.message(request), promise: nil) - channel.writeAndFlush(GRPCClientRequestPart.end, promise: nil) + channel.write(GRPCClientRequestPart.head(requestHead), promise: nil) + channel.write(GRPCClientRequestPart.message(request), promise: nil) + channel.writeAndFlush(GRPCClientRequestPart.end, promise: nil) } } } diff --git a/Sources/SwiftGRPCNIO/GRPCClient.swift b/Sources/SwiftGRPCNIO/GRPCClient.swift index e4393b444..e70354c4c 100644 --- a/Sources/SwiftGRPCNIO/GRPCClient.swift +++ b/Sources/SwiftGRPCNIO/GRPCClient.swift @@ -18,7 +18,6 @@ import NIO import NIOHTTP2 public final class GRPCClient { - public static func start( host: String, port: Int,