UnaryResponseCallContext.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Foundation
  2. import SwiftProtobuf
  3. import NIO
  4. import NIOHTTP1
  5. /// Abstract base class exposing a method that exposes a promise for the RPC response.
  6. ///
  7. /// - When `responsePromise` is fulfilled, the call is closed and the provided response transmitted with status `responseStatus` (`.ok` by default).
  8. /// - If `statusPromise` is failed and the error is of type `GRPCStatusTransformable`,
  9. /// the result of `error.asGRPCStatus()` will be returned to the client.
  10. /// - If `error.asGRPCStatus()` is not available, `GRPCStatus.processingError` is returned to the client.
  11. ///
  12. /// For unary calls, the response is not actually provided by fulfilling `responsePromise`, but instead by completing
  13. /// the future returned by `UnaryCallHandler.EventObserver`.
  14. open class UnaryResponseCallContext<ResponseMessage: Message>: ServerCallContextBase, StatusOnlyCallContext {
  15. public typealias WrappedResponse = GRPCServerResponsePart<ResponseMessage>
  16. public let responsePromise: EventLoopPromise<ResponseMessage>
  17. public var responseStatus: GRPCStatus = .ok
  18. public override init(eventLoop: EventLoop, request: HTTPRequestHead) {
  19. self.responsePromise = eventLoop.makePromise()
  20. super.init(eventLoop: eventLoop, request: request)
  21. }
  22. }
  23. /// Protocol variant of `UnaryResponseCallContext` that only exposes the `responseStatus` field, but not
  24. /// `responsePromise`.
  25. ///
  26. /// Motivation: `UnaryCallHandler` already asks the call handler return an `EventLoopFuture<ResponseMessage>` which
  27. /// is automatically cascaded into `UnaryResponseCallContext.responsePromise`, so that promise does not (and should not)
  28. /// be fulfilled by the user.
  29. ///
  30. /// We can use a protocol (instead of an abstract base class) here because removing the generic `responsePromise` field
  31. /// lets us avoid associated-type requirements on the protocol.
  32. public protocol StatusOnlyCallContext: ServerCallContext {
  33. var responseStatus: GRPCStatus { get set }
  34. }
  35. /// Concrete implementation of `UnaryResponseCallContext` used by our generated code.
  36. open class UnaryResponseCallContextImpl<ResponseMessage: Message>: UnaryResponseCallContext<ResponseMessage> {
  37. public let channel: Channel
  38. /// - Parameters:
  39. /// - channel: The NIO channel the call is handled on.
  40. /// - request: The headers provided with this call.
  41. /// - errorDelegate: Provides a means for transforming response promise failures to `GRPCStatusTransformable` before
  42. /// sending them to the client.
  43. public init(channel: Channel, request: HTTPRequestHead, errorDelegate: ServerErrorDelegate?) {
  44. self.channel = channel
  45. super.init(eventLoop: channel.eventLoop, request: request)
  46. responsePromise.futureResult
  47. // Send the response provided to the promise.
  48. .map { responseMessage in
  49. self.channel.writeAndFlush(NIOAny(WrappedResponse.message(responseMessage)))
  50. }
  51. .map { _ in
  52. self.responseStatus
  53. }
  54. // Ensure that any error provided can be transformed to `GRPCStatus`, using "internal server error" as a fallback.
  55. .recover { [weak errorDelegate] error in
  56. errorDelegate?.observeRequestHandlerError(error, request: request)
  57. return errorDelegate?.transformRequestHandlerError(error, request: request)
  58. ?? (error as? GRPCStatusTransformable)?.asGRPCStatus()
  59. ?? .processingError
  60. }
  61. // Finish the call by returning the final status.
  62. .whenSuccess { status in
  63. self.channel.writeAndFlush(NIOAny(WrappedResponse.status(status)), promise: nil)
  64. }
  65. }
  66. }
  67. /// Concrete implementation of `UnaryResponseCallContext` used for testing.
  68. ///
  69. /// Only provided to make it clear in tests that no "real" implementation is used.
  70. open class UnaryResponseCallContextTestStub<ResponseMessage: Message>: UnaryResponseCallContext<ResponseMessage> { }