Versatile HTTP Networking in Swift
Net is a versatile HTTP networking library written in Swift.
Net is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'NetClient'
For Swift 3 compatibility use:
pod 'NetClient', '~> 0.2'
github "intelygenz/NetClient-iOS"
dependencies: [
.package(url: "https://github.com/intelygenz/NetClient-iOS.git")
]
import Net
do {
let request = try NetRequest.builder("YOUR_URL")!
.setAccept(.json)
.setCache(.reloadIgnoringLocalCacheData)
.setMethod(.PATCH)
.setTimeout(20)
.setJSONBody(["foo", "bar"])
.setContentType(.json)
.setServiceType(.background)
.setCacheControls([.maxAge(500)])
.setURLParameters(["foo": "bar"])
.setAcceptEncodings([.gzip, .deflate])
.setContentEncodings([.gzip])
.setBasicAuthorization(user: "user", password: "password")
.setHeaders(["foo": "bar"])
.build()
} catch {
print("Request error: \(error)")
}
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)")
}
}
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)")
}
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)")
}
import Net
let net = NetURLSession()
do {
let task = try net.data("YOUR_URL").progress({ progress in
print(progress)
}).sync()
} catch {
print("Error: \(error)")
}
import Net
let net = NetURLSession()
net.addRequestInterceptor { request in
request.addHeader("foo", value: "bar")
request.setBearerAuthorization(token: "token")
return request
}
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)")
}
import Net
let request = NetRequest.builder("YOUR_URL")!
.setJSONObject(Encodable())
.build()
import Net
let net = NetURLSession()
do {
let object: Decodable = try net.data("YOUR_URL").sync().decode()
print("Response object: \(object)")
} catch {
print("Error: \(error)")
}
pod 'NetClient/Alamofire'
import Net
let net = NetAlamofire()
...
pod 'NetClient/Moya'
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)")
}
}
pod 'NetClient/Kommander'
import Net
import Kommander
let net = NetURLSession()
let kommander = Kommander.default
net.data(URL(string: "YOUR_URL")!).execute(by: kommander, onSuccess: { object in
print("Response dictionary: \(object as [AnyHashable: Any])")
}) { error in
print("Error: \(String(describing: error?.localizedDescription))")
}
net.data(URL(string: "YOUR_URL")!).executeDecoding(by: kommander, onSuccess: { object in
print("Response object: \(object as Decodable)")
}) { error in
print("Error: \(String(describing: error?.localizedDescription))")
}
pod 'NetClient/RxSwift'
import Net
import RxSwift
let request = NetRequest("YOUR_URL")!
_ = net.data(request).rx.response().observeOn(MainScheduler.instance).subscribe { print($0) }
pod 'NetClient/Stub'
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
alexruperez, [email protected]
Net is available under the MIT license. See the LICENSE file for more info.