ClientRequest.swift 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 created by the client for a single message.
  17. ///
  18. /// This is used for unary and server-streaming RPCs.
  19. ///
  20. /// See ``StreamingClientRequest`` for streaming requests and ``ServerRequest`` for the
  21. /// servers representation of a single-message request.
  22. ///
  23. /// ## Creating ``Single`` requests
  24. ///
  25. /// ```swift
  26. /// let request = ClientRequest<String>(message: "Hello, gRPC!")
  27. /// print(request.metadata) // prints '[:]'
  28. /// print(request.message) // prints 'Hello, gRPC!'
  29. /// ```
  30. public struct ClientRequest<Message: Sendable>: Sendable {
  31. /// Caller-specified metadata to send to the server at the start of the RPC.
  32. ///
  33. /// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
  34. /// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
  35. /// their own metadata, you should avoid using key names which may clash with transport specific
  36. /// metadata. Note that transports may also impose limits in the amount of metadata which may
  37. /// be sent.
  38. public var metadata: Metadata
  39. /// The message to send to the server.
  40. public var message: Message
  41. /// Create a new single client request.
  42. ///
  43. /// - Parameters:
  44. /// - message: The message to send to the server.
  45. /// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
  46. public init(
  47. message: Message,
  48. metadata: Metadata = [:]
  49. ) {
  50. self.metadata = metadata
  51. self.message = message
  52. }
  53. }
  54. /// A request created by the client for a stream of messages.
  55. ///
  56. /// This is used for client-streaming and bidirectional-streaming RPCs.
  57. ///
  58. /// See ``ClientRequest`` for single-message requests and ``StreamingServerRequest`` for the
  59. /// servers representation of a streaming-message request.
  60. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  61. public struct StreamingClientRequest<Message: Sendable>: Sendable {
  62. /// Caller-specified metadata sent to the server at the start of the RPC.
  63. ///
  64. /// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
  65. /// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
  66. /// their own metadata, you should avoid using key names which may clash with transport specific
  67. /// metadata. Note that transports may also impose limits in the amount of metadata which may
  68. /// be sent.
  69. public var metadata: Metadata
  70. /// A closure which, when called, writes messages in the writer.
  71. ///
  72. /// The producer will only be consumed once by gRPC and therefore isn't required to be
  73. /// idempotent. If the producer throws an error then the RPC will be cancelled. Once the
  74. /// producer returns the request stream is closed.
  75. public var producer: @Sendable (RPCWriter<Message>) async throws -> Void
  76. /// Create a new streaming client request.
  77. ///
  78. /// - Parameters:
  79. /// - messageType: The type of message contained in this request, defaults to `Message.self`.
  80. /// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
  81. /// - producer: A closure which writes messages to send to the server. The closure is called
  82. /// at most once and may not be called.
  83. public init(
  84. of messageType: Message.Type = Message.self,
  85. metadata: Metadata = [:],
  86. producer: @escaping @Sendable (RPCWriter<Message>) async throws -> Void
  87. ) {
  88. self.metadata = metadata
  89. self.producer = producer
  90. }
  91. }