GRPCClient.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /// A gRPC client.
  19. public protocol GRPCClient {
  20. /// Initialize the client with the given channel and default call options.
  21. init(channel: GRPCChannel, defaultCallOptions: CallOptions)
  22. /// The gRPC channel over which RPCs are sent and received. Note that this is distinct
  23. /// from `NIO.Channel`.
  24. var channel: GRPCChannel { get }
  25. /// The call options to use should the user not provide per-call options.
  26. var defaultCallOptions: CallOptions { get set }
  27. }
  28. extension GRPCClient {
  29. public func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  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 makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  42. path: String,
  43. request: Request,
  44. callOptions: CallOptions? = nil,
  45. responseType: Response.Type = Response.self,
  46. handler: @escaping (Response) -> Void
  47. ) -> ServerStreamingCall<Request, Response> {
  48. return self.channel.makeServerStreamingCall(
  49. path: path,
  50. request: request,
  51. callOptions: callOptions ?? self.defaultCallOptions,
  52. handler: handler
  53. )
  54. }
  55. public func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  56. path: String,
  57. callOptions: CallOptions? = nil,
  58. requestType: Request.Type = Request.self,
  59. responseType: Response.Type = Response.self
  60. ) -> ClientStreamingCall<Request, Response> {
  61. return self.channel.makeClientStreamingCall(
  62. path: path,
  63. callOptions: callOptions ?? self.defaultCallOptions
  64. )
  65. }
  66. public func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  67. path: String,
  68. callOptions: CallOptions? = nil,
  69. requestType: Request.Type = Request.self,
  70. responseType: Response.Type = Response.self,
  71. handler: @escaping (Response) -> Void
  72. ) -> BidirectionalStreamingCall<Request, Response> {
  73. return self.channel.makeBidirectionalStreamingCall(
  74. path: path,
  75. callOptions: callOptions ?? self.defaultCallOptions,
  76. handler: handler
  77. )
  78. }
  79. }
  80. /// A client which has no generated stubs and may be used to create gRPC calls manually.
  81. /// See `GRPCClient` for details.
  82. public final class AnyServiceClient: GRPCClient {
  83. public let channel: GRPCChannel
  84. public var defaultCallOptions: CallOptions
  85. /// Creates a client which may be used to call any service.
  86. ///
  87. /// - Parameters:
  88. /// - connection: `ClientConnection` to the service host.
  89. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  90. public init(channel: GRPCChannel, defaultCallOptions: CallOptions = CallOptions()) {
  91. self.channel = channel
  92. self.defaultCallOptions = defaultCallOptions
  93. }
  94. }