Skip to content

Commit 3a74450

Browse files
authored
Merge pull request #93 from Aldo10012/cleanup_and_fix_warnings
Cleanup and fix warnings
2 parents 8bcc43e + 042c883 commit 3a74450

File tree

11 files changed

+155
-177
lines changed

11 files changed

+155
-177
lines changed

Sources/EZNetworking/Util/Downloader/FileDownloader.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,34 @@ public class FileDownloader: FileDownloadable {
2020
self.decoder = decoder
2121
}
2222

23-
// MARK: Async Await
23+
// MARK: - CORE - AsyncStream
24+
25+
public func downloadFileStream(from serverUrl: URL) -> AsyncStream<DownloadStreamEvent> {
26+
AsyncStream { continuation in
27+
configureProgressTracking { progress in
28+
continuation.yield(.progress(progress))
29+
}
30+
let task = Task {
31+
do {
32+
let (localURL, response) = try await session.urlSession.download(from: serverUrl, delegate: nil)
33+
try validator.validateStatus(from: response)
34+
let url = try validator.validateUrl(localURL)
35+
continuation.yield(.success(url))
36+
continuation.finish()
37+
} catch is CancellationError {
38+
// optional: silently finish or emit failure
39+
} catch {
40+
continuation.yield(.failure(mapError(error)))
41+
continuation.finish()
42+
}
43+
}
44+
continuation.onTermination = { @Sendable _ in
45+
task.cancel()
46+
}
47+
}
48+
}
49+
50+
// MARK: - Adapter - async/await
2451

