SettingsDownloadClient.swift 3.4 KB

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