Skip to content

Commit

Permalink
Add async download for older system versions
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed May 8, 2024
1 parent cfc28d0 commit a5c4bc5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "Resting",
defaultLocalization: "en",
platforms: [.iOS(.v13), .macOS(.v12)],
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
Expand Down
33 changes: 33 additions & 0 deletions Sources/Resting/Helpers/URLSession+AsyncDownload.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// URLSession+AsyncDownload.swift
//
//
// Created by Ulaş Sancak on 8.05.2024.
//

import Foundation

/// A set of extensions for `URLSession` that provide asynchronous download capabilities.
extension URLSession {
/// Asynchronously downloads a file for a given `URLRequest`.
///
/// - Parameters:
/// - request: The `URLRequest` to be downloaded.
/// - Returns: A tuple containing the downloaded file's URL and its associated URL response.
/// - Throws: Any errors encountered during the download operation.
func download(for request: URLRequest) async throws -> (URL, URLResponse) {
return try await withCheckedThrowingContinuation { continuation in
downloadTask(with: request) { url, response, error in
guard let url, let response else {
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(throwing: RestingError.unknown)
}
return
}
continuation.resume(returning: (url, response))
}
}
}
}

0 comments on commit a5c4bc5

Please sign in to comment.