ServiceClient.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, arguments: [Channel.Argument] = []) {
  43. gRPC.initialize()
  44. channel = Channel(address: address, secure: secure, arguments: arguments)
  45. metadata = Metadata()
  46. }
  47. /// Create a client using a pre-defined channel.
  48. public init(channel: Channel) {
  49. self.channel = channel
  50. metadata = Metadata()
  51. }
  52. /// Create a client that makes secure connections with a custom certificate.
  53. public init(address: String, certificates: String, clientCertificates: String? = nil, clientKey: String? = nil, arguments: [Channel.Argument] = []) {
  54. gRPC.initialize()
  55. channel = Channel(address: address, certificates: certificates, clientCertificates: clientCertificates, clientKey: clientKey, arguments: arguments)
  56. metadata = Metadata()
  57. }
  58. }
  59. /// Simple fake implementation of ServiceClient that returns a previously-defined set of results
  60. /// and stores request values passed into it for later verification.
  61. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  62. open class ServiceClientTestStubBase: ServiceClient {
  63. open var channel: Channel { fatalError("not implemented") }
  64. open var metadata = Metadata()
  65. open var host = ""
  66. open var timeout: TimeInterval = 0
  67. public init() {}
  68. }