Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 1.42 KB

README.md

File metadata and controls

59 lines (40 loc) · 1.42 KB

HPNetwork Swift

HPNetwork is a lightweight but customizable network stack.

Posting Request

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

Creating Requests

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))

Cancelling Requests

Any call to send(request) { result in ... } returns an instance of NetworkTask that you can cancel by calling task.cancel()

WIP

  • Adding ability to add HTTP body data
  • Improving the documentation