2
0

UnaryCall.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 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 `Channel` used to transport messages for this RPC.
  35. public var subchannel: EventLoopFuture<Channel> {
  36. return self.call.channel
  37. }
  38. /// The `EventLoop` this call is running on.
  39. public var eventLoop: EventLoop {
  40. return self.call.eventLoop
  41. }
  42. /// Cancel this RPC if it hasn't already completed.
  43. public func cancel(promise: EventLoopPromise<Void>?) {
  44. self.call.cancel(promise: promise)
  45. }
  46. // MARK: - Response Parts
  47. /// The initial metadata returned from the server.
  48. public var initialMetadata: EventLoopFuture<HPACKHeaders> {
  49. return self.responseParts.initialMetadata
  50. }
  51. /// The response returned by the server.
  52. public var response: EventLoopFuture<ResponsePayload> {
  53. return self.responseParts.response
  54. }
  55. /// The trailing metadata returned from the server.
  56. public var trailingMetadata: EventLoopFuture<HPACKHeaders> {
  57. return self.responseParts.trailingMetadata
  58. }
  59. /// The final status of the the RPC.
  60. public var status: EventLoopFuture<GRPCStatus> {
  61. return self.responseParts.status
  62. }
  63. internal init(call: Call<RequestPayload, ResponsePayload>) {
  64. self.call = call
  65. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  66. }
  67. internal func invoke(_ request: RequestPayload) {
  68. self.call.invokeUnaryRequest(
  69. request,
  70. onError: self.responseParts.handleError(_:),
  71. onResponsePart: self.responseParts.handle(_:)
  72. )
  73. }
  74. }