RemoteConfigConsole.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseCore
  15. class RemoteConfigConsole {
  16. private let projectID: String
  17. private var latestConfig: [String: Any]!
  18. public var requestTimeout: TimeInterval = 10
  19. private var consoleURL: URL {
  20. let api = "https://firebaseremoteconfig.googleapis.com"
  21. let endpoint = "/v1/projects/\(projectID)/remoteConfig"
  22. return URL(string: api + endpoint)!
  23. }
  24. private lazy var accessToken: String = {
  25. guard let fileURL = Bundle.main.url(forResource: "AccessToken", withExtension: "json") else {
  26. fatalError("Could not find AccessToken.json in bundle.")
  27. }
  28. guard let data = try? Data(contentsOf: fileURL),
  29. let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments),
  30. let jsonDict = json as? [String: Any],
  31. let accessToken = jsonDict["access_token"] as? String else {
  32. fatalError("Could not retrieve access token.")
  33. }
  34. return accessToken
  35. }()
  36. /// Synchronously fetches and returns currently active Remote Config, if it exists.
  37. public var activeRemoteConfig: [String: Any]? {
  38. var config: [String: Any]?
  39. perform(configRequest: .get) { latestConfigJSON in
  40. config = latestConfigJSON
  41. }
  42. if let config {
  43. saveConfig(config)
  44. }
  45. return config
  46. }
  47. /// Exposing this initializer allows us to create`RemoteConfigConsole` instances without
  48. /// depending on a `GoogleService-Info.plist`.
  49. init(projectID: String) {
  50. self.projectID = projectID
  51. syncWithConsole()
  52. }
  53. /// This initializer will attempt to read from a `GoogleService-Info.plist` to set `projectID`.
  54. convenience init() {
  55. let projectID = FirebaseApp.app()?.options.projectID
  56. self.init(projectID: projectID!)
  57. }
  58. // MARK: - Public API
  59. /// Update Remote Config with multiple String key value pairs.
  60. /// - Parameter parameters: Dictionary representation of config key value pairs.
  61. public func updateRemoteConfig(with parameters: [String: CustomStringConvertible]) {
  62. var updatedConfig: [String: Any] = latestConfig
  63. let latestParameters = latestConfig["parameters"] as? [String: Any]
  64. var updatedParameters = latestParameters ?? [String: Any]()
  65. for (key, value) in parameters {
  66. updatedParameters.updateValue(["defaultValue": ["value": value.description]], forKey: key)
  67. }
  68. updatedConfig.updateValue(updatedParameters, forKey: "parameters")
  69. publish(config: updatedConfig)
  70. }
  71. /// Updates a Remote Config value for a given key.
  72. /// - Parameters:
  73. /// - value: Use strings, numbers, and booleans to represent Remote Config values.
  74. /// - key: The corresponding string key that maps to the given value.
  75. public func updateRemoteConfigValue(_ value: CustomStringConvertible, forKey key: String) {
  76. var updatedConfig: [String: Any] = latestConfig
  77. let latestParameters = latestConfig["parameters"] as? [String: Any]
  78. if var parameters = latestParameters {
  79. parameters.updateValue(["defaultValue": ["value": value.description]], forKey: key)
  80. updatedConfig.updateValue(parameters, forKey: "parameters")
  81. } else {
  82. updatedConfig.updateValue(
  83. [key: ["defaultValue": ["value": value.description]]],
  84. forKey: "parameters"
  85. )
  86. }
  87. publish(config: updatedConfig)
  88. }
  89. public func removeRemoteConfigValue(forKey key: String) {
  90. var updatedConfig: [String: Any] = latestConfig
  91. let latestParameters = latestConfig["parameters"] as? [String: Any]
  92. if var parameters = latestParameters {
  93. parameters.removeValue(forKey: key)
  94. updatedConfig.updateValue(parameters, forKey: "parameters")
  95. }
  96. publish(config: updatedConfig)
  97. }
  98. public func clearRemoteConfig() {
  99. var updatedConfig: [String: Any]! = latestConfig
  100. updatedConfig.removeValue(forKey: "parameters")
  101. publish(config: updatedConfig)
  102. }
  103. // MARK: - Networking
  104. private enum ConfigRequest {
  105. case get, put(_ data: Data)
  106. var httpMethod: String {
  107. switch self {
  108. case .get: return "GET"
  109. case .put(data: _): return "PUT"
  110. }
  111. }
  112. var httpBody: Data? {
  113. switch self {
  114. case .get: return nil
  115. case let .put(data: data): return data
  116. }
  117. }
  118. var httpHeaderFields: [String: String]? {
  119. switch self {
  120. case .get: return nil
  121. case .put(data: _):
  122. return ["Content-Type": "application/json; UTF8", "If-Match": "*"]
  123. }
  124. }
  125. func secureRequest(url: URL, with token: String, _ timeout: TimeInterval = 10) -> URLRequest {
  126. var request = URLRequest(url: url, timeoutInterval: timeout)
  127. request.httpMethod = httpMethod
  128. request.allHTTPHeaderFields = httpHeaderFields
  129. request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
  130. request.httpBody = httpBody
  131. return request
  132. }
  133. }
  134. /// Performs a given `ConfigRequest` synchronously.
  135. private func perform(configRequest: ConfigRequest,
  136. _ completion: (([String: Any]?) -> Void)? = nil) {
  137. let request = configRequest.secureRequest(url: consoleURL, with: accessToken, requestTimeout)
  138. let semaphore = DispatchSemaphore(value: 0)
  139. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  140. // Signal the semaphore when this scope is escaped.
  141. defer { semaphore.signal() }
  142. guard let data = data else {
  143. print(String(describing: error))
  144. return
  145. }
  146. let json = try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
  147. if let response = response as? HTTPURLResponse,
  148. let json = json as? [String: Any] {
  149. if response.statusCode >= 400 {
  150. print("RemoteConfigConsole Error: \(String(describing: json["error"]!))")
  151. }
  152. }
  153. completion?(json as? [String: Any])
  154. }
  155. task.resume()
  156. semaphore.wait()
  157. }
  158. /// Publishes a config object to the live console and updates `latestConfig`.
  159. private func publish(config: [String: Any]) {
  160. let configData = data(withConfig: config)
  161. perform(configRequest: .put(configData))
  162. saveConfig(config)
  163. }
  164. // MARK: - Private Helpers
  165. /// Creates an optional Data object given a config object.
  166. /// Used for serializing config objects before posting them to live console.
  167. private func data(withConfig config: [String: Any]) -> Data {
  168. let dictionary = NSDictionary(dictionary: config, copyItems: true)
  169. let data = try! JSONSerialization.data(withJSONObject: dictionary, options: .fragmentsAllowed)
  170. return data
  171. }
  172. /// Perform a synchronous sync with remote config console.
  173. private func syncWithConsole() {
  174. if let activeRemoteConfig {
  175. latestConfig = activeRemoteConfig
  176. } else {
  177. fatalError("Could not sync with console.")
  178. }
  179. }
  180. /// A more intuitively named setter for `latestConfig`.
  181. private func saveConfig(_ config: [String: Any]) {
  182. latestConfig = config
  183. }
  184. }
  185. // MARK: - Extensions
  186. extension Bundle {
  187. func plistValue(forKey key: String, fromPlist plist: String) -> Any? {
  188. guard let plistURL = url(forResource: plist, withExtension: "") else {
  189. print("Could not find plist file \(plist) in bundle.")
  190. return nil
  191. }
  192. let plistDictionary = NSDictionary(contentsOf: plistURL)
  193. return plistDictionary?.object(forKey: key)
  194. }
  195. }