HttpFiles.swift 985 B

123456789101112131415161718192021222324
  1. import Foundation
  2. @testable import Trio
  3. /// Helper struct to download files from localhost via HTTP. Must have a HTTP server
  4. /// running on port 8123 that supports listing files and downloading files
  5. ///
  6. /// This struct is only useful during testing as it is missing a number of error checks
  7. struct HttpFiles {
  8. static func listFiles() async throws -> [String] {
  9. let url = URL(string: "http://localhost:8123/list")!
  10. let (data, _) = try await URLSession.shared.data(from: url)
  11. let files = try JSONDecoder().decode([String].self, from: data)
  12. return files
  13. }
  14. static func downloadFile(at: String) async throws -> AlgorithmComparison {
  15. let decoder = JSONDecoder()
  16. decoder.dateDecodingStrategy = .secondsSince1970
  17. let dataUrl = URL(string: "http://localhost:8123\(at)")!
  18. let (data, _) = try await URLSession.shared.data(from: dataUrl)
  19. return try decoder.decode(AlgorithmComparison.self, from: data)
  20. }
  21. }