UnaryResponseCallContext.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /// A context provided to handlers for RPCs which return a single response, i.e. unary and client
  23. /// streaming RPCs.
  24. ///
  25. /// For client streaming RPCs the handler must complete the `responsePromise` to return the response
  26. /// to the client. Unary RPCs do complete the promise directly: they are provided an
  27. /// `StatusOnlyCallContext` view of this context where the `responsePromise` is not exposed. Instead
  28. /// they must return an `EventLoopFuture<Response>` from the method they are implementing.
  29. open class UnaryResponseCallContext<Response>: ServerCallContextBase, StatusOnlyCallContext {
  30. /// A promise for a single response message. This must be completed to send a response back to the
  31. /// client. If the promise is failed, the failure value will be converted to `GRPCStatus` and
  32. /// used as the final status for the RPC.
  33. public let responsePromise: EventLoopPromise<Response>
  34. /// The status sent back to the client at the end of the RPC, providing the `responsePromise` was
  35. /// completed successfully.
  36. public var responseStatus: GRPCStatus = .ok
  37. public convenience init(
  38. eventLoop: EventLoop,
  39. headers: HPACKHeaders,
  40. logger: Logger,
  41. userInfo: UserInfo = UserInfo()
  42. ) {
  43. self.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: .init(userInfo))
  44. }
  45. @inlinable
  46. override internal init(
  47. eventLoop: EventLoop,
  48. headers: HPACKHeaders,
  49. logger: Logger,
  50. userInfoRef: Ref<UserInfo>
  51. ) {
  52. self.responsePromise = eventLoop.makePromise()
  53. super.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: userInfoRef)
  54. }
  55. }
  56. /// Protocol variant of `UnaryResponseCallContext` that only exposes the `responseStatus` and `trailingMetadata`
  57. /// fields, but not `responsePromise`.
  58. ///
  59. /// We can use a protocol (instead of an abstract base class) here because removing the generic
  60. /// `responsePromise` field lets us avoid associated-type requirements on the protocol.
  61. public protocol StatusOnlyCallContext: ServerCallContext {
  62. /// The status sent back to the client at the end of the RPC, providing the `responsePromise` was
  63. /// completed successfully.
  64. var responseStatus: GRPCStatus { get set }
  65. /// Metadata to return at the end of the RPC.
  66. var trailers: HPACKHeaders { get set }
  67. }
  68. /// Concrete implementation of `UnaryResponseCallContext` used for testing.
  69. ///
  70. /// Only provided to make it clear in tests that no "real" implementation is used.
  71. open class UnaryResponseCallContextTestStub<Response>: UnaryResponseCallContext<Response> {}