UtilsURL.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2025 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. private let serverURLVersion = "/v1"
  17. private let serverURLProjects = "/projects/"
  18. private let serverURLNamespaces = "/namespaces/"
  19. private let serverURLQuery = "fetch"
  20. class Utils {
  21. class func constructServerURL(domain: String, apiKey: String?, optionsID: String,
  22. namespace: String) -> URL {
  23. var components = URLComponents()
  24. components.scheme = "https"
  25. components.host = domain
  26. guard let apiKey else {
  27. fatalError("Missing `APIKey` from `FirebaseOptions`, please ensure the configured " +
  28. "`FirebaseApp` is configured with `FirebaseOptions` that contains an `APIKey`.")
  29. }
  30. components.path =
  31. "\(serverURLVersion)\(serverURLProjects)\(optionsID)\(serverURLNamespaces)" +
  32. "\(namespaceOnly(namespace)):\(serverURLQuery)"
  33. components.queryItems = [
  34. URLQueryItem(name: "key", value: apiKey),
  35. ]
  36. guard let url = components.url else {
  37. fatalError("Could not construct valid URL. Check your project ID, namespace, and API Key")
  38. }
  39. return url
  40. }
  41. class func namespaceOnly(_ fullyQualifiedNamespace: String) -> String {
  42. let separatorIndex = fullyQualifiedNamespace.firstIndex(of: ":") ?? fullyQualifiedNamespace
  43. .endIndex
  44. return String(fullyQualifiedNamespace[..<separatorIndex])
  45. }
  46. }