HPNetwork
is a lightweight but customizable network stack.
To submit a request, use the singleton
Network.shared.send(request) { result in
// ...
}
or submit a custom URLSession
instance
let session = URLSession(...)
Network(session: session).send(request) { result in
// ...
}
Return type is Result<NetworkRequest.Output, Error>
where NetworkRequest.Output
is inferred from the request object
You can either use custom request objects like this:
class IPLocationRequest: NetworkRequest {
typealias Input = Data
typealias Output = IPLocation
let urlString: String = "https://ipapi.co/json"
let requestMethod: NetworkRequestMethod = .get
let authentication: NetworkRequestAuthentication? = nil
}
Or if you're simply trying to Decodable
types from a server, you can use DecodableRequest<Decodable>
directly like so:
DecodableRequest<IPLocation>(
urlString: "https://ipapi.co/json",
requestMethod: .get,
authentication: .basic(username: someUsername, password: userPassword))
Any call to send(request) { result in ... }
returns an instance of NetworkTask
that you can cancel by calling task.cancel()
- Adding ability to add HTTP body data
- Improving the documentation