Skip to content

Commit 6987773

Browse files
committed
Move APIs with default argument to URLSessionProtocol extensions
1 parent 34c9796 commit 6987773

File tree

2 files changed

+48
-70
lines changed

2 files changed

+48
-70
lines changed

Sources/Pulse/URLSessionProxy/URLSessionProtocol.swift

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55
import Foundation
66

77
public protocol URLSessionProtocol {
8-
var sessionDescription: String? { get set }
9-
10-
func finishTasksAndInvalidate()
11-
12-
/// Cancels all outstanding tasks and then invalidates the session.
13-
///
14-
/// Once invalidated, references to the delegate and callback objects are broken. After invalidation, session objects cannot be reused. To allow outstanding tasks to run until completion, call finishTasksAndInvalidate() instead.
15-
func invalidateAndCancel()
8+
// MARK: - Core
169

1710
func dataTask(with request: URLRequest) -> URLSessionDataTask
1811

@@ -101,32 +94,6 @@ public protocol URLSessionProtocol {
10194

10295
// MARK: - Swift Concurrency
10396

104-
/// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally.
105-
///
106-
/// - Parameter request: The URLRequest for which to load data.
107-
/// - Returns: Data and response.
108-
func data(for request: URLRequest) async throws -> (Data, URLResponse)
109-
110-
/// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally.
111-
///
112-
/// - Parameter url: The URL for which to load data.
113-
/// - Returns: Data and response.
114-
func data(from url: URL) async throws -> (Data, URLResponse)
115-
116-
/// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally.
117-
///
118-
/// - Parameter request: The URLRequest for which to upload data.
119-
/// - Parameter fileURL: File to upload.
120-
/// - Returns: Data and response.
121-
func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse)
122-
123-
/// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally.
124-
///
125-
/// - Parameter request: The URLRequest for which to upload data.
126-
/// - Parameter bodyData: Data to upload.
127-
/// - Returns: Data and response.
128-
func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse)
129-
13097
/// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally.
13198
///
13299
/// - Parameter request: The URLRequest for which to load data.
@@ -193,4 +160,44 @@ public protocol URLSessionProtocol {
193160
func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse)
194161
}
195162

163+
public extension URLSessionProtocol {
164+
/// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally.
165+
///
166+
/// - Parameter request: The URLRequest for which to load data.
167+
/// - Returns: Data and response.
168+
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
169+
try await data(for: request, delegate: nil)
170+
}
171+
172+
/// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally.
173+
///
174+
/// - Parameter url: The URL for which to load data.
175+
/// - Returns: Data and response.
176+
func data(from url: URL) async throws -> (Data, URLResponse) {
177+
try await data(from: url, delegate: nil)
178+
}
179+
180+
/// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally.
181+
///
182+
/// - Parameter request: The URLRequest for which to upload data.
183+
/// - Parameter fileURL: File to upload.
184+
/// - Returns: Data and response.
185+
func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) {
186+
try await upload(for: request, fromFile: fileURL, delegate: nil)
187+
}
188+
189+
/// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally.
190+
///
191+
/// - Parameter request: The URLRequest for which to upload data.
192+
/// - Parameter bodyData: Data to upload.
193+
/// - Returns: Data and response.
194+
func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) {
195+
try await upload(for: request, from: bodyData, delegate: nil)
196+
}
197+
198+
func bytes(from url: URL) async throws -> (URLSession.AsyncBytes, URLResponse) {
199+
try await bytes(from: url, delegate: nil)
200+
}
201+
}
202+
196203
extension URLSession: URLSessionProtocol {}

Sources/Pulse/URLSessionProxy/URLSessionProxy.swift

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable {
5151
self._logger = logger
5252
}
5353

54-
// MARK: - URLSessionProtocol
55-
56-
public var sessionDescription: String? {
57-
get { session.sessionDescription }
58-
set { session.sessionDescription = newValue }
59-
}
60-
61-
public func finishTasksAndInvalidate() {
62-
session.finishTasksAndInvalidate()
63-
}
64-
65-
public func invalidateAndCancel() {
66-
session.invalidateAndCancel()
67-
}
54+
// MARK: - URLSessionProtocol (Core)
6855

6956
public func dataTask(with request: URLRequest) -> URLSessionDataTask {
7057
session.dataTask(with: request)
@@ -119,7 +106,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable {
119106
session.webSocketTask(with: request)
120107
}
121108

122-
// MARK: - Closures
109+
// MARK: - URLSessionProtocol (Closures)
123110

124111
public func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> URLSessionDataTask {
125112
let box = Mutex<URLSessionDataTask?>(nil)
@@ -164,7 +151,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable {
164151
fatalError("Not implemented")
165152
}
166153

167-
// MARK: - Combine
154+
// MARK: - URLSessionProtocol (Combine)
168155

169156
// TODO: add support for logging requests from Combine
170157
public func dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher {
@@ -175,23 +162,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable {
175162
session.dataTaskPublisher(for: request)
176163
}
177164

178-
// MARK: - Swift Concurrency
179-
180-
public func data(for request: URLRequest) async throws -> (Data, URLResponse) {
181-
try await data(for: request, delegate: nil)
182-
}
183-
184-
public func data(from url: URL) async throws -> (Data, URLResponse) {
185-
try await data(from: url, delegate: nil)
186-
}
187-
188-
public func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) {
189-
fatalError("Not implemented")
190-
}
191-
192-
public func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) {
193-
fatalError("Not implemented")
194-
}
165+
// MARK: - URLSessionProtocol (Swift Concurrency)
195166

196167
public func data(for request: URLRequest, delegate: (any URLSessionTaskDelegate)?) async throws -> (Data, URLResponse) {
197168
let delegate = URLSessionProxyDelegate(logger: logger, delegate: delegate)
@@ -210,7 +181,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable {
210181
}
211182
}
212183

213-
public func data(from url: URL, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) {
184+
public func data(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (Data, URLResponse) {
214185
try await data(for: URLRequest(url: url), delegate: delegate)
215186
}
216187

@@ -234,12 +205,12 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable {
234205
fatalError("Not implemented")
235206
}
236207

237-
public func bytes(for request: URLRequest, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (URLSession.AsyncBytes, URLResponse) {
208+
public func bytes(for request: URLRequest, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse) {
238209
fatalError("Not implemented")
239210
}
240211

241212

242-
public func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (URLSession.AsyncBytes, URLResponse) {
213+
public func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse) {
243214
fatalError("Not implemented")
244215
}
245216
}

0 commit comments

Comments
 (0)