ServerRequest.swift 2.9 KB

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