diff --git a/Sources/Pulse/URLSessionProxy/URLSessionProtocol.swift b/Sources/Pulse/URLSessionProxy/URLSessionProtocol.swift index 3cb8b2142..ba796375f 100644 --- a/Sources/Pulse/URLSessionProxy/URLSessionProtocol.swift +++ b/Sources/Pulse/URLSessionProxy/URLSessionProtocol.swift @@ -5,14 +5,7 @@ import Foundation public protocol URLSessionProtocol { - var sessionDescription: String? { get set } - - func finishTasksAndInvalidate() - - /// Cancels all outstanding tasks and then invalidates the session. - /// - /// 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. - func invalidateAndCancel() + // MARK: - Core func dataTask(with request: URLRequest) -> URLSessionDataTask @@ -101,32 +94,6 @@ public protocol URLSessionProtocol { // MARK: - Swift Concurrency - /// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally. - /// - /// - Parameter request: The URLRequest for which to load data. - /// - Returns: Data and response. - func data(for request: URLRequest) async throws -> (Data, URLResponse) - - /// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally. - /// - /// - Parameter url: The URL for which to load data. - /// - Returns: Data and response. - func data(from url: URL) async throws -> (Data, URLResponse) - - /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. - /// - /// - Parameter request: The URLRequest for which to upload data. - /// - Parameter fileURL: File to upload. - /// - Returns: Data and response. - func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) - - /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. - /// - /// - Parameter request: The URLRequest for which to upload data. - /// - Parameter bodyData: Data to upload. - /// - Returns: Data and response. - func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) - /// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally. /// /// - Parameter request: The URLRequest for which to load data. @@ -193,4 +160,44 @@ public protocol URLSessionProtocol { func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse) } +public extension URLSessionProtocol { + /// Convenience method to load data using a URLRequest, creates and resumes a URLSessionDataTask internally. + /// + /// - Parameter request: The URLRequest for which to load data. + /// - Returns: Data and response. + func data(for request: URLRequest) async throws -> (Data, URLResponse) { + try await data(for: request, delegate: nil) + } + + /// Convenience method to load data using a URL, creates and resumes a URLSessionDataTask internally. + /// + /// - Parameter url: The URL for which to load data. + /// - Returns: Data and response. + func data(from url: URL) async throws -> (Data, URLResponse) { + try await data(from: url, delegate: nil) + } + + /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. + /// + /// - Parameter request: The URLRequest for which to upload data. + /// - Parameter fileURL: File to upload. + /// - Returns: Data and response. + func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) { + try await upload(for: request, fromFile: fileURL, delegate: nil) + } + + /// Convenience method to upload data using a URLRequest, creates and resumes a URLSessionUploadTask internally. + /// + /// - Parameter request: The URLRequest for which to upload data. + /// - Parameter bodyData: Data to upload. + /// - Returns: Data and response. + func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) { + try await upload(for: request, from: bodyData, delegate: nil) + } + + func bytes(from url: URL) async throws -> (URLSession.AsyncBytes, URLResponse) { + try await bytes(from: url, delegate: nil) + } +} + extension URLSession: URLSessionProtocol {} diff --git a/Sources/Pulse/URLSessionProxy/URLSessionProxy.swift b/Sources/Pulse/URLSessionProxy/URLSessionProxy.swift index 160cd5101..765880479 100644 --- a/Sources/Pulse/URLSessionProxy/URLSessionProxy.swift +++ b/Sources/Pulse/URLSessionProxy/URLSessionProxy.swift @@ -51,20 +51,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable { self._logger = logger } - // MARK: - URLSessionProtocol - - public var sessionDescription: String? { - get { session.sessionDescription } - set { session.sessionDescription = newValue } - } - - public func finishTasksAndInvalidate() { - session.finishTasksAndInvalidate() - } - - public func invalidateAndCancel() { - session.invalidateAndCancel() - } + // MARK: - URLSessionProtocol (Core) public func dataTask(with request: URLRequest) -> URLSessionDataTask { session.dataTask(with: request) @@ -119,7 +106,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable { session.webSocketTask(with: request) } - // MARK: - Closures + // MARK: - URLSessionProtocol (Closures) public func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> URLSessionDataTask { let box = Mutex(nil) @@ -164,7 +151,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable { fatalError("Not implemented") } - // MARK: - Combine + // MARK: - URLSessionProtocol (Combine) // TODO: add support for logging requests from Combine public func dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher { @@ -175,23 +162,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable { session.dataTaskPublisher(for: request) } - // MARK: - Swift Concurrency - - public func data(for request: URLRequest) async throws -> (Data, URLResponse) { - try await data(for: request, delegate: nil) - } - - public func data(from url: URL) async throws -> (Data, URLResponse) { - try await data(from: url, delegate: nil) - } - - public func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) { - fatalError("Not implemented") - } - - public func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) { - fatalError("Not implemented") - } + // MARK: - URLSessionProtocol (Swift Concurrency) public func data(for request: URLRequest, delegate: (any URLSessionTaskDelegate)?) async throws -> (Data, URLResponse) { let delegate = URLSessionProxyDelegate(logger: logger, delegate: delegate) @@ -210,7 +181,7 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable { } } - public func data(from url: URL, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (Data, URLResponse) { + public func data(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (Data, URLResponse) { try await data(for: URLRequest(url: url), delegate: delegate) } @@ -234,12 +205,12 @@ public final class URLSessionProxy: URLSessionProtocol, @unchecked Sendable { fatalError("Not implemented") } - public func bytes(for request: URLRequest, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (URLSession.AsyncBytes, URLResponse) { + public func bytes(for request: URLRequest, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse) { fatalError("Not implemented") } - public func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)? = nil) async throws -> (URLSession.AsyncBytes, URLResponse) { + public func bytes(from url: URL, delegate: (any URLSessionTaskDelegate)?) async throws -> (URLSession.AsyncBytes, URLResponse) { fatalError("Not implemented") } }