ServiceClient.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. required 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. required public init(channel: Channel) {
  49. self.channel = channel
  50. metadata = Metadata()
  51. }
  52. /// Create a client with Google credentials suitable for connecting to a Google-provided API.
  53. /// gRPC protobuf defnitions for use with this method are here: https://github.com/googleapis/googleapis
  54. /// - Parameter googleAPI: the name of the Google API service (e.g. "cloudkms" in "cloudkms.googleapis.com")
  55. /// - Parameter arguments: list of channel configuration options
  56. ///
  57. /// Note: CgRPC's `grpc_google_default_credentials_create` doesn't accept a root pem argument.
  58. /// To override: `export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=/path/to/your/root/cert.pem`
  59. required public init(googleAPI: String, arguments: [Channel.Argument] = []) {
  60. gRPC.initialize()
  61. // Force the address of the Google API to account for the security concern mentioned in
  62. // Sources/CgRPC/include/grpc/grpc_security.h:
  63. // WARNING: Do NOT use this credentials to connect to a non-google service as
  64. // this could result in an oauth2 token leak.
  65. let address = googleAPI + ".googleapis.com"
  66. channel = Channel(googleAddress: address, arguments: arguments)
  67. metadata = Metadata()
  68. }
  69. /// Create a client that makes secure connections with a custom certificate.
  70. required public init(address: String, certificates: String, clientCertificates: String? = nil, clientKey: String? = nil, arguments: [Channel.Argument] = []) {
  71. gRPC.initialize()
  72. channel = Channel(address: address, certificates: certificates, clientCertificates: clientCertificates, clientKey: clientKey, arguments: arguments)
  73. metadata = Metadata()
  74. }
  75. }
  76. /// Simple fake implementation of ServiceClient that returns a previously-defined set of results
  77. /// and stores request values passed into it for later verification.
  78. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  79. open class ServiceClientTestStubBase: ServiceClient {
  80. open var channel: Channel { fatalError("not implemented") }
  81. open var metadata = Metadata()
  82. open var host = ""
  83. open var timeout: TimeInterval = 0
  84. public init() {}
  85. }