@@ -17,29 +17,6 @@ public class DataUploader: DataUploadable {
1717 self . validator = validator
1818 }
1919
20- // MARK: - CORE - async/await
21-
22- public func uploadData( _ data: Data , with request: Request , progress: UploadProgressHandler ? ) async throws -> Data {
23- try Task . checkCancellation ( )
24- configureProgressTracking { percentage in
25- guard !Task. isCancelled else { return }
26- progress ? ( percentage)
27- }
28- do {
29- let urlRequest = try request. getURLRequest ( )
30- let ( data, urlResponse) = try await session. urlSession. upload ( for: urlRequest, from: data)
31- try Task . checkCancellation ( )
32- try validator. validateStatus ( from: urlResponse)
33- return data
34- } catch let cancellationError as CancellationError {
35- throw cancellationError
36- } catch {
37- throw mapError ( error)
38- }
39- }
40-
41- // MARK: - Adapter - AsyncStream
42-
4320 public func uploadDataStream( _ data: Data , with request: Request ) -> AsyncStream < UploadStreamEvent > {
4421 AsyncStream { continuation in
4522 let task = Task {
@@ -62,55 +39,27 @@ public class DataUploader: DataUploadable {
6239 }
6340 }
6441
65- // MARK: - Adapter - callbacks
66-
67- @discardableResult
68- public func uploadDataTask( _ data: Data , with request: Request , progress: UploadProgressHandler ? , completion: @escaping ( UploadCompletionHandler ) ) -> CancellableRequest {
69- let taskBox = TaskBox ( )
70- let cancellableRequest = CancellableRequest { [ weak self] in
71- taskBox. task = self ? . createTaskAndPerform ( data, with: request, progress: progress, completion: completion)
72- } onCancel: {
73- taskBox. task? . cancel ( )
42+ private func uploadData( _ data: Data , with request: Request , progress: UploadProgressHandler ? ) async throws -> Data {
43+ try Task . checkCancellation ( )
44+ configureProgressTracking { percentage in
45+ guard !Task. isCancelled else { return }
46+ progress ? ( percentage)
7447 }
75- cancellableRequest. resume ( )
76- return cancellableRequest
77- }
78-
79- // MARK: - Adapter - Publisher
80-
81- public func uploadDataPublisher( _ data: Data , with request: Request , progress: UploadProgressHandler ? ) -> AnyPublisher < Data , NetworkingError > {
82- Deferred {
83- let taskBox = TaskBox ( )
84- return Future < Data , NetworkingError > { [ weak self] promise in
85- taskBox. task = self ? . createTaskAndPerform ( data, with: request, progress: progress, completion: { promise ( $0) } )
86- }
87- . handleEvents ( receiveCancel: {
88- taskBox. task? . cancel ( )
89- } )
48+ do {
49+ let urlRequest = try request. getURLRequest ( )
50+ let ( data, urlResponse) = try await session. urlSession. upload ( for: urlRequest, from: data)
51+ try Task . checkCancellation ( )
52+ try validator. validateStatus ( from: urlResponse)
53+ return data
54+ } catch let cancellationError as CancellationError {
55+ throw cancellationError
56+ } catch {
57+ throw mapError ( error)
9058 }
91- . eraseToAnyPublisher ( )
9259 }
9360
9461 // MARK: - Helpers
9562
96- private func createTaskAndPerform(
97- _ data: Data ,
98- with request: Request ,
99- progress: UploadProgressHandler ? ,
100- completion: @escaping ( ( Result < Data , NetworkingError > ) -> Void )
101- ) -> Task < Void , Never > {
102- Task {
103- do {
104- let data = try await uploadData ( data, with: request, progress: progress)
105- completion ( . success( data) )
106- } catch is CancellationError {
107- // Do nothing, task has been cancelled
108- } catch {
109- completion ( . failure( mapError ( error) ) )
110- }
111- }
112- }
113-
11463 private func mapError( _ error: Error ) -> NetworkingError {
11564 if let networkError = error as? NetworkingError { return networkError }
11665 if let urlError = error as? URLError { return . requestFailed( reason: . urlError( underlying: urlError) ) }
0 commit comments