GRPCAsyncUnaryCall.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2021, 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. #if compiler(>=5.5)
  17. import NIOHPACK
  18. /// A unary gRPC call. The request is sent on initialization.
  19. ///
  20. /// Note: while this object is a `struct`, its implementation delegates to `Call`. It therefore
  21. /// has reference semantics.
  22. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  23. public struct GRPCAsyncUnaryCall<Request, Response> {
  24. private let call: Call<Request, Response>
  25. private let responseParts: UnaryResponseParts<Response>
  26. /// The options used to make the RPC.
  27. public var options: CallOptions {
  28. self.call.options
  29. }
  30. /// Cancel this RPC if it hasn't already completed.
  31. public func cancel() async throws {
  32. try await self.call.cancel().get()
  33. }
  34. // MARK: - Response Parts
  35. /// The initial metadata returned from the server.
  36. public var initialMetadata: HPACKHeaders {
  37. // swiftformat:disable:next redundantGet
  38. get async throws {
  39. try await self.responseParts.initialMetadata.get()
  40. }
  41. }
  42. /// The response message returned from the service if the call is successful. This may be throw
  43. /// if the call encounters an error.
  44. ///
  45. /// Callers should rely on the `status` of the call for the canonical outcome.
  46. public var response: Response {
  47. // swiftformat:disable:next redundantGet
  48. get async throws {
  49. try await self.responseParts.response.get()
  50. }
  51. }
  52. /// The trailing metadata returned from the server.
  53. ///
  54. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  55. public var trailingMetadata: HPACKHeaders {
  56. // swiftformat:disable:next redundantGet
  57. get async throws {
  58. try await self.responseParts.trailingMetadata.get()
  59. }
  60. }
  61. /// The final status of the the RPC.
  62. ///
  63. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  64. public var status: GRPCStatus {
  65. // swiftformat:disable:next redundantGet
  66. get async {
  67. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  68. try! await self.responseParts.status.get()
  69. }
  70. }
  71. private init(
  72. call: Call<Request, Response>,
  73. _ request: Request
  74. ) {
  75. self.call = call
  76. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  77. self.call.invokeUnaryRequest(
  78. request,
  79. onError: self.responseParts.handleError(_:),
  80. onResponsePart: self.responseParts.handle(_:)
  81. )
  82. }
  83. /// We expose this as the only non-private initializer so that the caller
  84. /// knows that invocation is part of initialisation.
  85. internal static func makeAndInvoke(
  86. call: Call<Request, Response>,
  87. _ request: Request
  88. ) -> Self {
  89. Self(call: call, request)
  90. }
  91. }
  92. #endif