ServiceClient.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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: class {
  20. var channel: Channel { get }
  21. /// This metadata will be sent with all requests.
  22. var metadata: Metadata { get set }
  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 set }
  27. /// This property allows the service timeout to be overridden.
  28. var timeout: TimeInterval { get set }
  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 using a pre-defined channel.
  48. public init(channel: Channel) {
  49. gRPC.initialize()
  50. self.channel = channel
  51. self.metadata = Metadata()
  52. }
  53. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  54. public init(address: String, certificates: String, host: String?) {
  55. gRPC.initialize()
  56. channel = Channel(address: address, certificates: certificates, host: host)
  57. metadata = Metadata()
  58. }
  59. }
  60. /// Simple fake implementation of ServiceClient that returns a previously-defined set of results
  61. /// and stores request values passed into it for later verification.
  62. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  63. open class ServiceClientTestStubBase: ServiceClient {
  64. open var channel: Channel { fatalError("not implemented") }
  65. open var metadata = Metadata()
  66. open var host = ""
  67. open var timeout: TimeInterval = 0
  68. public init() {}
  69. }