echo.grpc.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /// Usage: instantiate Echo_EchoClient, then call methods of this protocol to make API calls.
  25. public protocol Echo_EchoClientProtocol: GRPCClient {
  26. func get(
  27. _ request: Echo_EchoRequest,
  28. callOptions: CallOptions?
  29. ) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse>
  30. func expand(
  31. _ request: Echo_EchoRequest,
  32. callOptions: CallOptions?,
  33. handler: @escaping (Echo_EchoResponse) -> Void
  34. ) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse>
  35. func collect(
  36. callOptions: CallOptions?
  37. ) -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse>
  38. func update(
  39. callOptions: CallOptions?,
  40. handler: @escaping (Echo_EchoResponse) -> Void
  41. ) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse>
  42. }
  43. extension Echo_EchoClientProtocol {
  44. /// Immediately returns an echo of a request.
  45. ///
  46. /// - Parameters:
  47. /// - request: Request to send to Get.
  48. /// - callOptions: Call options.
  49. /// - Returns: A `UnaryCall` with futures for the metadata, status and response.
  50. public func get(
  51. _ request: Echo_EchoRequest,
  52. callOptions: CallOptions? = nil
  53. ) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse> {
  54. return self.makeUnaryCall(
  55. path: "/echo.Echo/Get",
  56. request: request,
  57. callOptions: callOptions ?? self.defaultCallOptions
  58. )
  59. }
  60. /// Splits a request into words and returns each word in a stream of messages.
  61. ///
  62. /// - Parameters:
  63. /// - request: Request to send to Expand.
  64. /// - callOptions: Call options.
  65. /// - handler: A closure called when each response is received from the server.
  66. /// - Returns: A `ServerStreamingCall` with futures for the metadata and status.
  67. public func expand(
  68. _ request: Echo_EchoRequest,
  69. callOptions: CallOptions? = nil,
  70. handler: @escaping (Echo_EchoResponse) -> Void
  71. ) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
  72. return self.makeServerStreamingCall(
  73. path: "/echo.Echo/Expand",
  74. request: request,
  75. callOptions: callOptions ?? self.defaultCallOptions,
  76. handler: handler
  77. )
  78. }
  79. /// Collects a stream of messages and returns them concatenated when the caller closes.
  80. ///
  81. /// Callers should use the `send` method on the returned object to send messages
  82. /// to the server. The caller should send an `.end` after the final message has been sent.
  83. ///
  84. /// - Parameters:
  85. /// - callOptions: Call options.
  86. /// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.
  87. public func collect(
  88. callOptions: CallOptions? = nil
  89. ) -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
  90. return self.makeClientStreamingCall(
  91. path: "/echo.Echo/Collect",
  92. callOptions: callOptions ?? self.defaultCallOptions
  93. )
  94. }
  95. /// Streams back messages as they are received in an input stream.
  96. ///
  97. /// Callers should use the `send` method on the returned object to send messages
  98. /// to the server. The caller should send an `.end` after the final message has been sent.
  99. ///
  100. /// - Parameters:
  101. /// - callOptions: Call options.
  102. /// - handler: A closure called when each response is received from the server.
  103. /// - Returns: A `ClientStreamingCall` with futures for the metadata and status.
  104. public func update(
  105. callOptions: CallOptions? = nil,
  106. handler: @escaping (Echo_EchoResponse) -> Void
  107. ) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
  108. return self.makeBidirectionalStreamingCall(
  109. path: "/echo.Echo/Update",
  110. callOptions: callOptions ?? self.defaultCallOptions,
  111. handler: handler
  112. )
  113. }
  114. }
  115. public final class Echo_EchoClient: Echo_EchoClientProtocol {
  116. public let channel: GRPCChannel
  117. public var defaultCallOptions: CallOptions
  118. /// Creates a client for the echo.Echo service.
  119. ///
  120. /// - Parameters:
  121. /// - channel: `GRPCChannel` to the service host.
  122. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  123. public init(channel: GRPCChannel, defaultCallOptions: CallOptions = CallOptions()) {
  124. self.channel = channel
  125. self.defaultCallOptions = defaultCallOptions
  126. }
  127. }
  128. public final class Echo_EchoTestClient: Echo_EchoClientProtocol {
  129. private let fakeChannel: FakeChannel
  130. public var defaultCallOptions: CallOptions
  131. public var channel: GRPCChannel {
  132. return self.fakeChannel
  133. }
  134. public init(
  135. fakeChannel: FakeChannel = FakeChannel(),
  136. defaultCallOptions callOptions: CallOptions = CallOptions()
  137. ) {
  138. self.fakeChannel = fakeChannel
  139. self.defaultCallOptions = callOptions
  140. }
  141. /// Make a unary response for the Get RPC. This must be called
  142. /// before calling 'get'. See also 'FakeUnaryResponse'.
  143. ///
  144. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  145. public func makeGetResponseStream(
  146. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  147. ) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
  148. return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Get", requestHandler: requestHandler)
  149. }
  150. public func enqueueGetResponse(
  151. _ response: Echo_EchoResponse,
  152. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  153. ) {
  154. let stream = self.makeGetResponseStream(requestHandler)
  155. // This is the only operation on the stream; try! is fine.
  156. try! stream.sendMessage(response)
  157. }
  158. /// Returns true if there are response streams enqueued for 'Get'
  159. public var hasGetResponsesRemaining: Bool {
  160. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Get")
  161. }
  162. /// Make a streaming response for the Expand RPC. This must be called
  163. /// before calling 'expand'. See also 'FakeStreamingResponse'.
  164. ///
  165. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  166. public func makeExpandResponseStream(
  167. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  168. ) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
  169. return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Expand", requestHandler: requestHandler)
  170. }
  171. public func enqueueExpandResponses(
  172. _ responses: [Echo_EchoResponse],
  173. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  174. ) {
  175. let stream = self.makeExpandResponseStream(requestHandler)
  176. // These are the only operation on the stream; try! is fine.
  177. responses.forEach { try! stream.sendMessage($0) }
  178. try! stream.sendEnd()
  179. }
  180. /// Returns true if there are response streams enqueued for 'Expand'
  181. public var hasExpandResponsesRemaining: Bool {
  182. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Expand")
  183. }
  184. /// Make a unary response for the Collect RPC. This must be called
  185. /// before calling 'collect'. See also 'FakeUnaryResponse'.
  186. ///
  187. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  188. public func makeCollectResponseStream(
  189. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  190. ) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
  191. return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Collect", requestHandler: requestHandler)
  192. }
  193. public func enqueueCollectResponse(
  194. _ response: Echo_EchoResponse,
  195. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  196. ) {
  197. let stream = self.makeCollectResponseStream(requestHandler)
  198. // This is the only operation on the stream; try! is fine.
  199. try! stream.sendMessage(response)
  200. }
  201. /// Returns true if there are response streams enqueued for 'Collect'
  202. public var hasCollectResponsesRemaining: Bool {
  203. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Collect")
  204. }
  205. /// Make a streaming response for the Update RPC. This must be called
  206. /// before calling 'update'. See also 'FakeStreamingResponse'.
  207. ///
  208. /// - Parameter requestHandler: a handler for request parts sent by the RPC.
  209. public func makeUpdateResponseStream(
  210. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  211. ) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
  212. return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Update", requestHandler: requestHandler)
  213. }
  214. public func enqueueUpdateResponses(
  215. _ responses: [Echo_EchoResponse],
  216. _ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
  217. ) {
  218. let stream = self.makeUpdateResponseStream(requestHandler)
  219. // These are the only operation on the stream; try! is fine.
  220. responses.forEach { try! stream.sendMessage($0) }
  221. try! stream.sendEnd()
  222. }
  223. /// Returns true if there are response streams enqueued for 'Update'
  224. public var hasUpdateResponsesRemaining: Bool {
  225. return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Update")
  226. }
  227. }
  228. /// To build a server, implement a class that conforms to this protocol.
  229. public protocol Echo_EchoProvider: CallHandlerProvider {
  230. /// Immediately returns an echo of a request.
  231. func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse>
  232. /// Splits a request into words and returns each word in a stream of messages.
  233. func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus>
  234. /// Collects a stream of messages and returns them concatenated when the caller closes.
  235. func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  236. /// Streams back messages as they are received in an input stream.
  237. func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  238. }
  239. extension Echo_EchoProvider {
  240. public var serviceName: Substring { return "echo.Echo" }
  241. /// Determines, calls and returns the appropriate request handler, depending on the request's method.
  242. /// Returns nil for methods not handled by this service.
  243. public func handleMethod(_ methodName: Substring, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? {
  244. switch methodName {
  245. case "Get":
  246. return CallHandlerFactory.makeUnary(callHandlerContext: callHandlerContext) { context in
  247. return { request in
  248. self.get(request: request, context: context)
  249. }
  250. }
  251. case "Expand":
  252. return CallHandlerFactory.makeServerStreaming(callHandlerContext: callHandlerContext) { context in
  253. return { request in
  254. self.expand(request: request, context: context)
  255. }
  256. }
  257. case "Collect":
  258. return CallHandlerFactory.makeClientStreaming(callHandlerContext: callHandlerContext) { context in
  259. return self.collect(context: context)
  260. }
  261. case "Update":
  262. return CallHandlerFactory.makeBidirectionalStreaming(callHandlerContext: callHandlerContext) { context in
  263. return self.update(context: context)
  264. }
  265. default: return nil
  266. }
  267. }
  268. }