Skip to content

Commit

Permalink
Added support to specify URLSession
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik-dmg committed Jun 29, 2020
1 parent 6e00304 commit cd88b1c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions HPNetwork.podspec
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.author = { "Henrik Panhans" => "[email protected]" }
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"
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
5 changes: 5 additions & 0 deletions Sources/HPNetwork/DecodableRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open class DecodableRequest<T: Decodable>: 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?
Expand All @@ -21,23 +22,27 @@ open class DecodableRequest<T: Decodable>: 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
}

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
Expand Down
3 changes: 3 additions & 0 deletions Sources/HPNetwork/ImageRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Sources/HPNetwork/NetworkRequest+URLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions Sources/HPNetwork/NetworkRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit cd88b1c

Please sign in to comment.