echo.grpc.swift 12 KB

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