Skip to content

Commit

Permalink
Renaming and removal of force unwrap/try
Browse files Browse the repository at this point in the history
  • Loading branch information
glbrntt committed Jan 25, 2019
1 parent 056f8b5 commit bf3bb5e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
62 changes: 50 additions & 12 deletions Sources/Examples/EchoNIO/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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()

Expand All @@ -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(
Expand All @@ -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)")
Expand All @@ -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()
8 changes: 5 additions & 3 deletions Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public protocol ClientCall {
var subchannel: EventLoopFuture<Channel> { get }

/// Initial response metadata.
var metadata: EventLoopFuture<HTTPHeaders> { get }
var initialMetadata: EventLoopFuture<HTTPHeaders> { get }

/// Response status.
var status: EventLoopFuture<GRPCStatus> { get }
Expand Down Expand Up @@ -78,7 +78,7 @@ public protocol UnaryResponseClientCall: ClientCall {

public class BaseClientCall<RequestMessage: Message, ResponseMessage: Message>: ClientCall {
public let subchannel: EventLoopFuture<Channel>
public let metadata: EventLoopFuture<HTTPHeaders>
public let initialMetadata: EventLoopFuture<HTTPHeaders>
public let status: EventLoopFuture<GRPCStatus>

/// Sets up a gRPC call.
Expand Down Expand Up @@ -113,14 +113,16 @@ public class BaseClientCall<RequestMessage: Message, ResponseMessage: Message>:
}

self.subchannel = subchannelPromise.futureResult
self.metadata = metadataPromise.futureResult
self.initialMetadata = metadataPromise.futureResult
self.status = statusPromise.futureResult
}

internal func makeRequestHead(path: String, host: String) -> HTTPRequestHead {
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import SwiftProtobuf
import NIO

public class ServerStreamingClientCall<RequestMessage: Message, ResponseMessage: Message>: BaseClientCall<RequestMessage, ResponseMessage> {

public init(client: GRPCClient, path: String, request: RequestMessage, handler: @escaping (ResponseMessage) -> Void) {
super.init(channel: client.channel, multiplexer: client.multiplexer, responseHandler: .callback(handler: handler))

Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftGRPCNIO/ClientCalls/UnaryClientCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ import Foundation
import SwiftProtobuf
import NIO

public class UnaryClientCall<Request: Message, Response: Message>: BaseClientCall<Request, Response>, UnaryResponseClientCall {
public let response: EventLoopFuture<Response>
public class UnaryClientCall<RequestMessage: Message, ResponseMessage: Message>: BaseClientCall<RequestMessage, ResponseMessage>, UnaryResponseClientCall {
public let response: EventLoopFuture<ResponseMessage>

public init(client: GRPCClient, path: String, request: Request) {
let responsePromise: EventLoopPromise<Response> = client.channel.eventLoop.newPromise()
public init(client: GRPCClient, path: String, request: RequestMessage) {
let responsePromise: EventLoopPromise<ResponseMessage> = 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<Request>.head(requestHead), promise: nil)
channel.write(GRPCClientRequestPart<Request>.message(request), promise: nil)
channel.writeAndFlush(GRPCClientRequestPart<Request>.end, promise: nil)
channel.write(GRPCClientRequestPart<RequestMessage>.head(requestHead), promise: nil)
channel.write(GRPCClientRequestPart<RequestMessage>.message(request), promise: nil)
channel.writeAndFlush(GRPCClientRequestPart<RequestMessage>.end, promise: nil)
}
}
}
1 change: 0 additions & 1 deletion Sources/SwiftGRPCNIO/GRPCClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import NIO
import NIOHTTP2

public final class GRPCClient {

public static func start(
host: String,
port: Int,
Expand Down

0 comments on commit bf3bb5e

Please sign in to comment.