GRPCClient.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 GRPC client for a named service.
  82. public protocol GRPCServiceClient: GRPCClient {
  83. /// Name of the service this client is for (e.g. "echo.Echo").
  84. var serviceName: String { get }
  85. /// Creates a path for a given method on this service.
  86. ///
  87. /// This defaults to "/Service-Name/Method-Name" but may be overriden if consumers
  88. /// require a different path format.
  89. ///
  90. /// - Parameter method: name of method to return a path for.
  91. /// - Returns: path for the given method used in gRPC request headers.
  92. func path(forMethod method: String) -> String
  93. }
  94. extension GRPCServiceClient {
  95. public func path(forMethod method: String) -> String {
  96. return "/\(self.serviceName)/\(method)"
  97. }
  98. }
  99. /// A client which has no generated stubs and may be used to create gRPC calls manually.
  100. /// See `GRPCClient` for details.
  101. public final class AnyServiceClient: GRPCClient {
  102. public let connection: ClientConnection
  103. public var defaultCallOptions: CallOptions
  104. /// Creates a client which may be used to call any service.
  105. ///
  106. /// - Parameters:
  107. /// - connection: `ClientConnection` to the service host.
  108. /// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
  109. public init(connection: ClientConnection, defaultCallOptions: CallOptions = CallOptions()) {
  110. self.connection = connection
  111. self.defaultCallOptions = defaultCallOptions
  112. }
  113. }