mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-03-10 08:31:23 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user