From cd88b1c1f1b208e8a36a8ba6bcaef7b81b741e36 Mon Sep 17 00:00:00 2001 From: Henrik Panhans Date: Mon, 29 Jun 2020 14:54:28 +0200 Subject: [PATCH] Added support to specify URLSession --- HPNetwork.podspec | 4 ++-- Package.swift | 2 +- Sources/HPNetwork/DecodableRequest.swift | 5 +++++ Sources/HPNetwork/ImageRequest.swift | 3 +++ Sources/HPNetwork/NetworkRequest+URLSessionTask.swift | 6 +++--- Sources/HPNetwork/NetworkRequest.swift | 1 + 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/HPNetwork.podspec b/HPNetwork.podspec index 57e4138..3a90567 100644 --- a/HPNetwork.podspec +++ b/HPNetwork.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "HPNetwork" - s.version = "0.4.2" + s.version = "0.5.0" s.summary = "A lightweight but customisable networking stack written in Swift" s.swift_version = "5.0" @@ -12,7 +12,7 @@ Pod::Spec.new do |s| s.author = { "Henrik Panhans" => "henrik@panhans.dev" } s.social_media_url = "https://twitter.com/henrik_dmg" - s.ios.deployment_target = "8.0" + s.ios.deployment_target = "9.0" s.osx.deployment_target = "10.11" s.watchos.deployment_target = "3.0" s.tvos.deployment_target = "9.0" diff --git a/Package.swift b/Package.swift index d372214..3eab024 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "HPNetwork", platforms: [ - .iOS(.v8), .macOS(.v10_11), .tvOS(.v9), .watchOS(.v3) + .iOS(.v9), .macOS(.v10_11), .tvOS(.v9), .watchOS(.v3) ], products: [ .library(name: "HPNetwork", targets: ["HPNetwork"]), diff --git a/Sources/HPNetwork/DecodableRequest.swift b/Sources/HPNetwork/DecodableRequest.swift index 7563328..ba92409 100644 --- a/Sources/HPNetwork/DecodableRequest.swift +++ b/Sources/HPNetwork/DecodableRequest.swift @@ -5,6 +5,7 @@ open class DecodableRequest: NetworkRequest { public typealias Input = Data public typealias Output = T + public let urlSession: URLSession public let finishingQueue: DispatchQueue public let requestMethod: NetworkRequestMethod public let authentication: NetworkRequestAuthentication? @@ -21,11 +22,13 @@ open class DecodableRequest: NetworkRequest { public init( urlString: String, + urlSession: URLSession = .shared, finishingQueue: DispatchQueue = .main, requestMethod: NetworkRequestMethod = .get, authentication: NetworkRequestAuthentication? = nil) { self.urlString = urlString + self.urlSession = urlSession self.finishingQueue = finishingQueue self.requestMethod = requestMethod self.authentication = authentication @@ -33,11 +36,13 @@ open class DecodableRequest: NetworkRequest { public init( url: URL, + urlSession: URLSession = .shared, finishingQueue: DispatchQueue = .main, requestMethod: NetworkRequestMethod = .get, authentication: NetworkRequestAuthentication? = nil) { self.urlString = url.absoluteString + self.urlSession = urlSession self.finishingQueue = finishingQueue self.requestMethod = requestMethod self.authentication = authentication diff --git a/Sources/HPNetwork/ImageRequest.swift b/Sources/HPNetwork/ImageRequest.swift index ea49eac..4889e3c 100644 --- a/Sources/HPNetwork/ImageRequest.swift +++ b/Sources/HPNetwork/ImageRequest.swift @@ -13,17 +13,20 @@ public class ImageDownloadRequest: NetworkRequest { #endif public let url: URL? + public let urlSession: URLSession public let finishingQueue: DispatchQueue public let requestMethod: NetworkRequestMethod public let authentication: NetworkRequestAuthentication? public init( url: URL?, + urlSession: URLSession = .shared, finishingQueue: DispatchQueue = .main, requestMethod: NetworkRequestMethod = .get, authentication: NetworkRequestAuthentication? = nil) { self.url = url + self.urlSession = urlSession self.finishingQueue = finishingQueue self.requestMethod = requestMethod self.authentication = authentication diff --git a/Sources/HPNetwork/NetworkRequest+URLSessionTask.swift b/Sources/HPNetwork/NetworkRequest+URLSessionTask.swift index 98abfc9..82fc1d3 100644 --- a/Sources/HPNetwork/NetworkRequest+URLSessionTask.swift +++ b/Sources/HPNetwork/NetworkRequest+URLSessionTask.swift @@ -13,7 +13,7 @@ extension NetworkRequest { let queue = finishingQueue - return URLSession.shared.dataTask(with: urlRequest) { data, response, error in + return urlSession.dataTask(with: urlRequest) { data, response, error in self.finish( data: data, response: response, @@ -36,7 +36,7 @@ extension NetworkRequest { let queue = finishingQueue - return URLSession.shared.uploadTask(with: urlRequest, from: data) { data, response, error in + return urlSession.uploadTask(with: urlRequest, from: data) { data, response, error in self.finish( data: data, response: response, @@ -59,7 +59,7 @@ extension NetworkRequest { let queue = finishingQueue - return URLSession.shared.uploadTask(with: urlRequest, fromFile: fileURL) { data, response, error in + return urlSession.uploadTask(with: urlRequest, fromFile: fileURL) { data, response, error in self.finish( data: data, response: response, diff --git a/Sources/HPNetwork/NetworkRequest.swift b/Sources/HPNetwork/NetworkRequest.swift index 108391f..f5c839d 100644 --- a/Sources/HPNetwork/NetworkRequest.swift +++ b/Sources/HPNetwork/NetworkRequest.swift @@ -16,6 +16,7 @@ public protocol NetworkRequest { var url: URL? { get } var requestMethod: NetworkRequestMethod { get } var authentication: NetworkRequestAuthentication? { get } + var urlSession: URLSession { get } func convertResponse(response: NetworkResponse) throws -> Output