πŸƒ A versatile HTTP(s) networking framework written in Swift.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Assets readme May 10, 2018
Leaf 1.2.0 May 24, 2018
Sample 1.2.0 May 24, 2018
.gitignore 1.0.0 May 10, 2018
LICENSE.md
Leaf.podspec
Leaf.swift 1.0.0 May 10, 2018
Package.swift 1.0.0 May 10, 2018
README.adoc README.adoc May 24, 2018
_config.yml 1.0.0 May 10, 2018

README.adoc

Meet Leaf

Leaf

Author EMail MIT
Version Platforms Swift
Build Passing Cocoapods Carthage SPM

🏡 Introduction

Leaf is a versatile HTTP(s) networking framework written in Swift.

🌟 Features

  • βœ“ Chainable Request / Response Methods

  • βœ“ Asynchronous & synchronous task execution

  • βœ“ Basic, Bearer & Custom Authorization Handling

  • βœ“ URL / JSON / Property List Parameter Encoding

  • βœ“ Upload File / Data / Stream / Multipart Form Data

  • βœ“ Download File using Request / Resume Data

  • βœ“ Authentication with URLCredential

  • βœ“ Custom Cache Controls

  • βœ“ Custom Content Types

  • βœ“ Upload & Download Progress Closures

  • βœ“ cURL Command Debug Output

  • βœ“ Request & Response Interceptors

  • βœ“ Inference of response object type

  • βœ“ Network reachability

  • βœ“ TLS Certificate & Public Key Pinning

  • βœ“ Retry requests

  • βœ“ Codable protocols compatible (JSON / Property List)

  • βœ“ watchOS Compatible

  • βœ“ tvOS Compatible

  • βœ“ macOS Compatible

πŸ“‹ Requirements

Type Requirement

Platform

iOS

8.0

macOS

10.9

tvOS

9.0

watchOS

2.0

Linux

N/A

IDE

Xcode

9.3

Language

Swift

4.1

πŸ“² Installation

CocoaPods

Leaf is available on CocoaPods.

use_frameworks!
pod 'Leaf'

Manually

Copy all files in the Leaf directory into your project.

πŸ›Œ Dependency

N/A

❀️ Contribution

You are welcome to fork and submit pull requests.

πŸ”– License

Leaf is open-sourced software, licensed under the MIT license.

πŸ”« Usage

import Leaf

do {
    let url = URL(string: "https://domain.com/someapi")!
    try Leaf.init(u, parameters: ["lan": "en-US"]).async(.GET, progress: { (progress) in
        // ...
    }, success: { (response) in
        // ...
    }, failure: { (error) in
        // ...
    })
} catch {
    // ...
}

βš™οΈ Advanced

Build a LeafRequest

import Leaf

do {
    let request = try LeafRequest.builder("https://domain.com/someapi")!
                .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()
} catch {

}

Request asynchronously

import Leaf

let Leaf = LeafURLSession()

Leaf.dataTask(URL(string: "https://domain.com/someapi")!).async(success: { (response) in

}, failure: { (error) in

})

Request synchronously

import Leaf

let Leaf = LeafURLSession()

do {
    let object: [AnyHashable: Any] = try Leaf.dataTask("https://domain.com/someapi").sync().object()
} catch {

}

Request from cache

import Leaf

let Leaf = LeafURLSession()

do {
    let object: [AnyHashable: Any] = try Leaf.dataTask("https://domain.com/someapi").cached().object()

} catch {

}

Track progress

import Leaf

let Leaf = LeafURLSession()

do {
    let task = try Leaf.dataTask("https://domain.com/someapi").progress({ progress in
    }).sync()
} catch {

}

Add interceptors for all requests

import Leaf

let Leaf = LeafURLSession()

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

Retry requests

import Leaf

let Leaf = LeafURLSession()

Leaf.retryClosure = { response, _, _ in response?.statusCode == XXX }

do {
    let task = try Leaf.dataTask("https://domain.com/someapi").retry({ response, error, retryCount in
        return retryCount < 2
    }).sync()
} catch {

}

πŸ§™β€β™‚οΈ Codable

Encodable

import Leaf

let request = LeafRequest.builder("https://domain.com/someapi")!
            .setJSONObject(Encodable())
            .build()

Decodable

import Leaf

let Leaf = URLSession()

do {
    let object: Decodable = try Leaf.dataTask("https://domain.com/someapi").sync().decode()

} catch {

}