GRPCClient.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: GRPCClientConnection { 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,
  30. responseType: Response.Type = Response.self
  31. ) -> UnaryClientCall<Request, Response> {
  32. return UnaryClientCall(connection: self.connection, path: path, request: request, callOptions: callOptions)
  33. }
  34. public func makeServerStreamingCall<Request: Message, Response: Message>(
  35. path: String,
  36. request: Request,
  37. callOptions: CallOptions,
  38. responseType: Response.Type = Response.self,
  39. handler: @escaping (Response) -> Void
  40. ) -> ServerStreamingClientCall<Request, Response> {
  41. return ServerStreamingClientCall(connection: self.connection, path: path, request: request, callOptions: callOptions, handler: handler)
  42. }
  43. public func makeClientStreamingCall<Request: Message, Response: Message>(
  44. path: String,
  45. callOptions: CallOptions,
  46. requestType: Request.Type = Request.self,
  47. responseType: Response.Type = Response.self
  48. ) -> ClientStreamingClientCall<Request, Response> {
  49. return ClientStreamingClientCall(connection: self.connection, path: path, callOptions: callOptions)
  50. }
  51. public func makeBidirectionalStreamingCall<Request: Message, Response: Message>(
  52. path: String,
  53. callOptions: CallOptions,
  54. requestType: Request.Type = Request.self,
  55. responseType: Response.Type = Response.self,
  56. handler: @escaping (Response) -> Void
  57. ) -> BidirectionalStreamingClientCall<Request, Response> {
  58. return BidirectionalStreamingClientCall(connection: self.connection, path: path, callOptions: callOptions, handler: handler)
  59. }
  60. }
  61. /// A GRPC client for a named service.
  62. public protocol GRPCServiceClient: GRPCClient {
  63. /// Name of the service this client is for (e.g. "echo.Echo").
  64. var serviceName: String { get }
  65. /// Creates a path for a given method on this service.
  66. ///
  67. /// This defaults to "/Service-Name/Method-Name" but may be overriden if consumers
  68. /// require a different path format.
  69. ///
  70. /// - Parameter method: name of method to return a path for.
  71. /// - Returns: path for the given method used in gRPC request headers.
  72. func path(forMethod method: String) -> String
  73. }
  74. extension GRPCServiceClient {
  75. public func path(forMethod method: String) -> String {
  76. return "/\(self.serviceName)/\(method)"
  77. }
  78. }