GRPCClient.swift 5.3 KB

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