ClientRequest.swift 4.0 KB

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