GRPCAsyncClientStreamingCall.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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> {
  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. // swiftformat:disable:next redundantGet
  39. get async throws {
  40. try await self.responseParts.initialMetadata.get()
  41. }
  42. }
  43. /// The response returned by the server.
  44. public var response: Response {
  45. // swiftformat:disable:next redundantGet
  46. get async throws {
  47. try await self.responseParts.response.get()
  48. }
  49. }
  50. /// The trailing metadata returned from the server.
  51. ///
  52. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  53. public var trailingMetadata: HPACKHeaders {
  54. // swiftformat:disable:next redundantGet
  55. get async throws {
  56. try await self.responseParts.trailingMetadata.get()
  57. }
  58. }
  59. /// The final status of the the RPC.
  60. ///
  61. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  62. public var status: GRPCStatus {
  63. // swiftformat:disable:next redundantGet
  64. get async {
  65. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  66. try! await self.responseParts.status.get()
  67. }
  68. }
  69. private init(call: Call<Request, Response>) {
  70. self.call = call
  71. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  72. self.call.invokeStreamingRequests(
  73. onError: self.responseParts.handleError(_:),
  74. onResponsePart: self.responseParts.handle(_:)
  75. )
  76. self.requestStream = call.makeRequestStreamWriter()
  77. }
  78. /// We expose this as the only non-private initializer so that the caller
  79. /// knows that invocation is part of initialisation.
  80. internal static func makeAndInvoke(call: Call<Request, Response>) -> Self {
  81. Self(call: call)
  82. }
  83. }
  84. #endif