GRPCChannel+AsyncAwaitSupport.swift 7.0 KB

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