ServerRequest.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 namespace for request message types used by servers.
  17. public enum ServerRequest {}
  18. extension ServerRequest {
  19. /// A request received at the server containing a single message.
  20. public struct Single<Message: Sendable>: Sendable {
  21. /// Metadata received from the client at the start of the RPC.
  22. ///
  23. /// The metadata contains gRPC and transport specific entries in addition to user-specified
  24. /// metadata.
  25. public var metadata: Metadata
  26. /// The message received from the client.
  27. public var message: Message
  28. /// Create a new single server request.
  29. ///
  30. /// - Parameters:
  31. /// - metadata: Metadata received from the client.
  32. /// - messages: The message received from the client.
  33. public init(metadata: Metadata, message: Message) {
  34. self.metadata = metadata
  35. self.message = message
  36. }
  37. }
  38. }
  39. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  40. extension ServerRequest {
  41. /// A request received at the server containing a stream of messages.
  42. public struct Stream<Message: Sendable>: Sendable {
  43. /// Metadata received from the client at the start of the RPC.
  44. ///
  45. /// The metadata contains gRPC and transport specific entries in addition to user-specified
  46. /// metadata.
  47. public var metadata: Metadata
  48. /// A sequence of messages received from the client.
  49. ///
  50. /// The sequence may be iterated at most once.
  51. public var messages: RPCAsyncSequence<Message>
  52. /// Create a new streaming request.
  53. ///
  54. /// - Parameters:
  55. /// - metadata: Metadata received from the client.
  56. /// - messages: A sequence of messages received from the client.
  57. public init(metadata: Metadata, messages: RPCAsyncSequence<Message>) {
  58. self.metadata = metadata
  59. self.messages = messages
  60. }
  61. }
  62. }
  63. // MARK: - Conversion
  64. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  65. extension ServerRequest.Stream {
  66. @_spi(Testing)
  67. public init(single request: ServerRequest.Single<Message>) {
  68. self.init(metadata: request.metadata, messages: .one(request.message))
  69. }
  70. }