GRPCChannel.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /// Make a unary gRPC call.
  67. ///
  68. /// - Parameters:
  69. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  70. /// - request: The request to send.
  71. /// - callOptions: Options for the RPC.
  72. func makeUnaryCall<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>(
  73. path: String,
  74. request: Request,
  75. callOptions: CallOptions
  76. ) -> UnaryCall<Request, Response>
  77. /// Make a unary gRPC call.
  78. ///
  79. /// - Parameters:
  80. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  81. /// - request: The request to send.
  82. /// - callOptions: Options for the RPC.
  83. func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  84. path: String,
  85. request: Request,
  86. callOptions: CallOptions
  87. ) -> UnaryCall<Request, Response>
  88. /// Make a server-streaming gRPC call.
  89. ///
  90. /// - Parameters:
  91. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  92. /// - request: The request to send.
  93. /// - callOptions: Options for the RPC.
  94. /// - handler: Response handler; called for every response received from the server.
  95. func makeServerStreamingCall<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>(
  96. path: String,
  97. request: Request,
  98. callOptions: CallOptions,
  99. handler: @escaping (Response) -> Void
  100. ) -> ServerStreamingCall<Request, Response>
  101. /// Make a server-streaming gRPC call.
  102. ///
  103. /// - Parameters:
  104. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  105. /// - request: The request to send.
  106. /// - callOptions: Options for the RPC.
  107. /// - handler: Response handler; called for every response received from the server.
  108. func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  109. path: String,
  110. request: Request,
  111. callOptions: CallOptions,
  112. handler: @escaping (Response) -> Void
  113. ) -> ServerStreamingCall<Request, Response>
  114. /// Makes a client-streaming gRPC call.
  115. ///
  116. /// - Parameters:
  117. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  118. /// - callOptions: Options for the RPC.
  119. func makeClientStreamingCall<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>(
  120. path: String,
  121. callOptions: CallOptions
  122. ) -> ClientStreamingCall<Request, Response>
  123. /// Makes a client-streaming gRPC call.
  124. ///
  125. /// - Parameters:
  126. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  127. /// - callOptions: Options for the RPC.
  128. func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  129. path: String,
  130. callOptions: CallOptions
  131. ) -> ClientStreamingCall<Request, Response>
  132. /// Makes a bidirectional-streaming gRPC call.
  133. ///
  134. /// - Parameters:
  135. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  136. /// - callOptions: Options for the RPC.
  137. /// - handler: Response handler; called for every response received from the server.
  138. func makeBidirectionalStreamingCall<
  139. Request: SwiftProtobuf.Message,
  140. Response: SwiftProtobuf.Message
  141. >(
  142. path: String,
  143. callOptions: CallOptions,
  144. handler: @escaping (Response) -> Void
  145. ) -> BidirectionalStreamingCall<Request, Response>
  146. /// Makes a bidirectional-streaming gRPC call.
  147. ///
  148. /// - Parameters:
  149. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  150. /// - callOptions: Options for the RPC.
  151. /// - handler: Response handler; called for every response received from the server.
  152. func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  153. path: String,
  154. callOptions: CallOptions,
  155. handler: @escaping (Response) -> Void
  156. ) -> BidirectionalStreamingCall<Request, Response>
  157. /// Close the channel, and any connections associated with it.
  158. func close() -> EventLoopFuture<Void>
  159. }