Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable open Package.swift without Error #65

Merged
merged 8 commits into from
Jun 25, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions Sources/OpenGraph/Extension/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,17 @@ public extension URLSession {
/// - returns: A tuple containing the binary `Data` that was downloaded,
/// as well as a `URLResponse` representing the server's response.
/// - throws: Any error encountered while performing the data task.
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
var dataTask: URLSessionDataTask?
let onCancel = { dataTask?.cancel() }

return try await withTaskCancellationHandler(
handler: {
onCancel()
},
operation: {
try await withCheckedThrowingContinuation { continuation in
dataTask = 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))
}

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