2
0

GRPCChannel.swift 10 KB

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