extension URLSession {
func data(request: URLRequest) async throws -> (Data, URLResponse) {
if #available(iOS 15.0, *) {
return try await data(for: request)
} else {
return try await withCheckedThrowingContinuation { continuation in
let task = self.dataTask(with: request) { data, response, error in
guard let data = data, let response = response else {
let error = error ?? URLError(.badServerResponse)
return continuation.resume(throwing: error)
}
continuation.resume(returning: (data, response))
}
task.resume()
}
}
}
}