RemoteConfigConsole.swift 7.7 KB

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