GRPCChannel+AsyncAwaitSupport.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2021, 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 SwiftProtobuf
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. extension GRPCChannel {
  19. /// Make a unary gRPC call.
  20. ///
  21. /// - Parameters:
  22. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  23. /// - request: The request to send.
  24. /// - callOptions: Options for the RPC.
  25. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  26. internal func makeAsyncUnaryCall<
  27. Request: Message & Sendable,
  28. Response: Message & Sendable
  29. >(
  30. path: String,
  31. request: Request,
  32. callOptions: CallOptions,
  33. interceptors: [ClientInterceptor<Request, Response>] = []
  34. ) -> GRPCAsyncUnaryCall<Request, Response> {
  35. return GRPCAsyncUnaryCall.makeAndInvoke(
  36. call: self.makeCall(
  37. path: path,
  38. type: .unary,
  39. callOptions: callOptions,
  40. interceptors: interceptors
  41. ),
  42. request
  43. )
  44. }
  45. /// Make a unary gRPC call.
  46. ///
  47. /// - Parameters:
  48. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  49. /// - request: The request to send.
  50. /// - callOptions: Options for the RPC.
  51. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  52. internal func makeAsyncUnaryCall<
  53. Request: GRPCPayload & Sendable,
  54. Response: GRPCPayload & Sendable
  55. >(
  56. path: String,
  57. request: Request,
  58. callOptions: CallOptions,
  59. interceptors: [ClientInterceptor<Request, Response>] = []
  60. ) -> GRPCAsyncUnaryCall<Request, Response> {
  61. return GRPCAsyncUnaryCall.makeAndInvoke(
  62. call: self.makeCall(
  63. path: path,
  64. type: .unary,
  65. callOptions: callOptions,
  66. interceptors: interceptors
  67. ),
  68. request
  69. )
  70. }
  71. /// Makes a client-streaming gRPC call.
  72. ///
  73. /// - Parameters:
  74. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  75. /// - callOptions: Options for the RPC.
  76. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  77. internal func makeAsyncClientStreamingCall<
  78. Request: Message & Sendable,
  79. Response: Message & Sendable
  80. >(
  81. path: String,
  82. callOptions: CallOptions,
  83. interceptors: [ClientInterceptor<Request, Response>] = []
  84. ) -> GRPCAsyncClientStreamingCall<Request, Response> {
  85. return GRPCAsyncClientStreamingCall.makeAndInvoke(
  86. call: self.makeCall(
  87. path: path,
  88. type: .clientStreaming,
  89. callOptions: callOptions,
  90. interceptors: interceptors
  91. )
  92. )
  93. }
  94. /// Makes a client-streaming gRPC call.
  95. ///
  96. /// - Parameters:
  97. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  98. /// - callOptions: Options for the RPC.
  99. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  100. internal func makeAsyncClientStreamingCall<
  101. Request: GRPCPayload & Sendable,
  102. Response: GRPCPayload & Sendable
  103. >(
  104. path: String,
  105. callOptions: CallOptions,
  106. interceptors: [ClientInterceptor<Request, Response>] = []
  107. ) -> GRPCAsyncClientStreamingCall<Request, Response> {
  108. return GRPCAsyncClientStreamingCall.makeAndInvoke(
  109. call: self.makeCall(
  110. path: path,
  111. type: .clientStreaming,
  112. callOptions: callOptions,
  113. interceptors: interceptors
  114. )
  115. )
  116. }
  117. /// Make a server-streaming gRPC call.
  118. ///
  119. /// - Parameters:
  120. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  121. /// - request: The request to send.
  122. /// - callOptions: Options for the RPC.
  123. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  124. internal func makeAsyncServerStreamingCall<
  125. Request: Message & Sendable,
  126. Response: Message & Sendable
  127. >(
  128. path: String,
  129. request: Request,
  130. callOptions: CallOptions,
  131. interceptors: [ClientInterceptor<Request, Response>] = []
  132. ) -> GRPCAsyncServerStreamingCall<Request, Response> {
  133. return GRPCAsyncServerStreamingCall.makeAndInvoke(
  134. call: self.makeCall(
  135. path: path,
  136. type: .serverStreaming,
  137. callOptions: callOptions,
  138. interceptors: interceptors
  139. ),
  140. request
  141. )
  142. }
  143. /// Make a server-streaming gRPC call.
  144. ///
  145. /// - Parameters:
  146. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  147. /// - request: The request to send.
  148. /// - callOptions: Options for the RPC.
  149. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  150. internal func makeAsyncServerStreamingCall<
  151. Request: GRPCPayload & Sendable,
  152. Response: GRPCPayload & Sendable
  153. >(
  154. path: String,
  155. request: Request,
  156. callOptions: CallOptions,
  157. interceptors: [ClientInterceptor<Request, Response>] = []
  158. ) -> GRPCAsyncServerStreamingCall<Request, Response> {
  159. return GRPCAsyncServerStreamingCall.makeAndInvoke(
  160. call: self.makeCall(
  161. path: path,
  162. type: .serverStreaming,
  163. callOptions: callOptions,
  164. interceptors: []
  165. ),
  166. request
  167. )
  168. }
  169. /// Makes a bidirectional-streaming gRPC call.
  170. ///
  171. /// - Parameters:
  172. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  173. /// - callOptions: Options for the RPC.
  174. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  175. internal func makeAsyncBidirectionalStreamingCall<
  176. Request: Message & Sendable,
  177. Response: Message & Sendable
  178. >(
  179. path: String,
  180. callOptions: CallOptions,
  181. interceptors: [ClientInterceptor<Request, Response>] = []
  182. ) -> GRPCAsyncBidirectionalStreamingCall<Request, Response> {
  183. return GRPCAsyncBidirectionalStreamingCall.makeAndInvoke(
  184. call: self.makeCall(
  185. path: path,
  186. type: .bidirectionalStreaming,
  187. callOptions: callOptions,
  188. interceptors: interceptors
  189. )
  190. )
  191. }
  192. /// Makes a bidirectional-streaming gRPC call.
  193. ///
  194. /// - Parameters:
  195. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  196. /// - callOptions: Options for the RPC.
  197. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  198. internal func makeAsyncBidirectionalStreamingCall<
  199. Request: GRPCPayload & Sendable,
  200. Response: GRPCPayload & Sendable
  201. >(
  202. path: String,
  203. callOptions: CallOptions,
  204. interceptors: [ClientInterceptor<Request, Response>] = []
  205. ) -> GRPCAsyncBidirectionalStreamingCall<Request, Response> {
  206. return GRPCAsyncBidirectionalStreamingCall.makeAndInvoke(
  207. call: self.makeCall(
  208. path: path,
  209. type: .bidirectionalStreaming,
  210. callOptions: callOptions,
  211. interceptors: interceptors
  212. )
  213. )
  214. }
  215. }