Skip to content

Commit 3cd505a

Browse files
xissyrebello95
authored andcommitted
Allow client to specify metadata per call (#356)
To address grpc/grpc-swift#355 (comment). It also closes grpc/grpc-swift#190.
1 parent 96bf49e commit 3cd505a

File tree

2 files changed

+122
-40
lines changed

2 files changed

+122
-40
lines changed

Sources/Examples/Echo/Generated/echo.grpc.swift

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,93 +117,120 @@ class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo
117117
/// Instantiate Echo_EchoServiceClient, then call methods of this protocol to make API calls.
118118
internal protocol Echo_EchoService: ServiceClient {
119119
/// Synchronous. Unary.
120-
func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
120+
func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata) throws -> Echo_EchoResponse
121121
/// Asynchronous. Unary.
122-
func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall
122+
func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall
123123

124124
/// Asynchronous. Server-streaming.
125125
/// Send the initial message.
126126
/// Use methods on the returned object to get streamed responses.
127-
func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall
127+
func expand(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall
128128

129129
/// Asynchronous. Client-streaming.
130130
/// Use methods on the returned object to stream messages and
131131
/// to close the connection and wait for a final response.
132-
func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall
132+
func collect(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall
133133

134134
/// Asynchronous. Bidirectional-streaming.
135135
/// Use methods on the returned object to stream messages,
136136
/// to wait for replies, and to close the connection.
137-
func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall
137+
func update(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall
138+
139+
}
140+
141+
internal extension Echo_EchoService {
142+
/// Synchronous. Unary.
143+
func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
144+
return try self.get(request, metadata: self.metadata)
145+
}
146+
/// Asynchronous. Unary.
147+
func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
148+
return try self.get(request, metadata: self.metadata, completion: completion)
149+
}
150+
151+
/// Asynchronous. Server-streaming.
152+
func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
153+
return try self.expand(request, metadata: self.metadata, completion: completion)
154+
}
155+
156+
/// Asynchronous. Client-streaming.
157+
func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
158+
return try self.collect(metadata: self.metadata, completion: completion)
159+
}
160+
161+
/// Asynchronous. Bidirectional-streaming.
162+
func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
163+
return try self.update(metadata: self.metadata, completion: completion)
164+
}
138165

139166
}
140167

141168
internal final class Echo_EchoServiceClient: ServiceClientBase, Echo_EchoService {
142169
/// Synchronous. Unary.
143-
internal func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
170+
internal func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata) throws -> Echo_EchoResponse {
144171
return try Echo_EchoGetCallBase(channel)
145-
.run(request: request, metadata: metadata)
172+
.run(request: request, metadata: customMetadata)
146173
}
147174
/// Asynchronous. Unary.
148-
internal func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
175+
internal func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
149176
return try Echo_EchoGetCallBase(channel)
150-
.start(request: request, metadata: metadata, completion: completion)
177+
.start(request: request, metadata: customMetadata, completion: completion)
151178
}
152179

153180
/// Asynchronous. Server-streaming.
154181
/// Send the initial message.
155182
/// Use methods on the returned object to get streamed responses.
156-
internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
183+
internal func expand(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
157184
return try Echo_EchoExpandCallBase(channel)
158-
.start(request: request, metadata: metadata, completion: completion)
185+
.start(request: request, metadata: customMetadata, completion: completion)
159186
}
160187

161188
/// Asynchronous. Client-streaming.
162189
/// Use methods on the returned object to stream messages and
163190
/// to close the connection and wait for a final response.
164-
internal func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
191+
internal func collect(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
165192
return try Echo_EchoCollectCallBase(channel)
166-
.start(metadata: metadata, completion: completion)
193+
.start(metadata: customMetadata, completion: completion)
167194
}
168195

169196
/// Asynchronous. Bidirectional-streaming.
170197
/// Use methods on the returned object to stream messages,
171198
/// to wait for replies, and to close the connection.
172-
internal func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
199+
internal func update(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
173200
return try Echo_EchoUpdateCallBase(channel)
174-
.start(metadata: metadata, completion: completion)
201+
.start(metadata: customMetadata, completion: completion)
175202
}
176203

177204
}
178205

