GRPCClient.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. responseType: Response.Type = Response.self
  34. ) -> UnaryCall<Request, Response> {
  35. return self.channel.makeUnaryCall(
  36. path: path,
  37. request: request,
  38. callOptions: callOptions ?? self.defaultCallOptions
  39. )
  40. }
  41. public func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  42. path: String,
  43. request: Request,
  44. callOptions: CallOptions? = nil,
  45. responseType: Response.Type = Response.self
  46. ) -> UnaryCall<Request, Response> {
  47. return self.channel.makeUnaryCall(
  48. path: path,
  49. request: request,
  50. callOptions: callOptions ?? self.defaultCallOptions
  51. )
  52. }
  53. public func makeServerStreamingCall<
  54. Request: SwiftProtobuf.Message,
  55. Response: SwiftProtobuf.Message
  56. >(
  57. path: String,
  58. request: Request,
  59. callOptions: CallOptions? = nil,
  60. responseType: Response.Type = Response.self,
  61. handler: @escaping (Response) -> Void
  62. ) -> ServerStreamingCall<Request, Response> {
  63. return self.channel.makeServerStreamingCall(
  64. path: path,
  65. request: request,
  66. callOptions: callOptions ?? self.defaultCallOptions,
  67. handler: handler
  68. )
  69. }
  70. public func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  71. path: String,
  72. request: Request,
  73. callOptions: CallOptions? = nil,
  74. responseType: Response.Type = Response.self,
  75. handler: @escaping (Response) -> Void
  76. ) -> ServerStreamingCall<Request, Response> {
  77. return self.channel.makeServerStreamingCall(
  78. path: path,
  79. request: request,
  80. callOptions: callOptions ?? self.defaultCallOptions,
  81. handler: handler
  82. )
  83. }
  84. public func makeClientStreamingCall<
  85. Request: SwiftProtobuf.Message,
  86. Response: SwiftProtobuf.Message
  87. >(
  88. path: String,
  89. callOptions: CallOptions? = nil,
  90. requestType: Request.Type = Request.self,
  91. responseType: Response.Type = Response.self
  92. ) -> ClientStreamingCall<Request, Response> {
  93. return self.channel.makeClientStreamingCall(
  94. path: path,
  95. callOptions: callOptions ?? self.defaultCallOptions
  96. )
  97. }
  98. public func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  99. path: String,
  100. callOptions: CallOptions? = nil,
  101. requestType: Request.Type = Request.self,
  102. responseType: Response.Type = Response.self
  103. ) -> ClientStreamingCall<Request, Response> {
  104. return self.channel.makeClientStreamingCall(
  105. path: path,
  106. callOptions: callOptions ?? self.defaultCallOptions
  107. )
  108. }
  109. public func makeBidirectionalStreamingCall<
  110. Request: SwiftProtobuf.Message,
  111. Response: SwiftProtobuf.Message
  112. >(
  113. path: String,
  114. callOptions: CallOptions? = nil,
  115. requestType: Request.Type = Request.self,
  116. responseType: Response.Type = Response.self,
  117. handler: @escaping (Response) -> Void
  118. ) -> BidirectionalStreamingCall<Request, Response> {
  119. return self.channel.makeBidirectionalStreamingCall(
  120. path: path,
  121. callOptions: callOptions ?? self.defaultCallOptions,
  122. handler: handler
  123. )
  124. }
  125. public func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  126. path: String,
  127. callOptions: CallOptions? = nil,
  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. handler: handler
  136. )
  137. }
  138. }
  139. /// A client which has no generated stubs and may be used to create gRPC calls manually.
  140. /// See `GRPCClient` for details.
  141. public final class AnyServiceClient: GRPCClient {
  142. public let channel: GRPCChannel
  143. public var defaultCallOptions: CallOptions
  144. /// Creates a client which may be used to call any service.
  145. ///
  146. /// - Parameters:
  147. /// - connection: `ClientConnection` to the service host.
  148. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  149. public init(channel: GRPCChannel, defaultCallOptions: CallOptions = CallOptions()) {
  150. self.channel = channel
  151. self.defaultCallOptions = defaultCallOptions
  152. }
  153. }