GRPCClient.swift 3.7 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 Foundation
  17. import SwiftProtobuf
  18. /// A GRPC client.
  19. public protocol GRPCClient {
  20. /// The connection providing the underlying HTTP/2 channel for this client.
  21. var connection: ClientConnection { get }
  22. /// The call options to use should the user not provide per-call options.
  23. var defaultCallOptions: CallOptions { get set }
  24. }
  25. extension GRPCClient {
  26. public func makeUnaryCall<Request: Message, Response: Message>(
  27. path: String,
  28. request: Request,
  29. callOptions: CallOptions? = nil,
  30. responseType: Response.Type = Response.self
  31. ) -> UnaryCall<Request, Response> {
  32. return UnaryCall(
  33. connection: self.connection,
  34. path: path,
  35. request: request,
  36. callOptions: callOptions ?? self.defaultCallOptions,
  37. errorDelegate: self.connection.configuration.errorDelegate)
  38. }
  39. public func makeServerStreamingCall<Request: Message, Response: Message>(
  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 ServerStreamingCall(
  47. connection: self.connection,
  48. path: path,
  49. request: request,
  50. callOptions: callOptions ?? self.defaultCallOptions,
  51. errorDelegate: self.connection.configuration.errorDelegate,
  52. handler: handler)
  53. }
  54. public func makeClientStreamingCall<Request: Message, Response: Message>(
  55. path: String,
  56. callOptions: CallOptions? = nil,
  57. requestType: Request.Type = Request.self,
  58. responseType: Response.Type = Response.self
  59. ) -> ClientStreamingCall<Request, Response> {
  60. return ClientStreamingCall(
  61. connection: self.connection,
  62. path: path,
  63. callOptions: callOptions ?? self.defaultCallOptions,
  64. errorDelegate: self.connection.configuration.errorDelegate)
  65. }
  66. public func makeBidirectionalStreamingCall<Request: Message, Response: Message>(
  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 BidirectionalStreamingCall(
  74. connection: self.connection,
  75. path: path,
  76. callOptions: callOptions ?? self.defaultCallOptions,
  77. errorDelegate: self.connection.configuration.errorDelegate,
  78. handler: handler)
  79. }
  80. }
  81. /// A client which has no generated stubs and may be used to create gRPC calls manually.
  82. /// See `GRPCClient` for details.
  83. public final class AnyServiceClient: GRPCClient {
  84. public let connection: ClientConnection
  85. public var defaultCallOptions: CallOptions
  86. /// Creates a client which may be used to call any service.
  87. ///
  88. /// - Parameters:
  89. /// - connection: `ClientConnection` to the service host.
  90. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  91. public init(connection: ClientConnection, defaultCallOptions: CallOptions = CallOptions()) {
  92. self.connection = connection
  93. self.defaultCallOptions = defaultCallOptions
  94. }
  95. }