Add ResumableTask to URLSessionImplementations.mustache (#18438)

* Add `ResumableTask` to `URLSessionImplementations.mustache`

- Makes it testable
- Implementations can return something _other_ than a URLSessionDataTask if they want to implement another request format (sockets maybe?)
- Default implementation for `URLSession` provided

* Regenerate examples

* Add more properties and rename to `CancelableResumableTask`

* Regen samples

* Rename missed reference

* Missed some generated classes somehow

* Rename from `CancellableResumableTask` to `URLSessionDataTaskProtocol`

Rename from `resumableTask` to `dataTaskFromProtocol`

---------

Co-authored-by: welshm-ideogram <welsh@Mikes-MacBook-Pro.local>
This commit is contained in:
Mike Welsh
2024-04-22 08:51:12 -04:00
committed by GitHub
parent cbf52aefe4
commit a724c754bc
34 changed files with 476 additions and 102 deletions

View File

@@ -130,9 +130,9 @@ extension NullEncodable: Codable where Wrapped: Codable {
}
{{/useAlamofire}}
{{^useAlamofire}}
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask: @unchecked Sendable {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ internal class Response<T> {
internal final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
internal protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
internal protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
internal protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
internal func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()

View File

@@ -112,9 +112,9 @@ open class Response<T> {
public final class RequestTask {
private var lock = NSRecursiveLock()
private var task: URLSessionTask?
private var task: URLSessionDataTaskProtocol?
internal func set(task: URLSessionTask) {
internal func set(task: URLSessionDataTaskProtocol) {
lock.lock()
defer { lock.unlock() }
self.task = task

View File

@@ -12,11 +12,33 @@ import MobileCoreServices
import UniformTypeIdentifiers
#endif
public protocol URLSessionProtocol {
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
// Protocol defined for a session data task. This allows mocking out the URLSessionProtocol below since
// you may not want to create or return a real URLSessionDataTask.
public protocol URLSessionDataTaskProtocol {
func resume()
var taskIdentifier: Int { get }
var progress: Progress { get }
func cancel()
}
extension URLSession: URLSessionProtocol {}
// Protocol allowing implementations to alter what is returned or to test their implementations.
public protocol URLSessionProtocol {
// Task which performs the network fetch. Expected to be from URLSession.dataTask(with:completionHandler:) such that a network request
// is sent off when `.resume()` is called.
func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
extension URLSession: URLSessionProtocol {
// Passthrough to URLSession.dataTask(with:completionHandler) since URLSessionDataTask conforms to URLSessionDataTaskProtocol and fetches the network data.
public func dataTaskFromProtocol(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> any URLSessionDataTaskProtocol {
return dataTask(with: request, completionHandler: completionHandler)
}
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
@@ -136,7 +158,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
}
let dataTask = urlSession.dataTask(with: request) { data, response, error in
let dataTask = urlSession.dataTaskFromProtocol(with: request) { data, response, error in
apiResponseQueue.async {
self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
cleanupRequest()