echo.grpc.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. internal 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. internal 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. internal 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. internal 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. internal 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. internal 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. internal func makeInterceptors<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>() -> [ClientInterceptor<Request, Response>] {
  143. return []
  144. }
  145. internal func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  146. return self.makeInterceptors()
  147. }
  148. internal func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  149. return self.makeInterceptors()
  150. }
  151. internal func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  152. return self.makeInterceptors()
  153. }
  154. internal func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  155. return self.makeInterceptors()
  156. }
  157. }
  158. internal final class Echo_EchoClient: Echo_EchoClientProtocol {
  159. internal let channel: GRPCChannel
  160. internal var defaultCallOptions: CallOptions
  161. internal 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. internal 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. /// To build a server, implement a class that conforms to this protocol.
  179. internal protocol Echo_EchoProvider: CallHandlerProvider {
  180. /// Immediately returns an echo of a request.
  181. func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse>
  182. /// Splits a request into words and returns each word in a stream of messages.
  183. func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus>
  184. /// Collects a stream of messages and returns them concatenated when the caller closes.
  185. func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  186. /// Streams back messages as they are received in an input stream.
  187. func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void>
  188. }
  189. extension Echo_EchoProvider {
  190. internal var serviceName: Substring { return "echo.Echo" }
  191. /// Determines, calls and returns the appropriate request handler, depending on the request's method.
  192. /// Returns nil for methods not handled by this service.
  193. internal func handleMethod(_ methodName: Substring, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? {
  194. switch methodName {
  195. case "Get":
  196. return CallHandlerFactory.makeUnary(callHandlerContext: callHandlerContext) { context in
  197. return { request in
  198. self.get(request: request, context: context)
  199. }
  200. }
  201. case "Expand":
  202. return CallHandlerFactory.makeServerStreaming(callHandlerContext: callHandlerContext) { context in
  203. return { request in
  204. self.expand(request: request, context: context)
  205. }
  206. }
  207. case "Collect":
  208. return CallHandlerFactory.makeClientStreaming(callHandlerContext: callHandlerContext) { context in
  209. return self.collect(context: context)
  210. }
  211. case "Update":
  212. return CallHandlerFactory.makeBidirectionalStreaming(callHandlerContext: callHandlerContext) { context in
  213. return self.update(context: context)
  214. }
  215. default: return nil
  216. }
  217. }
  218. }