ServiceClient.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2018, 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 Dispatch
  17. import Foundation
  18. import SwiftProtobuf
  19. public protocol ServiceClient {
  20. var channel: Channel { get }
  21. /// This metadata will be sent with all requests.
  22. var metadata: Metadata { get }
  23. /// This property allows the service host name to be overridden.
  24. /// For example, it can be used to make calls to "localhost:8080"
  25. /// appear to be to "example.com".
  26. var host: String { get }
  27. /// This property allows the service timeout to be overridden.
  28. var timeout: TimeInterval { get }
  29. }
  30. open class ServiceClientBase: ServiceClient {
  31. public let channel: Channel
  32. public var metadata: Metadata
  33. public var host: String {
  34. get { return channel.host }
  35. set { channel.host = newValue }
  36. }
  37. public var timeout: TimeInterval {
  38. get { return channel.timeout }
  39. set { channel.timeout = newValue }
  40. }
  41. /// Create a client.
  42. public init(address: String, secure: Bool = true) {
  43. gRPC.initialize()
  44. channel = Channel(address: address, secure: secure)
  45. metadata = Metadata()
  46. }
  47. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  48. public init(address: String, certificates: String, host: String?) {
  49. gRPC.initialize()
  50. channel = Channel(address: address, certificates: certificates, host: host)
  51. metadata = Metadata()
  52. }
  53. }
  54. /// Simple fake implementation of ServiceClient that returns a previously-defined set of results
  55. /// and stores request values passed into it for later verification.
  56. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  57. open class ServiceClientTestStubBase: ServiceClient {
  58. open var channel: Channel { fatalError("not implemented") }
  59. open var metadata = Metadata()
  60. open var host = ""
  61. open var timeout: TimeInterval = 0
  62. public init() {}
  63. }