Skip to content

Commit

Permalink
Move APIs with default argument to URLSessionProtocol extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Sep 2, 2024
1 parent 34c9796 commit 6987773
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 70 deletions.
75 changes: 41 additions & 34 deletions Sources/Pulse/URLSessionProxy/URLSessionProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {}
43 changes: 7 additions & 36 deletions Sources/Pulse/URLSessionProxy/URLSessionProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<URLSessionDataTask?>(nil)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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")
}
}

0 comments on commit 6987773

Please sign in to comment.