Skip to content

Commit 9f01202

Browse files
authored
Add client request and response objects (#1665)
1 parent 9af35a0 commit 9f01202

File tree

10 files changed

+996
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)