import Net
let net = NetURLSession()
net.data(URL(string: "YOUR_URL")!).async { (response, error) in
do {
if let object: [AnyHashable: Any] = try response?.object() {
print("Response dictionary: \(object)")
} else if let error = error {
print("Net error: \(error)")
}
} catch {
print("Parse error: \(error)")
}
}
Request synchronously
import Net
let net = NetURLSession()
do {
let object: [AnyHashable: Any] = try net.data("YOUR_URL").sync().object()
print("Response dictionary: \(object)")
} catch {
print("Error: \(error)")
}
Request from cache
import Net
let net = NetURLSession()
do {
let object: [AnyHashable: Any] = try net.data("YOUR_URL").cached().object()
print("Response dictionary: \(object)")
} catch {
print("Error: \(error)")
}
Track progress
import Net
let net = NetURLSession()
do {
let task = try net.data("YOUR_URL").progress({ progress in
print(progress)
}).sync()
} catch {
print("Error: \(error)")
}
Add interceptors for all requests
import Net
let net = NetURLSession()
net.addRequestInterceptor { request in
request.addHeader("foo", value: "bar")
request.setBearerAuthorization(token: "token")
return request
}
Retry requests
import Net
let net = NetURLSession()
net.retryClosure = { response, _, _ in response?.statusCode == XXX }
do {
let task = try net.data("YOUR_URL").retry({ response, error, retryCount in
return retryCount < 2
}).sync()
} catch {
print("Error: \(error)")
}
🧙♂️ Codable
Encodable
import Net
let request = NetRequest.builder("YOUR_URL")!
.setJSONObject(Encodable())
.build()
Decodable
import Net
let net = NetURLSession()
do {
let object: Decodable = try net.data("YOUR_URL").sync().decode()
print("Response object: \(object)")
} catch {
print("Error: \(error)")
}
import Net
import Moya
let request = NetRequest("YOUR_URL")!
let provider = MoyaProvider<NetRequest>()
provider.request(request) { result in
switch result {
case let .success(response):
print("Response: \(response)")
case let .failure(error):
print("Error: \(error)")
}
}
import Net
let net = NetStub()
net.asyncBehavior = .delayed(.main, .seconds(10)) // If you want to delay the response.
net.nextResult = .response(NetResponse.builder()....build())
// Your test request here
net.nextResult = .error(.net(code: 500, message: "Your network error.", headers: ..., object: ..., underlying: ...))
// Your test request here
❤️ Etc.
Contributions are very welcome.
Attribution is appreciated (let’s spread the word!), but not mandatory.
Net is a versatile HTTP networking library written in Swift.
🌟 Features
📋 Requirements
📲 Installation
Net is available through CocoaPods. To install it, simply add the following line to your Podfile:
For Swift 3 compatibility use:
Or you can install it with Carthage:
Or install it with Swift Package Manager:
🐒 Usage
Build a NetRequest
Request asynchronously
Request synchronously
Request from cache
Track progress
Add interceptors for all requests
Retry requests
🧙♂️ Codable
Encodable
Decodable
🤝 Integrations
Love Alamofire?
Love Moya?
Love Kommander?
Love RxSwift?
Stub Implementation
❤️ Etc.
👨💻 Authors
alexruperez, alejandro.ruperez@intelygenz.com
👮♂️ License
Net is available under the MIT license. See the LICENSE file for more info.