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 URLRequest
NetworkHalpers
may be used without NetworkHandler
. However, NetworkHandler
depends on NetworkHalpers
.
HTTPMethod
GET
, POST
, etc)HTTPHeaderKey
Authorization
, Content-Type
, etc)HTTPHeaderValue
image/jpeg
, application/json
, etc)MultipartFormInputStream
InputStream
for efficient form uploading.URLRequest
func encodeData<EncodableType: Encodable>(_ encodableType: EncodableType, encoder: NHEncoder? = nil)
as a convenient way to encapsulate data for sending to servers. Uses URLRequest.defaultEncoder
which can be either a JSONEncoder
or PropertyListEncoder
(or any other encoder you create and conform to NHEncoder
)func setContentType(_ contentType: HTTPHeaderValue)
and func setAuthorization(_ value: HTTPHeaderValue)
as convenient methods to set exceptionally common headers on requestsURL
var urlRequest: URLRequest
adds convenient creation of a URLRequest
from a URL
Adds robust custom caching, progress tracking, control over how URLSessionTasks
are constructed, and convenience for mocking on top of URLSession
NetworkRequest
from a url url.request
URLRequest
properties and methods, you can additionally set the priority that a task will be created, provide a decoder for decoding a Decodable
response, and provide the expected response code range to have an error automatcially thrown if the code is not within the range.NetworkHandlerTransferDelegate
for progress and state tracking or if you otherwise want to be able to retrieve the associated URLSessionTask
that is running behind the async method.NetworkHelper
(or the default instance) initiate an async transfer via transferMahDatas
or transferMahCodableDatas
.package(url: "https://github.com/mredig/NetworkHandler.git", .upToNextMinor(from: "1.0.0"))
to the appropriate section of your Package.swiftNetworkHandler
- add that as a dependency to any targets you want to use it in.
swift package update or use Xcodeimport NetworkHandler
to the top of any file you with to use it inSources/NetworkHandler
folder to your project, if you're masochistic.import NetworkHandler
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.
link |
Stars: 7 |
Last commit: 3 days ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics