ConfigSetup.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2024 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 Foundation
  15. import FirebaseCore
  16. import FirebaseDataConnect
  17. enum KitchenSinkError: Error {
  18. case configureFailed
  19. }
  20. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  21. actor ProjectConfigurator {
  22. static let shared = ProjectConfigurator()
  23. private init() {}
  24. private var setupComplete = false
  25. func configureProject(useDummyEngine: Bool = true) async throws {
  26. guard !setupComplete else {
  27. // setup already complete
  28. return
  29. }
  30. guard let resourcePath = Bundle.module.resourcePath
  31. else { throw KitchenSinkError.configureFailed }
  32. let projectDirPath = URL(fileURLWithPath: resourcePath)
  33. .appendingPathComponent("fdc-kitchensink/dataconnect").path
  34. let configureBody = """
  35. {
  36. "service_id": "\(KitchenSinkClient.connectorConfig.serviceId)",
  37. "config_directory": "\(projectDirPath)",
  38. "use_dummy": \(useDummyEngine)
  39. }'
  40. """
  41. let configureUrl = URL(string: "http://127.0.0.1:3628/emulator/configure")!
  42. var configureRequest = URLRequest(url: configureUrl)
  43. configureRequest.httpMethod = "POST"
  44. let (data, response) = try await URLSession.shared.upload(
  45. for: configureRequest,
  46. from: configureBody.data(using: .utf8)!
  47. )
  48. print("responseData \(response)")
  49. setupComplete = true
  50. }
  51. }