2
0

GRPCChannel+AsyncAwaitSupport.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.5)
  17. import SwiftProtobuf
  18. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  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<Request: Message, Response: Message>(
  28. path: String,
  29. request: Request,
  30. callOptions: CallOptions,
  31. interceptors: [ClientInterceptor<Request, Response>] = []
  32. ) -> GRPCAsyncUnaryCall<Request, Response> {
  33. return GRPCAsyncUnaryCall.makeAndInvoke(
  34. call: self.makeCall(
  35. path: path,
  36. type: .unary,
  37. callOptions: callOptions,
  38. interceptors: interceptors
  39. ),
  40. request
  41. )
  42. }
  43. /// Make a unary gRPC call.
  44. ///
  45. /// - Parameters:
  46. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  47. /// - request: The request to send.
  48. /// - callOptions: Options for the RPC.
  49. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  50. internal func makeAsyncUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  51. path: String,
  52. request: Request,
  53. callOptions: CallOptions,
  54. interceptors: [ClientInterceptor<Request, Response>] = []
  55. ) -> GRPCAsyncUnaryCall<Request, Response> {
  56. return GRPCAsyncUnaryCall.makeAndInvoke(
  57. call: self.makeCall(
  58. path: path,
  59. type: .unary,
  60. callOptions: callOptions,
  61. interceptors: interceptors
  62. ),
  63. request
  64. )
  65. }
  66. /// Makes a client-streaming gRPC call.
  67. ///
  68. /// - Parameters:
  69. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  70. /// - callOptions: Options for the RPC.
  71. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  72. internal func makeAsyncClientStreamingCall<Request: Message, Response: Message>(
  73. path: String,
  74. callOptions: CallOptions,
  75. interceptors: [ClientInterceptor<Request, Response>] = []
  76. ) -> GRPCAsyncClientStreamingCall<Request, Response> {
  77. return GRPCAsyncClientStreamingCall.makeAndInvoke(
  78. call: self.makeCall(
  79. path: path,
  80. type: .clientStreaming,
  81. callOptions: callOptions,
  82. interceptors: interceptors
  83. )
  84. )
  85. }
  86. /// Makes a client-streaming gRPC call.
  87. ///
  88. /// - Parameters:
  89. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  90. /// - callOptions: Options for the RPC.
  91. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  92. internal func makeAsyncClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  93. path: String,
  94. callOptions: CallOptions,
  95. interceptors: [ClientInterceptor<Request, Response>] = []
  96. ) -> GRPCAsyncClientStreamingCall<Request, Response> {
  97. return GRPCAsyncClientStreamingCall.makeAndInvoke(
  98. call: self.makeCall(
  99. path: path,
  100. type: .clientStreaming,
  101. callOptions: callOptions,
  102. interceptors: interceptors
  103. )
  104. )
  105. }
  106. /// Make a server-streaming gRPC call.
  107. ///
  108. /// - Parameters:
  109. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  110. /// - request: The request to send.
  111. /// - callOptions: Options for the RPC.
  112. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  113. internal func makeAsyncServerStreamingCall<Request: Message, Response: Message>(
  114. path: String,
  115. request: Request,
  116. callOptions: CallOptions,
  117. interceptors: [ClientInterceptor<Request, Response>] = []
  118. ) -> GRPCAsyncServerStreamingCall<Request, Response> {
  119. return GRPCAsyncServerStreamingCall.makeAndInvoke(
  120. call: self.makeCall(
  121. path: path,
  122. type: .serverStreaming,
  123. callOptions: callOptions,
  124. interceptors: interceptors
  125. ),
  126. request
  127. )
  128. }
  129. /// Make a server-streaming gRPC call.
  130. ///
  131. /// - Parameters:
  132. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  133. /// - request: The request to send.
  134. /// - callOptions: Options for the RPC.
  135. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  136. internal func makeAsyncServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  137. path: String,
  138. request: Request,
  139. callOptions: CallOptions,
  140. interceptors: [ClientInterceptor<Request, Response>] = []
  141. ) -> GRPCAsyncServerStreamingCall<Request, Response> {
  142. return GRPCAsyncServerStreamingCall.makeAndInvoke(
  143. call: self.makeCall(
  144. path: path,
  145. type: .serverStreaming,
  146. callOptions: callOptions,
  147. interceptors: []
  148. ),
  149. request
  150. )
  151. }
  152. /// Makes a bidirectional-streaming gRPC call.
  153. ///
  154. /// - Parameters:
  155. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  156. /// - callOptions: Options for the RPC.
  157. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  158. internal func makeAsyncBidirectionalStreamingCall<Request: Message, Response: Message>(
  159. path: String,
  160. callOptions: CallOptions,
  161. interceptors: [ClientInterceptor<Request, Response>] = []
  162. ) -> GRPCAsyncBidirectionalStreamingCall<Request, Response> {
  163. return GRPCAsyncBidirectionalStreamingCall.makeAndInvoke(
  164. call: self.makeCall(
  165. path: path,
  166. type: .bidirectionalStreaming,
  167. callOptions: callOptions,
  168. interceptors: interceptors
  169. )
  170. )
  171. }
  172. /// Makes a bidirectional-streaming gRPC call.
  173. ///
  174. /// - Parameters:
  175. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  176. /// - callOptions: Options for the RPC.
  177. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  178. internal func makeAsyncBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  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. }
  193. #endif