ClientRequest.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 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. public struct StreamingClientRequest<Message: Sendable>: Sendable {
  61. /// Caller-specified metadata sent to the server at the start of the RPC.
  62. ///
  63. /// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
  64. /// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
  65. /// their own metadata, you should avoid using key names which may clash with transport specific
  66. /// metadata. Note that transports may also impose limits in the amount of metadata which may
  67. /// be sent.
  68. public var metadata: Metadata
  69. /// A closure which, when called, writes messages in the writer.
  70. ///
  71. /// The producer will only be consumed once by gRPC and therefore isn't required to be
  72. /// idempotent. If the producer throws an error then the RPC will be cancelled. Once the
  73. /// producer returns the request stream is closed.
  74. public var producer: @Sendable (RPCWriter<Message>) async throws -> Void
  75. /// Create a new streaming client request.
  76. ///
  77. /// - Parameters:
  78. /// - messageType: The type of message contained in this request, defaults to `Message.self`.
  79. /// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
  80. /// - producer: A closure which writes messages to send to the server. The closure is called
  81. /// at most once and may not be called.
  82. public init(
  83. of messageType: Message.Type = Message.self,
  84. metadata: Metadata = [:],
  85. producer: @escaping @Sendable (RPCWriter<Message>) async throws -> Void
  86. ) {
  87. self.metadata = metadata
  88. self.producer = producer
  89. }
  90. }