ServerRequest.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2023, 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. /// A request received at the server containing a single message.
  17. public struct ServerRequest<Message: Sendable>: Sendable {
  18. /// Metadata received from the client at the start of the RPC.
  19. ///
  20. /// The metadata contains gRPC and transport specific entries in addition to user-specified
  21. /// metadata.
  22. public var metadata: Metadata
  23. /// The message received from the client.
  24. public var message: Message
  25. /// Create a new single server request.
  26. ///
  27. /// - Parameters:
  28. /// - metadata: Metadata received from the client.
  29. /// - message: The message received from the client.
  30. public init(metadata: Metadata, message: Message) {
  31. self.metadata = metadata
  32. self.message = message
  33. }
  34. }
  35. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  36. /// A request received at the server containing a stream of messages.
  37. public struct StreamingServerRequest<Message: Sendable>: Sendable {
  38. /// Metadata received from the client at the start of the RPC.
  39. ///
  40. /// The metadata contains gRPC and transport specific entries in addition to user-specified
  41. /// metadata.
  42. public var metadata: Metadata
  43. /// A sequence of messages received from the client.
  44. ///
  45. /// The sequence may be iterated at most once.
  46. public var messages: RPCAsyncSequence<Message, any Error>
  47. /// Create a new streaming request.
  48. ///
  49. /// - Parameters:
  50. /// - metadata: Metadata received from the client.
  51. /// - messages: A sequence of messages received from the client.
  52. public init(metadata: Metadata, messages: RPCAsyncSequence<Message, any Error>) {
  53. self.metadata = metadata
  54. self.messages = messages
  55. }
  56. }
  57. // MARK: - Conversion
  58. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  59. extension StreamingServerRequest {
  60. public init(single request: ServerRequest<Message>) {
  61. self.init(metadata: request.metadata, messages: .one(request.message))
  62. }
  63. }
  64. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  65. extension ServerRequest {
  66. public init(stream request: StreamingServerRequest<Message>) async throws {
  67. var iterator = request.messages.makeAsyncIterator()
  68. guard let message = try await iterator.next() else {
  69. throw RPCError(code: .internalError, message: "Empty stream.")
  70. }
  71. guard try await iterator.next() == nil else {
  72. throw RPCError(code: .internalError, message: "Too many messages.")
  73. }
  74. self = ServerRequest(metadata: request.metadata, message: message)
  75. }
  76. }