NetClient-iOS

Versatile HTTP networking library written in Swift 3.

View project on GitHub

Net

Twitter Version License Platform Carthage compatible Swift Package Manager Compatible Build Status

Net

Net is a versatile HTTP networking library written in Swift 3.

Features

  • [x] URL / JSON / Property List Parameter Encoding
  • [x] Upload File / Data / Stream / Multipart Form Data
  • [x] Download File using Request or Resume Data
  • [x] Authentication with URLCredential
  • [x] Basic, Bearer and Custom Authorization Handling
  • [x] Default and Custom Cache Controls
  • [x] Default and Custom Content Types
  • [x] Upload and Download Progress Closures with Progress
  • [x] cURL Command Debug Output
  • [x] Request and Response Interceptors
  • [x] Asynchronous and synchronous task execution
  • [x] Inference of response object type
  • [x] Network reachability
  • [x] Retry requests
  • [x] watchOS Compatible
  • [x] tvOS Compatible
  • [x] macOS Compatible
  • [x] Alamofire Implementation
  • [x] MoyaProvider Compatible

Requirements

  • iOS 8.0+ / macOS 10.9+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.1+
  • Swift 3.0+

Installation

Net is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'NetClient'

Or you can install it with Carthage:

github "intelygenz/NetClient-iOS"

Or install it with Swift Package Manager:

dependencies: [
    .Package(url: "https://github.com/intelygenz/NetClient-iOS.git")
]

Usage

Build a NetRequest

let request = 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])
            .setBasicAuthorization(user: "user", password: "password")
            .setHeaders(["foo": "bar"])
            .build()

Request asynchronously

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

Love Alamofire?

pod 'NetClient/Alamofire'
import Net

let net = NetAlamofire()

...

Love Moya?

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

Etc.

  • Contributions are very welcome.
  • Attribution is appreciated (let’s spread the word!), but not mandatory.

Authors

alexruperez, alejandro.ruperez@intelygenz.com

License

Net is available under the MIT license. See the LICENSE file for more info.