NetClient 0.3.5

NetClient 0.3.5

TestsTested โœ“
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2017
SwiftSwift Version 4.0
SPMSupports SPM โœ“

Maintained by alexruperez.


Downloads

Total1,238
Week25
Month161

Installs

Apps27
Apps WeekApps This Week 7
Test Targets14
Test WeekTests This week 3
powered by Segment

GitHub

Stars48
Watchers8
Forks3
Issues1
Contributors1
Pull Requests0

Code

Files37
LOCLines of Code 2,897



NetClient 0.3.5

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

Net is a versatile HTTP networking library written in Swift.

๐ŸŒŸ Features

  • URL / JSON / Property List Parameter Encoding
  • Upload File / Data / Stream / Multipart Form Data
  • Download File using Request or Resume Data
  • Authentication with URLCredential
  • Basic, Bearer and Custom Authorization Handling
  • Default and Custom Cache Controls
  • Default and Custom Content Types
  • Upload and Download Progress Closures with Progress
  • cURL Command Debug Output
  • Request and Response Interceptors
  • Asynchronous and synchronous task execution
  • Inference of response object type
  • Network reachability
  • TLS Certificate and Public Key Pinning
  • Retry requests
  • Codable / Decodable / Encodable protocols compatible (JSON / Property List)
  • watchOS Compatible
  • tvOS Compatible
  • macOS Compatible
  • Alamofire Implementation
  • MoyaProvider Extension
  • Kommander Extension

๐Ÿ“‹ Requirements

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

๐Ÿ“ฒ Installation

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'

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

import Net

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.shared

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.shared

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.shared

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.shared

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.shared

net.addRequestInterceptor { request in
    request.addHeader("foo", value: "bar")
    request.setBearerAuthorization(token: "token")
    return request
}

Retry requests

import Net

let net = NetURLSession.shared

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.shared

do {
    let object: Decodable = try net.data("YOUR_URL").sync().decode()
    print("Response object: \(object)")
} catch {
    print("Error: \(error)")
}

๐Ÿค Integrations

Love Alamofire?

pod 'NetClient/Alamofire'
import Net

let net = NetAlamofire.shared

...

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

Love Kommander?

pod 'NetClient/Kommander'
import Net
import Kommander

let net = NetURLSession.shared
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))")
}

โค๏ธ Etc.

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

๐Ÿ‘จโ€๐Ÿ’ป Authors

alexruperez, [email protected]

๐Ÿ‘ฎโ€โ™‚๏ธ License

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