echo.grpc.swift 12 KB

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