2552
public func downloadFile(
2653
from serverUrl: URL,
@@ -39,7 +66,7 @@ public class FileDownloader: FileDownloadable {
3966
throw NetworkingError.internalError(.unknown)
4067
}
4168

42-
// MARK: Completion Handler
69+
// MARK: - Adapter - callbacks
4370

4471
@discardableResult
4572
public func downloadFileTask(
@@ -57,7 +84,7 @@ public class FileDownloader: FileDownloadable {
5784
return cancellableRequest
5885
}
5986

60-
// MARK: Publisher
87+
// MARK: - Adapter - Publisher
6188

6289
public func downloadFilePublisher(
6390
from serverUrl: URL,
@@ -75,33 +102,6 @@ public class FileDownloader: FileDownloadable {
75102
.eraseToAnyPublisher()
76103
}
77104

78-
// MARK: AsyncStream
79-
80-
public func downloadFileStream(from serverUrl: URL) -> AsyncStream<DownloadStreamEvent> {
81-
AsyncStream { continuation in
82-
configureProgressTracking { progress in
83-
continuation.yield(.progress(progress))
84-
}
85-
let task = Task {
86-
do {
87-
let (localURL, response) = try await session.urlSession.download(from: serverUrl, delegate: nil)
88-
try validator.validateStatus(from: response)
89-
let url = try validator.validateUrl(localURL)
90-
continuation.yield(.success(url))
91-
continuation.finish()
92-
} catch is CancellationError {
93-
// optional: silently finish or emit failure
94-
} catch {
95-
continuation.yield(.failure(mapError(error)))
96-
continuation.finish()
97-
}
98-
}
99-
continuation.onTermination = { @Sendable _ in
100-
task.cancel()
101-
}
102-
}
103-
}
104-
105105
// MARK: - Helpers
106106

107107
private func createTaskAndPerform(

Sources/EZNetworking/Util/Performers/RequestPerformer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct RequestPerformer: RequestPerformable {
1616
self.decoder = decoder
1717
}
1818

19-
// MARK: Async Await
19+
// MARK: - CORE - async/await
2020

2121
public func perform<T: Decodable & Sendable>(
2222
request: Request,
@@ -33,7 +33,7 @@ public struct RequestPerformer: RequestPerformable {
3333
}
3434
}
3535

36-
// MARK: Completion Handler
36+
// MARK: - Adapter - callbacks
3737

3838
@discardableResult
3939
public func performTask<T: Decodable & Sendable>(
@@ -51,7 +51,7 @@ public struct RequestPerformer: RequestPerformable {
5151
return cancellableRequest
5252
}
5353

54-
// MARK: Publisher
54+
// MARK: - Adapter - Publisher
5555

5656
public func performPublisher<T: Decodable & Sendable>(
5757
request: Request,
@@ -69,7 +69,7 @@ public struct RequestPerformer: RequestPerformable {
6969
.eraseToAnyPublisher()
7070
}
7171

72-
// MARK: Helpers
72+
// MARK: - Helpers
7373

7474
private func createTaskAndPerform<T: Decodable & Sendable>(
7575
request: Request,

Sources/EZNetworking/Util/Uploader/DataUploader/DataUploader.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,33 @@ public class DataUploader: DataUploadable {
1717
self.validator = validator
1818
}
1919

20-
// MARK: Async Await
20+
// MARK: - CORE - AsyncStream
21+
22+
public func uploadDataStream(_ data: Data, with request: Request) -> AsyncStream<UploadStreamEvent> {
23+
AsyncStream { continuation in
24+
configureProgressTracking { progress in
25+
continuation.yield(.progress(progress))
26+
}
27+
let task = Task {
28+
do {
29+
let urlRequest = try request.getURLRequest()
30+
let (data, urlResponse) = try await session.urlSession.upload(for: urlRequest, from: data)
31+
try validator.validateStatus(from: urlResponse)
32+
let validData = try validator.validateData(data)
33+
continuation.yield(.success(validData))
34+
continuation.finish()
35+
} catch {
36+
continuation.yield(.failure(mapError(error)))
37+
continuation.finish()
38+
}
39+
}
40+
continuation.onTermination = { @Sendable _ in
41+
task.cancel()
42+
}
43+
}
44+
}
45+
46+
// MARK: - Adapter - async/await
2147

2248
public func uploadData(_ data: Data, with request: Request, progress: UploadProgressHandler?) async throws -> Data {
2349
for await event in uploadDataStream(data, with: request) {
@@ -33,7 +59,7 @@ public class DataUploader: DataUploadable {
3359
throw NetworkingError.internalError(.unknown)
3460
}
3561

36-
// MARK: Completion Handler
62+
// MARK: - Adapter - callbacks
3763

3864
@discardableResult
3965
public func uploadDataTask(_ data: Data, with request: Request, progress: UploadProgressHandler?, completion: @escaping (UploadCompletionHandler)) -> CancellableRequest {
@@ -47,7 +73,7 @@ public class DataUploader: DataUploadable {
4773
return cancellableRequest
4874
}
4975

50-
// MARK: Publisher
76+
// MARK: - Adapter - Publisher
5177

5278
public func uploadDataPublisher(_ data: Data, with request: Request, progress: UploadProgressHandler?) -> AnyPublisher<Data, NetworkingError> {
5379
Deferred {
@@ -62,33 +88,7 @@ public class DataUploader: DataUploadable {
6288
.eraseToAnyPublisher()
6389
}
6490

65-
// MARK: Async Stream
66-
67-
public func uploadDataStream(_ data: Data, with request: Request) -> AsyncStream<UploadStreamEvent> {
68-
AsyncStream { continuation in
69-
configureProgressTracking { progress in
70-
continuation.yield(.progress(progress))
71-
}
72-
let task = Task {
73-
do {
74-
let urlRequest = try request.getURLRequest()
75-
let (data, urlResponse) = try await session.urlSession.upload(for: urlRequest, from: data)
76-
try validator.validateStatus(from: urlResponse)
77-
let validData = try validator.validateData(data)
78-
continuation.yield(.success(validData))
79-
continuation.finish()
80-
} catch {
81-
continuation.yield(.failure(mapError(error)))
82-
continuation.finish()
83-
}
84-
}
85-
continuation.onTermination = { @Sendable _ in
86-
task.cancel()
87-
}
88-
}
89-
}
90-
91-
// MARK: Helpers
91+
// MARK: - Helpers
9292

9393
private func createTaskAndPerform(
9494
_ data: Data,

Sources/EZNetworking/Util/Uploader/FileUploader/FileUploader.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,33 @@ public class FileUploader: FileUploadable {
1717
self.validator = validator
1818
}
1919

20-
// MARK: Async Await
20+
// MARK: - CORE - AsyncStream
21+
22+
public func uploadFileStream(_ fileURL: URL, with request: any Request) -> AsyncStream<UploadStreamEvent> {
23+
AsyncStream { continuation in
24+
configureProgressTracking { progress in
25+
continuation.yield(.progress(progress))
26+
}
27+
let task = Task {
28+
do {
29+
let urlRequest = try request.getURLRequest()
30+
let (data, urlResponse) = try await session.urlSession.upload(for: urlRequest, fromFile: fileURL)
31+
try validator.validateStatus(from: urlResponse)
32+
let validData = try validator.validateData(data)
33+
continuation.yield(.success(validData))
34+
continuation.finish()
35+
} catch {
36+
continuation.yield(.failure(mapError(error)))
37+
continuation.finish()
38+
}
39+
}
40+
continuation.onTermination = { @Sendable _ in
41+
task.cancel()
42+
}
43+
}
44+
}
45+
46+
// MARK: - Adapter - async/await
2147

2248
public func uploadFile(_ fileURL: URL, with request: any Request, progress: UploadProgressHandler?) async throws -> Data {
2349
for await event in uploadFileStream(fileURL, with: request) {
@@ -33,7 +59,7 @@ public class FileUploader: FileUploadable {
3359
throw NetworkingError.internalError(.unknown)
3460
}
3561

36-
// MARK: Completion Handler
62+
// MARK: - Adapter - callbacks
3763

3864
@discardableResult
3965
public func uploadFileTask(_ fileURL: URL, with request: any Request, progress: UploadProgressHandler?, completion: @escaping UploadCompletionHandler) -> CancellableRequest? {
@@ -47,7 +73,7 @@ public class FileUploader: FileUploadable {
4773
return cancellableRequest
4874
}
4975

50-
// MARK: Publisher
76+
// MARK: - Adapter - Publisher
5177

5278
public func uploadFilePublisher(_ fileURL: URL, with request: any Request, progress: UploadProgressHandler?) -> AnyPublisher<Data, NetworkingError> {
5379
Deferred {
@@ -62,32 +88,6 @@ public class FileUploader: FileUploadable {
6288
.eraseToAnyPublisher()
6389
}
6490

65-
// MARK: AsyncStream
66-
67-
public func uploadFileStream(_ fileURL: URL, with request: any Request) -> AsyncStream<UploadStreamEvent> {
68-
AsyncStream { continuation in
69-
configureProgressTracking { progress in
70-
continuation.yield(.progress(progress))
71-
}
72-
let task = Task {
73-
do {
74-
let urlRequest = try request.getURLRequest()
75-
let (data, urlResponse) = try await session.urlSession.upload(for: urlRequest, fromFile: fileURL)
76-
try validator.validateStatus(from: urlResponse)
77-
let validData = try validator.validateData(data)
78-
continuation.yield(.success(validData))
79-
continuation.finish()
80-
} catch {
81-
continuation.yield(.failure(mapError(error)))
82-
continuation.finish()
83-
}
84-
}
85-
continuation.onTermination = { @Sendable _ in
86-
task.cancel()
87-
}
88-
}
89-
}
90-
9191
// MARK: - Helpers
9292

9393
private func createTaskAndPerform(

0 commit comments

Comments
 (0)