GRPCAsyncClientStreamingCall.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.6)
  17. import NIOHPACK
  18. /// Async-await variant of `ClientStreamingCall`.
  19. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  20. public struct GRPCAsyncClientStreamingCall<Request: Sendable, Response: Sendable>: Sendable {
  21. private let call: Call<Request, Response>
  22. private let responseParts: UnaryResponseParts<Response>
  23. /// A request stream writer for sending messages to the server.
  24. public let requestStream: GRPCAsyncRequestStreamWriter<Request>
  25. /// The options used to make the RPC.
  26. public var options: CallOptions {
  27. return self.call.options
  28. }
  29. /// Cancel this RPC if it hasn't already completed.
  30. public func cancel() async throws {
  31. try await self.call.cancel().get()
  32. }
  33. // MARK: - Response Parts
  34. /// The initial metadata returned from the server.
  35. ///
  36. /// - Important: The initial metadata will only be available when the response has been received.
  37. public var initialMetadata: HPACKHeaders {
  38. get async throws {
  39. try await self.responseParts.initialMetadata.get()
  40. }
  41. }
  42. /// The response returned by the server.
  43. public var response: Response {
  44. get async throws {
  45. try await self.responseParts.response.get()
  46. }
  47. }
  48. /// The trailing metadata returned from the server.
  49. ///
  50. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  51. public var trailingMetadata: HPACKHeaders {
  52. get async throws {
  53. try await self.responseParts.trailingMetadata.get()
  54. }
  55. }
  56. /// The final status of the the RPC.
  57. ///
  58. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  59. public var status: GRPCStatus {
  60. get async {
  61. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  62. try! await self.responseParts.status.get()
  63. }
  64. }
  65. private init(call: Call<Request, Response>) {
  66. self.call = call
  67. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  68. self.call.invokeStreamingRequests(
  69. onError: self.responseParts.handleError(_:),
  70. onResponsePart: self.responseParts.handle(_:)
  71. )
  72. self.requestStream = call.makeRequestStreamWriter()
  73. }
  74. /// We expose this as the only non-private initializer so that the caller
  75. /// knows that invocation is part of initialisation.
  76. internal static func makeAndInvoke(call: Call<Request, Response>) -> Self {
  77. Self(call: call)
  78. }
  79. }
  80. #endif