GRPCChannel.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import NIOCore
  17. import NIOHTTP2
  18. import SwiftProtobuf
  19. public protocol GRPCChannel {
  20. /// Makes a gRPC call on the channel with requests and responses conforming to
  21. /// `SwiftProtobuf.Message`.
  22. ///
  23. /// Note: this is a lower-level construct that any of `UnaryCall`, `ClientStreamingCall`,
  24. /// `ServerStreamingCall` or `BidirectionalStreamingCall` and does not have an API to protect
  25. /// users against protocol violations (such as sending to requests on a unary call).
  26. ///
  27. /// After making the `Call`, users must `invoke` the call with a callback which is invoked
  28. /// for each response part (or error) received. Any call to `send(_:promise:)` prior to calling
  29. /// `invoke` will fail and not be sent. Users are also responsible for closing the request stream
  30. /// by sending the `.end` request part.
  31. ///
  32. /// - Parameters:
  33. /// - path: The path of the RPC, e.g. "/echo.Echo/get".
  34. /// - type: The type of the RPC, e.g. `.unary`.
  35. /// - callOptions: Options for the RPC.
  36. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  37. func makeCall<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>(
  38. path: String,
  39. type: GRPCCallType,
  40. callOptions: CallOptions,
  41. interceptors: [ClientInterceptor<Request, Response>]
  42. ) -> Call<Request, Response>
  43. /// Makes a gRPC call on the channel with requests and responses conforming to `GRPCPayload`.
  44. ///
  45. /// Note: this is a lower-level construct that any of `UnaryCall`, `ClientStreamingCall`,
  46. /// `ServerStreamingCall` or `BidirectionalStreamingCall` and does not have an API to protect
  47. /// users against protocol violations (such as sending to requests on a unary call).
  48. ///
  49. /// After making the `Call`, users must `invoke` the call with a callback which is invoked
  50. /// for each response part (or error) received. Any call to `send(_:promise:)` prior to calling
  51. /// `invoke` will fail and not be sent. Users are also responsible for closing the request stream
  52. /// by sending the `.end` request part.
  53. ///
  54. /// - Parameters:
  55. /// - path: The path of the RPC, e.g. "/echo.Echo/get".
  56. /// - type: The type of the RPC, e.g. `.unary`.
  57. /// - callOptions: Options for the RPC.
  58. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  59. func makeCall<Request: GRPCPayload, Response: GRPCPayload>(
  60. path: String,
  61. type: GRPCCallType,
  62. callOptions: CallOptions,
  63. interceptors: [ClientInterceptor<Request, Response>]
  64. ) -> Call<Request, Response>
  65. /// Close the channel, and any connections associated with it.
  66. func close() -> EventLoopFuture<Void>
  67. }
  68. extension GRPCChannel {
  69. /// Make a unary gRPC call.
  70. ///
  71. /// - Parameters:
  72. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  73. /// - request: The request to send.
  74. /// - callOptions: Options for the RPC.
  75. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  76. public func makeUnaryCall<Request: Message, Response: Message>(
  77. path: String,
  78. request: Request,
  79. callOptions: CallOptions,
  80. interceptors: [ClientInterceptor<Request, Response>] = []
  81. ) -> UnaryCall<Request, Response> {
  82. let unary: UnaryCall<Request, Response> = UnaryCall(
  83. call: self.makeCall(
  84. path: path,
  85. type: .unary,
  86. callOptions: callOptions,
  87. interceptors: interceptors
  88. )
  89. )
  90. unary.invoke(request)
  91. return unary
  92. }
  93. /// Make a unary gRPC call.
  94. ///
  95. /// - Parameters:
  96. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  97. /// - request: The request to send.
  98. /// - callOptions: Options for the RPC.
  99. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  100. public func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  101. path: String,
  102. request: Request,
  103. callOptions: CallOptions,
  104. interceptors: [ClientInterceptor<Request, Response>] = []
  105. ) -> UnaryCall<Request, Response> {
  106. let rpc: UnaryCall<Request, Response> = UnaryCall(
  107. call: self.makeCall(
  108. path: path,
  109. type: .unary,
  110. callOptions: callOptions,
  111. interceptors: interceptors
  112. )
  113. )
  114. rpc.invoke(request)
  115. return rpc
  116. }
  117. /// Makes a client-streaming gRPC call.
  118. ///
  119. /// - Parameters:
  120. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  121. /// - callOptions: Options for the RPC.
  122. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  123. public func makeClientStreamingCall<Request: Message, Response: Message>(
  124. path: String,
  125. callOptions: CallOptions,
  126. interceptors: [ClientInterceptor<Request, Response>] = []
  127. ) -> ClientStreamingCall<Request, Response> {
  128. let rpc: ClientStreamingCall<Request, Response> = ClientStreamingCall(
  129. call: self.makeCall(
  130. path: path,
  131. type: .clientStreaming,
  132. callOptions: callOptions,
  133. interceptors: interceptors
  134. )
  135. )
  136. rpc.invoke()
  137. return rpc
  138. }
  139. /// Makes a client-streaming gRPC call.
  140. ///
  141. /// - Parameters:
  142. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  143. /// - callOptions: Options for the RPC.
  144. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  145. public func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  146. path: String,
  147. callOptions: CallOptions,
  148. interceptors: [ClientInterceptor<Request, Response>] = []
  149. ) -> ClientStreamingCall<Request, Response> {
  150. let rpc: ClientStreamingCall<Request, Response> = ClientStreamingCall(
  151. call: self.makeCall(
  152. path: path,
  153. type: .clientStreaming,
  154. callOptions: callOptions,
  155. interceptors: interceptors
  156. )
  157. )
  158. rpc.invoke()
  159. return rpc
  160. }
  161. /// Make a server-streaming gRPC call.
  162. ///
  163. /// - Parameters:
  164. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  165. /// - request: The request to send.
  166. /// - callOptions: Options for the RPC.
  167. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  168. /// - handler: Response handler; called for every response received from the server.
  169. public func makeServerStreamingCall<Request: Message, Response: Message>(
  170. path: String,
  171. request: Request,
  172. callOptions: CallOptions,
  173. interceptors: [ClientInterceptor<Request, Response>] = [],
  174. handler: @escaping (Response) -> Void
  175. ) -> ServerStreamingCall<Request, Response> {
  176. let rpc: ServerStreamingCall<Request, Response> = ServerStreamingCall(
  177. call: self.makeCall(
  178. path: path,
  179. type: .serverStreaming,
  180. callOptions: callOptions,
  181. interceptors: interceptors
  182. ),
  183. callback: handler
  184. )
  185. rpc.invoke(request)
  186. return rpc
  187. }
  188. /// Make a server-streaming gRPC call.
  189. ///
  190. /// - Parameters:
  191. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  192. /// - request: The request to send.
  193. /// - callOptions: Options for the RPC.
  194. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  195. /// - handler: Response handler; called for every response received from the server.
  196. public func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  197. path: String,
  198. request: Request,
  199. callOptions: CallOptions,
  200. interceptors: [ClientInterceptor<Request, Response>] = [],
  201. handler: @escaping (Response) -> Void
  202. ) -> ServerStreamingCall<Request, Response> {
  203. let rpc: ServerStreamingCall<Request, Response> = ServerStreamingCall(
  204. call: self.makeCall(
  205. path: path,
  206. type: .serverStreaming,
  207. callOptions: callOptions,
  208. interceptors: []
  209. ),
  210. callback: handler
  211. )
  212. rpc.invoke(request)
  213. return rpc
  214. }
  215. /// Makes a bidirectional-streaming gRPC call.
  216. ///
  217. /// - Parameters:
  218. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  219. /// - callOptions: Options for the RPC.
  220. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  221. /// - handler: Response handler; called for every response received from the server.
  222. public func makeBidirectionalStreamingCall<Request: Message, Response: Message>(
  223. path: String,
  224. callOptions: CallOptions,
  225. interceptors: [ClientInterceptor<Request, Response>] = [],
  226. handler: @escaping (Response) -> Void
  227. ) -> BidirectionalStreamingCall<Request, Response> {
  228. let rpc: BidirectionalStreamingCall<Request, Response> = BidirectionalStreamingCall(
  229. call: self.makeCall(
  230. path: path,
  231. type: .bidirectionalStreaming,
  232. callOptions: callOptions,
  233. interceptors: interceptors
  234. ),
  235. callback: handler
  236. )
  237. rpc.invoke()
  238. return rpc
  239. }
  240. /// Makes a bidirectional-streaming gRPC call.
  241. ///
  242. /// - Parameters:
  243. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  244. /// - callOptions: Options for the RPC.
  245. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  246. /// - handler: Response handler; called for every response received from the server.
  247. public func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  248. path: String,
  249. callOptions: CallOptions,
  250. interceptors: [ClientInterceptor<Request, Response>] = [],
  251. handler: @escaping (Response) -> Void
  252. ) -> BidirectionalStreamingCall<Request, Response> {
  253. let rpc: BidirectionalStreamingCall<Request, Response> = BidirectionalStreamingCall(
  254. call: self.makeCall(
  255. path: path,
  256. type: .bidirectionalStreaming,
  257. callOptions: callOptions,
  258. interceptors: interceptors
  259. ),
  260. callback: handler
  261. )
  262. rpc.invoke()
  263. return rpc
  264. }
  265. }