PrivacyPolicy.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import Foundation
  2. enum PrivacyPolicyLocation {
  3. static let defaultURL = URL(string: "http://a806b934804d0f0ab.awsglobalaccelerator.com/privacy/jokersudoku.html")!
  4. static func fromBundle(_ bundle: Bundle = .main) -> URL {
  5. guard let raw = bundle.object(forInfoDictionaryKey: "PrivacyPolicyURL") as? String,
  6. let url = ActivityURL.parse(raw) else { return defaultURL }
  7. return url.pathExtension.lowercased() == "md" ? url.deletingPathExtension().appendingPathExtension("html") : url
  8. }
  9. }
  10. protocol PrivacyPolicyFetching: Sendable {
  11. func fetchPolicy() async throws -> String
  12. }
  13. struct PrivacyPolicyClient: PrivacyPolicyFetching {
  14. enum PolicyError: Error { case invalidURL, invalidText }
  15. let url: URL?
  16. let transport: DataTransport
  17. init(url: URL?, session: URLSession? = nil) {
  18. self.url = url
  19. transport = NetworkClient(session: session, allowedHTTPHosts: url?.scheme == "http" ? Set([url?.host].compactMap { $0 }) : [])
  20. }
  21. static func fromBundle(_ bundle: Bundle = .main) -> Self {
  22. Self(url: PrivacyPolicyLocation.fromBundle(bundle))
  23. }
  24. func fetchPolicy() async throws -> String {
  25. guard let url, ActivityURL.parse(url.absoluteString) != nil else { throw PolicyError.invalidURL }
  26. var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData)
  27. request.httpMethod = "GET"
  28. request.setValue("text/html, text/markdown;q=0.9, text/plain;q=0.8", forHTTPHeaderField: "Accept")
  29. let data = try await transport.execute(request)
  30. guard let text = String(data: data, encoding: .utf8) else { throw PolicyError.invalidText }
  31. return text
  32. }
  33. }