UnaryCall.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 NIOCore
  19. import NIOHPACK
  20. import NIOHTTP1
  21. import NIOHTTP2
  22. import SwiftProtobuf
  23. /// A unary gRPC call. The request is sent on initialization.
  24. ///
  25. /// Note: while this object is a `struct`, its implementation delegates to `Call`. It therefore
  26. /// has reference semantics.
  27. public struct UnaryCall<RequestPayload, ResponsePayload>: UnaryResponseClientCall {
  28. private let call: Call<RequestPayload, ResponsePayload>
  29. private let responseParts: UnaryResponseParts<ResponsePayload>
  30. /// The options used to make the RPC.
  31. public var options: CallOptions {
  32. return self.call.options
  33. }
  34. /// The path used to make the RPC.
  35. public var path: String {
  36. return self.call.path
  37. }
  38. /// The `Channel` used to transport messages for this RPC.
  39. public var subchannel: EventLoopFuture<Channel> {
  40. return self.call.channel
  41. }
  42. /// The `EventLoop` this call is running on.
  43. public var eventLoop: EventLoop {
  44. return self.call.eventLoop
  45. }
  46. /// Cancel this RPC if it hasn't already completed.
  47. public func cancel(promise: EventLoopPromise<Void>?) {
  48. self.call.cancel(promise: promise)
  49. }
  50. // MARK: - Response Parts
  51. /// The initial metadata returned from the server.
  52. public var initialMetadata: EventLoopFuture<HPACKHeaders> {
  53. return self.responseParts.initialMetadata
  54. }
  55. /// The response returned by the server.
  56. public var response: EventLoopFuture<ResponsePayload> {
  57. return self.responseParts.response
  58. }
  59. /// The trailing metadata returned from the server.
  60. public var trailingMetadata: EventLoopFuture<HPACKHeaders> {
  61. return self.responseParts.trailingMetadata
  62. }
  63. /// The final status of the the RPC.
  64. public var status: EventLoopFuture<GRPCStatus> {
  65. return self.responseParts.status
  66. }
  67. internal init(call: Call<RequestPayload, ResponsePayload>) {
  68. self.call = call
  69. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  70. }
  71. internal func invoke(_ request: RequestPayload) {
  72. self.call.invokeUnaryRequest(
  73. request,
  74. onError: self.responseParts.handleError(_:),
  75. onResponsePart: self.responseParts.handle(_:)
  76. )
  77. }
  78. }