SettingsDownloadClient.swift 3.8 KB

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