GRPCClient.swift 3.4 KB

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