SettingsDownloadClient.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Copyright 2022 Google LLC
  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. import Foundation
  16. #if SWIFT_PACKAGE
  17. @_implementationOnly import GoogleUtilities_Environment
  18. #else
  19. @_implementationOnly import GoogleUtilities
  20. #endif // SWIFT_PACKAGE
  21. protocol SettingsDownloadClient {
  22. func fetch(completion: @escaping (Result<[String: Any], SettingsDownloaderError>) -> Void)
  23. }
  24. enum SettingsDownloaderError: Error {
  25. /// Error contructing the URL
  26. case URLError(String)
  27. /// Error from the URLSession task
  28. case URLSessionError(String)
  29. /// Error parsing the JSON response from Settings
  30. case JSONParseError(String)
  31. /// Error getting the Installation ID
  32. case InstallationIDError(String)
  33. }
  34. class SettingsDownloader: SettingsDownloadClient {
  35. private let appInfo: ApplicationInfoProtocol
  36. private let installations: InstallationsProtocol
  37. init(appInfo: ApplicationInfoProtocol, installations: InstallationsProtocol) {
  38. self.appInfo = appInfo
  39. self.installations = installations
  40. }
  41. func fetch(completion: @escaping (Result<[String: Any], SettingsDownloaderError>) -> Void) {
  42. guard let validURL = url else {
  43. completion(.failure(.URLError("Invalid URL")))
  44. return
  45. }
  46. installations.installationID { result in
  47. switch result {
  48. case let .success(fiid):
  49. let request = self.buildRequest(url: validURL, fiid: fiid)
  50. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  51. if let data = data {
  52. if let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
  53. completion(.success(dict))
  54. } else {
  55. completion(.failure(
  56. .JSONParseError("Failed to parse JSON to dictionary")
  57. ))
  58. }
  59. } else if let error = error {
  60. completion(.failure(.URLSessionError(error.localizedDescription)))
  61. }
  62. }
  63. // Start the task that sends the network request
  64. task.resume()
  65. case let .failure(error):
  66. completion(.failure(.InstallationIDError(error.localizedDescription)))
  67. }
  68. }
  69. }
  70. private var url: URL? {
  71. var components = URLComponents()
  72. components.scheme = "https"
  73. components.host = "firebase-settings.crashlytics.com"
  74. components.path = "/spi/v2/platforms/\(appInfo.osName)/gmp/\(appInfo.appID)/settings"
  75. components.queryItems = [
  76. URLQueryItem(name: "build_version", value: appInfo.appBuildVersion),
  77. URLQueryItem(name: "display_version", value: appInfo.appDisplayVersion),
  78. ]
  79. return components.url
  80. }
  81. private func buildRequest(url: URL, fiid: String) -> URLRequest {
  82. var request = URLRequest(url: url)
  83. request.setValue("application/json", forHTTPHeaderField: "Accept")
  84. request.setValue(fiid, forHTTPHeaderField: "X-Crashlytics-Installation-ID")
  85. request.setValue(appInfo.deviceModel, forHTTPHeaderField: "X-Crashlytics-Device-Model")
  86. request.setValue(
  87. appInfo.osBuildVersion,
  88. forHTTPHeaderField: "X-Crashlytics-OS-Build-Version"
  89. )
  90. request.setValue(
  91. appInfo.osDisplayVersion,
  92. forHTTPHeaderField: "X-Crashlytics-OS-Display-Version"
  93. )
  94. request.setValue(appInfo.sdkVersion, forHTTPHeaderField: "X-Crashlytics-API-Client-Version")
  95. return request
  96. }
  97. }