echo.grpc.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /// - Returns: Interceptors to use when invoking 'get'.
  123. func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  124. /// - Returns: Interceptors to use when invoking 'expand'.
  125. func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  126. /// - Returns: Interceptors to use when invoking 'collect'.
  127. func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  128. /// - Returns: Interceptors to use when invoking 'update'.
  129. func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  130. }
  131. public final class Echo_EchoClient: Echo_EchoClientProtocol {
  132. public let channel: GRPCChannel
  133. public var defaultCallOptions: CallOptions
  134. public var interceptors: Echo_EchoClientInterceptorFactoryProtocol?
  135. /// Creates a client for the echo.Echo service.
  136. ///
  137. /// - Parameters:
  138. /// - channel: `GRPCChannel` to the service host.
  139. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  140. /// - interceptors: A factory providing interceptors for each RPC.
  141. public init(
  142. channel: GRPCChannel,
  143. defaultCallOptions: CallOptions = CallOptions(),
  144. interceptors: Echo_EchoClientInterceptorFactoryProtocol? = nil
  145. ) {
  146. self.channel = channel
  147. self.defaultCallOptions = defaultCallOptions
  148. self.interceptors = interceptors
  149. }
  150. }
  151. public final class Echo_EchoTestClient: Echo_EchoClientProtocol {
  152. private let fakeChannel: FakeChannel
  153. public var defaultCallOptions: CallOptions
  154. public var interceptors: Echo_EchoClientInterceptorFactoryProtocol?
  155. public var channel: GRPCChannel {
  156. return self.fakeChannel
  157. }
  158. public init(
  159. fakeChannel: FakeChannel = FakeChannel(),
  160. defaultCallOptions callOptions: CallOptions = CallOptions(),
  161. interceptors: Echo_EchoClientInterceptorFactoryProtocol? = nil
  162. ) {
  163. self.fakeChannel = fakeChannel
  164. self.defaultCallOptions = callOptions
  165. self.interceptors = interceptors
  166. }
  167. /// Make a unary response for the Get RPC. This must be called
  168. /// before calling 'get'. See also 'FakeUnaryResponse'.
  169. ///
  170. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  171. public func makeGetResponseStream(
  172. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  173. ) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
  174. return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Get", requestHandler: requestHandler)
  175. }
  176. public func enqueueGetResponse(
  177. _ response: Echo_EchoResponse,
  178. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  179. ) {
  180. let stream = self.makeGetResponseStream(requestHandler)
  181. // This is the only operation on the stream; try! is fine.
  182. try! stream.sendMessage(response)
  183. }
  184. /// Returns true if there are response streams enqueued for 'Get'
  185. public var hasGetResponsesRemaining: Bool {
  186. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Get")
  187. }
  188. /// Make a streaming response for the Expand RPC. This must be called
  189. /// before calling 'expand'. See also 'FakeStreamingResponse'.
  190. ///
  191. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  192. public func makeExpandResponseStream(
  193. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  194. ) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
  195. return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Expand", requestHandler: requestHandler)
  196. }
  197. public func enqueueExpandResponses(
  198. _ responses: [Echo_EchoResponse],
  199. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  200. ) {
  201. let stream = self.makeExpandResponseStream(requestHandler)
  202. // These are the only operation on the stream; try! is fine.
  203. responses.forEach { try! stream.sendMessage($0) }
  204. try! stream.sendEnd()
  205. }
  206. /// Returns true if there are response streams enqueued for 'Expand'
  207. public var hasExpandResponsesRemaining: Bool {
  208. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Expand")
  209. }
  210. /// Make a unary response for the Collect RPC. This must be called
  211. /// before calling 'collect'. See also 'FakeUnaryResponse'.
  212. ///
  213. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  214. public func makeCollectResponseStream(
  215. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  216. ) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
  217. return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Collect", requestHandler: requestHandler)
  218. }
  219. public func enqueueCollectResponse(
  220. _ response: Echo_EchoResponse,
  221. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  222. ) {
  223. let stream = self.makeCollectResponseStream(requestHandler)
  224. // This is the only operation on the stream; try! is fine.
  225. try! stream.sendMessage(response)
  226. }
  227. /// Returns true if there are response streams enqueued for 'Collect'
  228. public var hasCollectResponsesRemaining: Bool {
  229. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Collect")
  230. }
  231. /// Make a streaming response for the Update RPC. This must be called
  232. /// before calling 'update'. See also 'FakeStreamingResponse'.
  233. ///
  234. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  235. public func makeUpdateResponseStream(
  236. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  237. ) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
  238. return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Update", requestHandler: requestHandler)
  239. }
  240. public func enqueueUpdateResponses(
  241. _ responses: [Echo_EchoResponse],
  242. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  243. ) {
  244. let stream = self.makeUpdateResponseStream(requestHandler)
  245. // These are the only operation on the stream; try! is fine.
  246. responses.forEach { try! stream.sendMessage($0) }
  247. try! stream.sendEnd()
  248. }
  249. /// Returns true if there are response streams enqueued for 'Update'
  250. public var hasUpdateResponsesRemaining: Bool {
  251. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Update")
  252. }
  253. }
  254. /// To build a server, implement a class that conforms to this protocol.
  255. public protocol Echo_EchoProvider: CallHandlerProvider {
  256. var interceptors: Echo_EchoServerInterceptorFactoryProtocol? { get }
  257. /// Immediately returns an echo of a request.
  258. func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse>
  259. /// Splits a request into words and returns each word in a stream of messages.
  260. func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus>
  261. /// Collects a stream of messages and returns them concatenated when the caller closes.
  262. func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  263. /// Streams back messages as they are received in an input stream.
  264. func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  265. }
  266. extension Echo_EchoProvider {
  267. public var serviceName: Substring { return "echo.Echo" }
  268. /// Determines, calls and returns the appropriate request handler, depending on the request's method.
  269. /// Returns nil for methods not handled by this service.
  270. public func handleMethod(
  271. _ methodName: Substring,
  272. callHandlerContext: CallHandlerContext
  273. ) -> GRPCCallHandler? {
  274. switch methodName {
  275. case "Get":
  276. return CallHandlerFactory.makeUnary(
  277. callHandlerContext: callHandlerContext,
  278. interceptors: self.interceptors?.makeGetInterceptors() ?? []
  279. ) { context in
  280. return { request in
  281. self.get(request: request, context: context)
  282. }
  283. }
  284. case "Expand":
  285. return CallHandlerFactory.makeServerStreaming(
  286. callHandlerContext: callHandlerContext,
  287. interceptors: self.interceptors?.makeExpandInterceptors() ?? []
  288. ) { context in
  289. return { request in
  290. self.expand(request: request, context: context)
  291. }
  292. }
  293. case "Collect":
  294. return CallHandlerFactory.makeClientStreaming(
  295. callHandlerContext: callHandlerContext,
  296. interceptors: self.interceptors?.makeCollectInterceptors() ?? []
  297. ) { context in
  298. self.collect(context: context)
  299. }
  300. case "Update":
  301. return CallHandlerFactory.makeBidirectionalStreaming(
  302. callHandlerContext: callHandlerContext,
  303. interceptors: self.interceptors?.makeUpdateInterceptors() ?? []
  304. ) { context in
  305. self.update(context: context)
  306. }
  307. default:
  308. return nil
  309. }
  310. }
  311. }
  312. public protocol Echo_EchoServerInterceptorFactoryProtocol {
  313. /// - Returns: Interceptors to use when handling 'get'.
  314. /// Defaults to calling `self.makeInterceptors()`.
  315. func makeGetInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  316. /// - Returns: Interceptors to use when handling 'expand'.
  317. /// Defaults to calling `self.makeInterceptors()`.
  318. func makeExpandInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  319. /// - Returns: Interceptors to use when handling 'collect'.
  320. /// Defaults to calling `self.makeInterceptors()`.
  321. func makeCollectInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  322. /// - Returns: Interceptors to use when handling 'update'.
  323. /// Defaults to calling `self.makeInterceptors()`.
  324. func makeUpdateInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
  325. }