GRPCClient.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2019, 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 SwiftProtobuf
  19. /// A gRPC client.
  20. public protocol GRPCClient {
  21. /// The gRPC channel over which RPCs are sent and received. Note that this is distinct
  22. /// from `NIO.Channel`.
  23. var channel: GRPCChannel { get }
  24. /// The call options to use should the user not provide per-call options.
  25. var defaultCallOptions: CallOptions { get set }
  26. }
  27. // MARK: Convenience methods
  28. extension GRPCClient {
  29. public func makeUnaryCall<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>(
  30. path: String,
  31. request: Request,
  32. callOptions: CallOptions? = nil,
  33. interceptors: [ClientInterceptor<Request, Response>] = [],
  34. responseType: Response.Type = Response.self
  35. ) -> UnaryCall<Request, Response> {
  36. return self.channel.makeUnaryCall(
  37. path: path,
  38. request: request,
  39. callOptions: callOptions ?? self.defaultCallOptions,
  40. interceptors: interceptors
  41. )
  42. }
  43. public func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  44. path: String,
  45. request: Request,
  46. callOptions: CallOptions? = nil,
  47. interceptors: [ClientInterceptor<Request, Response>] = [],
  48. responseType: Response.Type = Response.self
  49. ) -> UnaryCall<Request, Response> {
  50. return self.channel.makeUnaryCall(
  51. path: path,
  52. request: request,
  53. callOptions: callOptions ?? self.defaultCallOptions,
  54. interceptors: interceptors
  55. )
  56. }
  57. public func makeServerStreamingCall<
  58. Request: SwiftProtobuf.Message,
  59. Response: SwiftProtobuf.Message
  60. >(
  61. path: String,
  62. request: Request,
  63. callOptions: CallOptions? = nil,
  64. interceptors: [ClientInterceptor<Request, Response>] = [],
  65. responseType: Response.Type = Response.self,
  66. handler: @escaping (Response) -> Void
  67. ) -> ServerStreamingCall<Request, Response> {
  68. return self.channel.makeServerStreamingCall(
  69. path: path,
  70. request: request,
  71. callOptions: callOptions ?? self.defaultCallOptions,
  72. interceptors: interceptors,
  73. handler: handler
  74. )
  75. }
  76. public func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  77. path: String,
  78. request: Request,
  79. callOptions: CallOptions? = nil,
  80. interceptors: [ClientInterceptor<Request, Response>] = [],
  81. responseType: Response.Type = Response.self,
  82. handler: @escaping (Response) -> Void
  83. ) -> ServerStreamingCall<Request, Response> {
  84. return self.channel.makeServerStreamingCall(
  85. path: path,
  86. request: request,
  87. callOptions: callOptions ?? self.defaultCallOptions,
  88. interceptors: interceptors,
  89. handler: handler
  90. )
  91. }
  92. public func makeClientStreamingCall<
  93. Request: SwiftProtobuf.Message,
  94. Response: SwiftProtobuf.Message
  95. >(
  96. path: String,
  97. callOptions: CallOptions? = nil,
  98. interceptors: [ClientInterceptor<Request, Response>] = [],
  99. requestType: Request.Type = Request.self,
  100. responseType: Response.Type = Response.self
  101. ) -> ClientStreamingCall<Request, Response> {
  102. return self.channel.makeClientStreamingCall(
  103. path: path,
  104. callOptions: callOptions ?? self.defaultCallOptions,
  105. interceptors: interceptors
  106. )
  107. }
  108. public func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  109. path: String,
  110. callOptions: CallOptions? = nil,
  111. interceptors: [ClientInterceptor<Request, Response>] = [],
  112. requestType: Request.Type = Request.self,
  113. responseType: Response.Type = Response.self
  114. ) -> ClientStreamingCall<Request, Response> {
  115. return self.channel.makeClientStreamingCall(
  116. path: path,
  117. callOptions: callOptions ?? self.defaultCallOptions,
  118. interceptors: interceptors
  119. )
  120. }
  121. public func makeBidirectionalStreamingCall<
  122. Request: SwiftProtobuf.Message,
  123. Response: SwiftProtobuf.Message
  124. >(
  125. path: String,
  126. callOptions: CallOptions? = nil,
  127. interceptors: [ClientInterceptor<Request, Response>] = [],
  128. requestType: Request.Type = Request.self,
  129. responseType: Response.Type = Response.self,
  130. handler: @escaping (Response) -> Void
  131. ) -> BidirectionalStreamingCall<Request, Response> {
  132. return self.channel.makeBidirectionalStreamingCall(
  133. path: path,
  134. callOptions: callOptions ?? self.defaultCallOptions,
  135. interceptors: interceptors,
  136. handler: handler
  137. )
  138. }
  139. public func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  140. path: String,
  141. callOptions: CallOptions? = nil,
  142. interceptors: [ClientInterceptor<Request, Response>] = [],
  143. requestType: Request.Type = Request.self,
  144. responseType: Response.Type = Response.self,
  145. handler: @escaping (Response) -> Void
  146. ) -> BidirectionalStreamingCall<Request, Response> {
  147. return self.channel.makeBidirectionalStreamingCall(
  148. path: path,
  149. callOptions: callOptions ?? self.defaultCallOptions,
  150. interceptors: interceptors,
  151. handler: handler
  152. )
  153. }
  154. }
  155. /// A client which has no generated stubs and may be used to create gRPC calls manually.
  156. /// See `GRPCClient` for details.
  157. public final class AnyServiceClient: GRPCClient {
  158. public let channel: GRPCChannel
  159. public var defaultCallOptions: CallOptions
  160. /// Creates a client which may be used to call any service.
  161. ///
  162. /// - Parameters:
  163. /// - connection: `ClientConnection` to the service host.
  164. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  165. public init(channel: GRPCChannel, defaultCallOptions: CallOptions = CallOptions()) {
  166. self.channel = channel
  167. self.defaultCallOptions = defaultCallOptions
  168. }
  169. }