179206
class Echo_EchoServiceTestStub: ServiceClientTestStubBase, Echo_EchoService {
180207
var getRequests: [Echo_EchoRequest] = []
181208
var getResponses: [Echo_EchoResponse] = []
182-
func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
209+
func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata) throws -> Echo_EchoResponse {
183210
getRequests.append(request)
184211
defer { getResponses.removeFirst() }
185212
return getResponses.first!
186213
}
187-
func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
214+
func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
188215
fatalError("not implemented")
189216
}
190217

191218
var expandRequests: [Echo_EchoRequest] = []
192219
var expandCalls: [Echo_EchoExpandCall] = []
193-
func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
220+
func expand(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
194221
expandRequests.append(request)
195222
defer { expandCalls.removeFirst() }
196223
return expandCalls.first!
197224
}
198225

199226
var collectCalls: [Echo_EchoCollectCall] = []
200-
func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
227+
func collect(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
201228
defer { collectCalls.removeFirst() }
202229
return collectCalls.first!
203230
}
204231

205232
var updateCalls: [Echo_EchoUpdateCall] = []
206-
func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
233+
func update(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
207234
defer { updateCalls.removeFirst() }
208235
return updateCalls.first!
209236
}

Sources/protoc-gen-swiftgrpc/Generator-Client.swift

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extension Generator {
3737
printServiceClientProtocol(asynchronousCode: asynchronousCode,
3838
synchronousCode: synchronousCode)
3939
println()
40+
printServiceClientProtocolExtension(asynchronousCode: asynchronousCode,
41+
synchronousCode: synchronousCode)
42+
println()
4043
printServiceClientImplementation(asynchronousCode: asynchronousCode,
4144
synchronousCode: synchronousCode)
4245
if options.generateTestStubs {
@@ -158,27 +161,79 @@ extension Generator {
158161
case .unary:
159162
if synchronousCode {
160163
println("/// Synchronous. Unary.")
161-
println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName)")
164+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName)")
162165
}
163166
if asynchronousCode {
164167
println("/// Asynchronous. Unary.")
165-
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
168+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
166169
}
167170
case .serverStreaming:
168171
println("/// Asynchronous. Server-streaming.")
169172
println("/// Send the initial message.")
170173
println("/// Use methods on the returned object to get streamed responses.")
171-
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName)")
174+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
172175
case .clientStreaming:
173176
println("/// Asynchronous. Client-streaming.")
174177
println("/// Use methods on the returned object to stream messages and")
175178
println("/// to close the connection and wait for a final response.")
176-
println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName)")
179+
println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
177180
case .bidirectionalStreaming:
178181
println("/// Asynchronous. Bidirectional-streaming.")
179182
println("/// Use methods on the returned object to stream messages,")
180183
println("/// to wait for replies, and to close the connection.")
181-
println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName)")
184+
println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
185+
}
186+
println()
187+
}
188+
outdent()
189+
println("}")
190+
}
191+
192+
private func printServiceClientProtocolExtension(asynchronousCode: Bool,
193+
synchronousCode: Bool) {
194+
println("\(options.visibility.sourceSnippet) extension \(serviceClassName) {")
195+
indent()
196+
for method in service.methods {
197+
self.method = method
198+
switch streamingType(method) {
199+
case .unary:
200+
if synchronousCode {
201+
println("/// Synchronous. Unary.")
202+
println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
203+
indent()
204+
println("return try self.\(methodFunctionName)(request, metadata: self.metadata)")
205+
outdent()
206+
println("}")
207+
}
208+
if asynchronousCode {
209+
println("/// Asynchronous. Unary.")
210+
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
211+
indent()
212+
println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
213+
outdent()
214+
println("}")
215+
}
216+
case .serverStreaming:
217+
println("/// Asynchronous. Server-streaming.")
218+
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName) {")
219+
indent()
220+
println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
221+
outdent()
222+
println("}")
223+
case .clientStreaming:
224+
println("/// Asynchronous. Client-streaming.")
225+
println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
226+
indent()
227+
println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
228+
outdent()
229+
println("}")
230+
case .bidirectionalStreaming:
231+
println("/// Asynchronous. Bidirectional-streaming.")
232+
println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
233+
indent()
234+
println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
235+
outdent()
236+
println("}")
182237
}
183238
println()
184239
}
@@ -196,22 +251,22 @@ extension Generator {
196251
case .unary:
197252
if synchronousCode {
198253
println("/// Synchronous. Unary.")
199-
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
254+
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
200255
indent()
201256
println("return try \(callName)Base(channel)")
202257
indent()
203-
println(".run(request: request, metadata: metadata)")
258+
println(".run(request: request, metadata: customMetadata)")
204259
outdent()
205260
outdent()
206261
println("}")
207262
}
208263
if asynchronousCode {
209264
println("/// Asynchronous. Unary.")
210-
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
265+
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
211266
indent()
212267
println("return try \(callName)Base(channel)")
213268
indent()
214-
println(".start(request: request, metadata: metadata, completion: completion)")
269+
println(".start(request: request, metadata: customMetadata, completion: completion)")
215270
outdent()
216271
outdent()
217272
println("}")
@@ -220,35 +275,35 @@ extension Generator {
220275
println("/// Asynchronous. Server-streaming.")
221276
println("/// Send the initial message.")
222277
println("/// Use methods on the returned object to get streamed responses.")
223-
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName) {")
278+
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
224279
indent()
225280
println("return try \(callName)Base(channel)")
226281
indent()
227-
println(".start(request: request, metadata: metadata, completion: completion)")
282+
println(".start(request: request, metadata: customMetadata, completion: completion)")
228283
outdent()
229284
outdent()
230285
println("}")
231286
case .clientStreaming:
232287
println("/// Asynchronous. Client-streaming.")
233288
println("/// Use methods on the returned object to stream messages and")
234289
println("/// to close the connection and wait for a final response.")
235-
println("\(access) func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
290+
println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
236291
indent()
237292
println("return try \(callName)Base(channel)")
238293
indent()
239-
println(".start(metadata: metadata, completion: completion)")
294+
println(".start(metadata: customMetadata, completion: completion)")
240295
outdent()
241296
outdent()
242297
println("}")
243298
case .bidirectionalStreaming:
244299
println("/// Asynchronous. Bidirectional-streaming.")
245300
println("/// Use methods on the returned object to stream messages,")
246301
println("/// to wait for replies, and to close the connection.")
247-
println("\(access) func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
302+
println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
248303
indent()
249304
println("return try \(callName)Base(channel)")
250305
indent()
251-
println(".start(metadata: metadata, completion: completion)")
306+
println(".start(metadata: customMetadata, completion: completion)")
252307
outdent()
253308
outdent()
254309
println("}")
@@ -268,22 +323,22 @@ extension Generator {
268323
case .unary:
269324
println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
270325
println("var \(methodFunctionName)Responses: [\(methodOutputName)] = []")
271-
println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
326+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
272327
indent()
273328
println("\(methodFunctionName)Requests.append(request)")
274329
println("defer { \(methodFunctionName)Responses.removeFirst() }")
275330
println("return \(methodFunctionName)Responses.first!")
276331
outdent()
277332
println("}")
278-
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
333+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
279334
indent()
280335
println("fatalError(\"not implemented\")")
281336
outdent()
282337
println("}")
283338
case .serverStreaming:
284339
println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
285340
println("var \(methodFunctionName)Calls: [\(callName)] = []")
286-
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName) {")
341+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
287342
indent()
288343
println("\(methodFunctionName)Requests.append(request)")
289344
println("defer { \(methodFunctionName)Calls.removeFirst() }")
@@ -292,15 +347,15 @@ extension Generator {
292347
println("}")
293348
case .clientStreaming:
294349
println("var \(methodFunctionName)Calls: [\(callName)] = []")
295-
println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
350+
println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
296351
indent()
297352
println("defer { \(methodFunctionName)Calls.removeFirst() }")
298353
println("return \(methodFunctionName)Calls.first!")
299354
outdent()
300355
println("}")
301356
case .bidirectionalStreaming:
302357
println("var \(methodFunctionName)Calls: [\(callName)] = []")
303-
println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
358+
println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
304359
indent()
305360
println("defer { \(methodFunctionName)Calls.removeFirst() }")
306361
println("return \(methodFunctionName)Calls.first!")

0 commit comments

Comments
 (0)