UnaryResponseCallContext.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import Logging
  18. import NIO
  19. import NIOHPACK
  20. import NIOHTTP1
  21. import SwiftProtobuf
  22. /// Abstract base class exposing a method that exposes a promise for the RPC response.
  23. ///
  24. /// - When `responsePromise` is fulfilled, the call is closed and the provided response transmitted with status `responseStatus` (`.ok` by default).
  25. /// - If `statusPromise` is failed and the error is of type `GRPCStatusTransformable`,
  26. /// the result of `error.asGRPCStatus()` will be returned to the client.
  27. /// - If `error.asGRPCStatus()` is not available, `GRPCStatus.processingError` is returned to the client.
  28. ///
  29. /// For unary calls, the response is not actually provided by fulfilling `responsePromise`, but instead by completing
  30. /// the future returned by `UnaryCallHandler.EventObserver`.
  31. open class UnaryResponseCallContext<ResponsePayload>: ServerCallContextBase, StatusOnlyCallContext {
  32. typealias WrappedResponse = GRPCServerResponsePart<ResponsePayload>
  33. public let responsePromise: EventLoopPromise<ResponsePayload>
  34. public var responseStatus: GRPCStatus = .ok
  35. public convenience init(
  36. eventLoop: EventLoop,
  37. headers: HPACKHeaders,
  38. logger: Logger,
  39. userInfo: UserInfo = UserInfo()
  40. ) {
  41. self.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: .init(userInfo))
  42. }
  43. @inlinable
  44. override internal init(
  45. eventLoop: EventLoop,
  46. headers: HPACKHeaders,
  47. logger: Logger,
  48. userInfoRef: Ref<UserInfo>
  49. ) {
  50. self.responsePromise = eventLoop.makePromise()
  51. super.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: userInfoRef)
  52. }
  53. }
  54. /// Protocol variant of `UnaryResponseCallContext` that only exposes the `responseStatus` and `trailingMetadata`
  55. /// fields, but not `responsePromise`.
  56. ///
  57. /// Motivation: `UnaryCallHandler` already asks the call handler return an `EventLoopFuture<ResponsePayload>` which
  58. /// is automatically cascaded into `UnaryResponseCallContext.responsePromise`, so that promise does not (and should not)
  59. /// be fulfilled by the user.
  60. ///
  61. /// We can use a protocol (instead of an abstract base class) here because removing the generic `responsePromise` field
  62. /// lets us avoid associated-type requirements on the protocol.
  63. public protocol StatusOnlyCallContext: ServerCallContext {
  64. var responseStatus: GRPCStatus { get set }
  65. var trailers: HPACKHeaders { get set }
  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<ResponsePayload>: UnaryResponseCallContext<ResponsePayload> {}