echo.grpc.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // DO NOT EDIT.
  3. //
  4. // Generated by the protocol buffer compiler.
  5. // Source: echo.proto
  6. //
  7. //
  8. // Copyright 2018, gRPC Authors All rights reserved.
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. //
  22. import GRPC
  23. import NIO
  24. import SwiftProtobuf
  25. /// Usage: instantiate Echo_EchoClient, then call methods of this protocol to make API calls.
  26. public protocol Echo_EchoClientProtocol: GRPCClient {
  27. var interceptors: Echo_EchoClientInterceptorFactoryProtocol? { get }
  28. func get(
  29. _ request: Echo_EchoRequest,
  30. callOptions: CallOptions?
  31. ) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse>
  32. func expand(
  33. _ request: Echo_EchoRequest,
  34. callOptions: CallOptions?,
  35. handler: @escaping (Echo_EchoResponse) -> Void
  36. ) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse>
  37. func collect(
  38. callOptions: CallOptions?
  39. ) -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse>
  40. func update(
  41. callOptions: CallOptions?,
  42. handler: @escaping (Echo_EchoResponse) -> Void
  43. ) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse>
  44. }
  45. extension Echo_EchoClientProtocol {
  46. /// Immediately returns an echo of a request.
  47. ///
  48. /// - Parameters:
  49. /// - request: Request to send to Get.
  50. /// - callOptions: Call options.
  51. /// - Returns: A `UnaryCall` with futures for the metadata, status and response.
  52. public func get(
  53. _ request: Echo_EchoRequest,
  54. callOptions: CallOptions? = nil
  55. ) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse> {
  56. return self.makeUnaryCall(
  57. path: "/echo.Echo/Get",
  58. request: request,
  59. callOptions: callOptions ?? self.defaultCallOptions,
  60. interceptors: self.interceptors?.makeGetInterceptors() ?? []
  61. )
  62. }
  63. /// Splits a request into words and returns each word in a stream of messages.
  64. ///
  65. /// - Parameters:
  66. /// - request: Request to send to Expand.
  67. /// - callOptions: Call options.
  68. /// - handler: A closure called when each response is received from the server.
  69. /// - Returns: A `ServerStreamingCall` with futures for the metadata and status.
  70. public func expand(
  71. _ request: Echo_EchoRequest,
  72. callOptions: CallOptions? = nil,
  73. handler: @escaping (Echo_EchoResponse) -> Void
  74. ) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
  75. return self.makeServerStreamingCall(
  76. path: "/echo.Echo/Expand",
  77. request: request,
  78. callOptions: callOptions ?? self.defaultCallOptions,
  79. interceptors: self.interceptors?.makeExpandInterceptors() ?? [],
  80. handler: handler
  81. )
  82. }
  83. /// Collects a stream of messages and returns them concatenated when the caller closes.
  84. ///
  85. /// Callers should use the `send` method on the returned object to send messages
  86. /// to the server. The caller should send an `.end` after the final message has been sent.
  87. ///
  88. /// - Parameters:
  89. /// - callOptions: Call options.
  90. /// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.
  91. public func collect(
  92. callOptions: CallOptions? = nil
  93. ) -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
  94. return self.makeClientStreamingCall(
  95. path: "/echo.Echo/Collect",
  96. callOptions: callOptions ?? self.defaultCallOptions,
  97. interceptors: self.interceptors?.makeCollectInterceptors() ?? []
  98. )
  99. }
  100. /// Streams back messages as they are received in an input stream.
  101. ///
  102. /// Callers should use the `send` method on the returned object to send messages
  103. /// to the server. The caller should send an `.end` after the final message has been sent.
  104. ///
  105. /// - Parameters:
  106. /// - callOptions: Call options.
  107. /// - handler: A closure called when each response is received from the server.
  108. /// - Returns: A `ClientStreamingCall` with futures for the metadata and status.
  109. public func update(
  110. callOptions: CallOptions? = nil,
  111. handler: @escaping (Echo_EchoResponse) -> Void
  112. ) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
  113. return self.makeBidirectionalStreamingCall(
  114. path: "/echo.Echo/Update",
  115. callOptions: callOptions ?? self.defaultCallOptions,
  116. interceptors: self.interceptors?.makeUpdateInterceptors() ?? [],
  117. handler: handler
  118. )
  119. }
  120. }
  121. public protocol Echo_EchoClientInterceptorFactoryProtocol {
  122. /// Makes an array of generic interceptors. The per-method interceptor
  123. /// factories default to calling this function and it therefore provides a
  124. /// convenient way of setting interceptors for all methods on a client.
  125. /// - Returns: An array of interceptors generic over `Request` and `Response`.
  126. /// Defaults to an empty array.
  127. func makeInterceptors<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>() -> [ClientInterceptor<Request, Response>]
  128. /// - Returns: Interceptors to use when invoking 'get'.
  129. /// Defaults to calling `self.makeInterceptors()`.
  130. func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  131. /// - Returns: Interceptors to use when invoking 'expand'.
  132. /// Defaults to calling `self.makeInterceptors()`.
  133. func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  134. /// - Returns: Interceptors to use when invoking 'collect'.
  135. /// Defaults to calling `self.makeInterceptors()`.
  136. func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  137. /// - Returns: Interceptors to use when invoking 'update'.
  138. /// Defaults to calling `self.makeInterceptors()`.
  139. func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  140. }
  141. extension Echo_EchoClientInterceptorFactoryProtocol {
  142. public func makeInterceptors<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>() -> [ClientInterceptor<Request, Response>] {
  143. return []
  144. }
  145. public func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  146. return self.makeInterceptors()
  147. }
  148. public func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  149. return self.makeInterceptors()
  150. }
  151. public func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  152. return self.makeInterceptors()
  153. }
  154. public func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  155. return self.makeInterceptors()
  156. }
  157. }
  158. public final class Echo_EchoClient: Echo_EchoClientProtocol {
  159. public let channel: GRPCChannel
  160. public var defaultCallOptions: CallOptions
  161. public var interceptors: Echo_EchoClientInterceptorFactoryProtocol?
  162. /// Creates a client for the echo.Echo service.
  163. ///
  164. /// - Parameters:
  165. /// - channel: `GRPCChannel` to the service host.
  166. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  167. /// - interceptors: A factory providing interceptors for each RPC.
  168. public init(
  169. channel: GRPCChannel,
  170. defaultCallOptions: CallOptions = CallOptions(),
  171. interceptors: Echo_EchoClientInterceptorFactoryProtocol? = nil
  172. ) {
  173. self.channel = channel
  174. self.defaultCallOptions = defaultCallOptions
  175. self.interceptors = interceptors
  176. }
  177. }
  178. public final class Echo_EchoTestClient: Echo_EchoClientProtocol {
  179. private let fakeChannel: FakeChannel
  180. public var defaultCallOptions: CallOptions
  181. public var interceptors: Echo_EchoClientInterceptorFactoryProtocol?
  182. public var channel: GRPCChannel {
  183. return self.fakeChannel
  184. }
  185. public init(
  186. fakeChannel: FakeChannel = FakeChannel(),
  187. defaultCallOptions callOptions: CallOptions = CallOptions(),
  188. interceptors: Echo_EchoClientInterceptorFactoryProtocol? = nil
  189. ) {
  190. self.fakeChannel = fakeChannel
  191. self.defaultCallOptions = callOptions
  192. self.interceptors = interceptors
  193. }
  194. /// Make a unary response for the Get RPC. This must be called
  195. /// before calling 'get'. See also 'FakeUnaryResponse'.
  196. ///
  197. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  198. public func makeGetResponseStream(
  199. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  200. ) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
  201. return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Get", requestHandler: requestHandler)
  202. }
  203. public func enqueueGetResponse(
  204. _ response: Echo_EchoResponse,
  205. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  206. ) {
  207. let stream = self.makeGetResponseStream(requestHandler)
  208. // This is the only operation on the stream; try! is fine.
  209. try! stream.sendMessage(response)
  210. }
  211. /// Returns true if there are response streams enqueued for 'Get'
  212. public var hasGetResponsesRemaining: Bool {
  213. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Get")
  214. }
  215. /// Make a streaming response for the Expand RPC. This must be called
  216. /// before calling 'expand'. See also 'FakeStreamingResponse'.
  217. ///
  218. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  219. public func makeExpandResponseStream(
  220. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  221. ) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
  222. return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Expand", requestHandler: requestHandler)
  223. }
  224. public func enqueueExpandResponses(
  225. _ responses: [Echo_EchoResponse],
  226. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  227. ) {
  228. let stream = self.makeExpandResponseStream(requestHandler)
  229. // These are the only operation on the stream; try! is fine.
  230. responses.forEach { try! stream.sendMessage($0) }
  231. try! stream.sendEnd()
  232. }
  233. /// Returns true if there are response streams enqueued for 'Expand'
  234. public var hasExpandResponsesRemaining: Bool {
  235. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Expand")
  236. }
  237. /// Make a unary response for the Collect RPC. This must be called
  238. /// before calling 'collect'. See also 'FakeUnaryResponse'.
  239. ///
  240. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  241. public func makeCollectResponseStream(
  242. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  243. ) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
  244. return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Collect", requestHandler: requestHandler)
  245. }
  246. public func enqueueCollectResponse(
  247. _ response: Echo_EchoResponse,
  248. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  249. ) {
  250. let stream = self.makeCollectResponseStream(requestHandler)
  251. // This is the only operation on the stream; try! is fine.
  252. try! stream.sendMessage(response)
  253. }
  254. /// Returns true if there are response streams enqueued for 'Collect'
  255. public var hasCollectResponsesRemaining: Bool {
  256. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Collect")
  257. }
  258. /// Make a streaming response for the Update RPC. This must be called
  259. /// before calling 'update'. See also 'FakeStreamingResponse'.
  260. ///
  261. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  262. public func makeUpdateResponseStream(
  263. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  264. ) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
  265. return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Update", requestHandler: requestHandler)
  266. }
  267. public func enqueueUpdateResponses(
  268. _ responses: [Echo_EchoResponse],
  269. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  270. ) {
  271. let stream = self.makeUpdateResponseStream(requestHandler)
  272. // These are the only operation on the stream; try! is fine.
  273. responses.forEach { try! stream.sendMessage($0) }
  274. try! stream.sendEnd()
  275. }
  276. /// Returns true if there are response streams enqueued for 'Update'
  277. public var hasUpdateResponsesRemaining: Bool {
  278. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Update")
  279. }
  280. }
  281. /// To build a server, implement a class that conforms to this protocol.
  282. public protocol Echo_EchoProvider: CallHandlerProvider {
  283. /// Immediately returns an echo of a request.
  284. func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse>
  285. /// Splits a request into words and returns each word in a stream of messages.
  286. func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus>
  287. /// Collects a stream of messages and returns them concatenated when the caller closes.
  288. func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  289. /// Streams back messages as they are received in an input stream.
  290. func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  291. }
  292. extension Echo_EchoProvider {
  293. public var serviceName: Substring { return "echo.Echo" }
  294. /// Determines, calls and returns the appropriate request handler, depending on the request's method.
  295. /// Returns nil for methods not handled by this service.
  296. public func handleMethod(_ methodName: Substring, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? {
  297. switch methodName {
  298. case "Get":
  299. return CallHandlerFactory.makeUnary(callHandlerContext: callHandlerContext) { context in
  300. return { request in
  301. self.get(request: request, context: context)
  302. }
  303. }
  304. case "Expand":
  305. return CallHandlerFactory.makeServerStreaming(callHandlerContext: callHandlerContext) { context in
  306. return { request in
  307. self.expand(request: request, context: context)
  308. }
  309. }
  310. case "Collect":
  311. return CallHandlerFactory.makeClientStreaming(callHandlerContext: callHandlerContext) { context in
  312. return self.collect(context: context)
  313. }
  314. case "Update":
  315. return CallHandlerFactory.makeBidirectionalStreaming(callHandlerContext: callHandlerContext) { context in
  316. return self.update(context: context)
  317. }
  318. default: return nil
  319. }
  320. }
  321. }