Skip to content

Commit c44b535

Browse files
author
Guilherme Souza
committed
use URLSession implementation for linux
1 parent fa79834 commit c44b535

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

Sources/Helpers/URLSession+AsyncAwait.swift

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,54 @@
7777
) async throws -> (Data, URLResponse) {
7878
let helper = URLSessionTaskCancellationHelper()
7979

80-
return try await withTaskCancellationHandler(operation: {
80+
return try await withTaskCancellationHandler(
81+
operation: {
82+
try await withCheckedThrowingContinuation { continuation in
83+
let task = dataTask(
84+
with: request,
85+
completionHandler: { data, response, error in
86+
if let error {
87+
continuation.resume(throwing: error)
88+
} else if let data, let response {
89+
continuation.resume(returning: (data, response))
90+
} else {
91+
continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned)
92+
}
93+
})
94+
95+
helper.register(task)
96+
97+
task.resume()
98+
}
99+
},
100+
onCancel: {
101+
helper.cancel()
102+
})
103+
}
104+
105+
public func data(
106+
from url: URL,
107+
delegate _: (any URLSessionTaskDelegate)? = nil
108+
) async throws -> (Data, URLResponse) {
109+
let helper = URLSessionTaskCancellationHelper()
110+
return try await withTaskCancellationHandler {
81111
try await withCheckedThrowingContinuation { continuation in
82-
let task = dataTask(with: request, completionHandler: { data, response, error in
112+
let task = dataTask(with: url) { data, response, error in
83113
if let error {
84114
continuation.resume(throwing: error)
85115
} else if let data, let response {
86116
continuation.resume(returning: (data, response))
87117
} else {
88118
continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned)
89119
}
90-
})
120+
}
91121

92122
helper.register(task)
93-
94123
task.resume()
95124
}
96-
}, onCancel: {
125+
} onCancel: {
97126
helper.cancel()
98-
})
127+
}
99128
}
100129

101130
public func upload(
@@ -105,29 +134,31 @@
105134
) async throws -> (Data, URLResponse) {
106135
let helper = URLSessionTaskCancellationHelper()
107136

108-
return try await withTaskCancellationHandler(operation: {
109-
try await withCheckedThrowingContinuation { continuation in
110-
let task = uploadTask(
111-
with: request,
112-
from: bodyData,
113-
completionHandler: { data, response, error in
114-
if let error {
115-
continuation.resume(throwing: error)
116-
} else if let data, let response {
117-
continuation.resume(returning: (data, response))
118-
} else {
119-
continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned)
137+
return try await withTaskCancellationHandler(
138+
operation: {
139+
try await withCheckedThrowingContinuation { continuation in
140+
let task = uploadTask(
141+
with: request,
142+
from: bodyData,
143+
completionHandler: { data, response, error in
144+
if let error {
145+
continuation.resume(throwing: error)
146+
} else if let data, let response {
147+
continuation.resume(returning: (data, response))
148+
} else {
149+
continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned)
150+
}
120151
}
121-
}
122-
)
152+
)
123153

124-
helper.register(task)
154+
helper.register(task)
125155

126-
task.resume()
127-
}
128-
}, onCancel: {
129-
helper.cancel()
130-
})
156+
task.resume()
157+
}
158+
},
159+
onCancel: {
160+
helper.cancel()
161+
})
131162
}
132163
}
133164

Tests/IntegrationTests/StorageFileIntegrationTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import InlineSnapshotTesting
99
import Storage
1010
import XCTest
11+
import Helpers
1112

1213
#if canImport(FoundationNetworking)
1314
import FoundationNetworking

0 commit comments

Comments
 (0)