NetworkHandler was originally created to reduce boilerplate when using
URLSession. With the advent of Async/Await in Swift 5.5, that's largely a non issue now. However, there are still some shortcomings of URLSession.
Bring some type safety and convenience to constructing a
NetworkHalpers may be used without
NetworkHandler depends on
InputStreamfor efficient form uploading.
func encodeData<EncodableType: Encodable>(_ encodableType: EncodableType, encoder: NHEncoder? = nil)as a convenient way to encapsulate data for sending to servers. Uses
URLRequest.defaultEncoderwhich can be either a
PropertyListEncoder(or any other encoder you create and conform to
func setContentType(_ contentType: HTTPHeaderValue)and
func setAuthorization(_ value: HTTPHeaderValue)as convenient methods to set exceptionally common headers on requests
var urlRequest: URLRequestadds convenient creation of a
Adds robust custom caching, progress tracking, control over how
URLSessionTasks are constructed, and convenience for mocking on top of
NetworkRequestfrom a url
URLRequestproperties and methods, you can additionally set the priority that a task will be created, provide a decoder for decoding a
Decodableresponse, and provide the expected response code range to have an error automatcially thrown if the code is not within the range.
NetworkHandlerTransferDelegatefor progress and state tracking or if you otherwise want to be able to retrieve the associated
URLSessionTaskthat is running behind the async method.
NetworkHelper(or the default instance) initiate an async transfer via
.package(url: "https://github.com/mredig/NetworkHandler.git", .upToNextMinor(from: "1.0.0"))to the appropriate section of your Package.swift
NetworkHandler- add that as a dependency to any targets you want to use it in. swift package update or use Xcode
import NetworkHandlerto the top of any file you with to use it in
Sources/NetworkHandlerfolder to your project, if you're masochistic.
Everything should be compatible with all Apple platforms that support Swift 5.5 with Async/Await.
However, while the previous version was theoretically cross compatible with Linux, this latest iteration is not. I started an attempt (which you can see on the
linux-compatibility branch), but ultimately it was more involved than the time I had available to proceed with support. Right now, the main obstacle is KVO compatibility for progress tracking a download. If someone is abitious enough, you should be able to get progress information from the delegate's data loaded method and open a PR. A couple files also need
FoundationNetworking imported conditionally for Linux.