Swiftpack.co - Package - halcyonmobile/RestBird

RestBird 🦉

Codacy Badge Build Status codecov

About

Lightweight, stateless REST network manager over the Codable protocol.

To learn more aboit this library, check out the documentation.

Requirements

  • iOS 9.0+ / macOS 10.11+
  • XCode 10.0+
  • Swift 4.2+

Features

  • [x] Codable support.
  • [x] Built-in Alamofire driver.
  • [x] Built-in URLSession driver.
  • [x] PromiseKit wrapper.
  • [ ] Automatic request parameter serialization (Codable?)

Driver coverage

The following table describes how much the specific drivers cover SessionManager defined in the Core of RestBird.

| | Coverage | |-------------------------|----------| | AlamofireSessionManager | 100% | | URLSessionManager | 70% |

Installation Instructions

Swift Package Manager

Add RestBird as a dependency to your project.

.Package(url: "https://github.com/halcyonmobile/RestBird.git", majorVersion: 0, minorVersion: 4)

You can use RestBird and implement your own session handling or use one of the built-in drivers implemented by RestBird (Alamofire and URLSession).

// Use RestBird without any driver
targets: [
    Target(name: "YourTarget", dependencies: ["RestBird"])
]

// Use the URLSession Driver
targets: [
    Target(name: "YourTarget", dependencies: [
        .product(name: "RestBird-URLSession", package: "RestBird")
    ])
]

// Use the Alamofire Driver
targets: [
    Target(name: "YourTarget", dependencies: [
        .product(name: "RestBird-Alamofire", package: "RestBird")
    ])
]

CocoaPods

# use RestBird without any driver
pod 'RestBird'

# use RestBird with Alamofire driver
pod 'RestBird/Alamofire'

# use RestBird with URLSession driver
pod 'RestBird/URLSession'

You can also try it out by running

pod try RestBird

Carthage

github "halcyonmobile/RestBird"

Setup

Steps for setting up the project for development:

  • Clone the repo
  • Generate Xcode project by executing swift package generate-xcodeproj
  • Open project

Usage

General

First you need to create your NetworkClientConfiguration configuration with your custom or one of the provided session manager drivers. We're going to use the AlamofireSessionManager.

struct MainAPIConfiguration: NetworkClientConfiguration {
    let baseUrl = "https://api.example.com"
    let sessionManager = AlamofireSessionManager()
}

Now we can pass this configuration to the network client.

let networkClient = NetworkClient(configuration: MainAPIConfiguration())

In order to make requests, a DataRequest object should be defined.

struct SignIn: DataRequest {
    typealias ResponseType = Authentication

    let email: String
    let password: String

    let suffix: String? = API.Path.login
    let method: HTTPMethod = .post
    var parameters: [String : Any]? {
        return [API.Param.email: email, API.Param.password: password]
    }
}

Now use your network client to execute requests.

let request = SignIn(email: "john-doe@acme.inc", password: "123456")
networkClient.execute(request: request, completion: { result: Result<Authentication> in
    print(result)
})

Middlewares

Middlewares are a powerful way to intercept network requests or react to the response the endpoint returns.

At the moment, middlewares fall under two categories:

  • Pre Middlewares (evaluated before the request is about to be executed)
  • Post Middlewares (evaluated after the request was executed)
struct LoggerMiddleware: PreMiddleware {
    func willPerform(_ request: URLRequest) throws {
        Logger.log(resquest)
    }
}

struct ErrorMiddleware: PostMiddleware {
    func didPerform(_ request: URLRequest, response: URLResponse, data: Data?) throws {
        if let data = data, let error = ErrorProvider.provide(for: data) {
            throw error
        }
    }
}

// Register middleware
networkClient.register(LoggerMiddleware())
networkClient.register(ErrorMiddleware())

Convenience

You can find convenience wrappers for RestBird which are not distributed through the package. This includes a PromiseKit wrapper.

Check out here.

License

RestBird is released under the MIT license. See LICENSE for details.

Github

link
Stars: 14
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

v0.4.1 - Feb 18, 2019

  • Add ability to customize parameter encoding for requests.
  • Add ability to upload multipart form data.
  • Add auto-generated Jazzy documentation.
  • Fix Middlewares not delegating back to the network client.

v0.4.0 - Feb 5, 2019

• Middlewares were introduced for request interception. • Removed NetworkLogger in favor of Middlewares.

v0.3.1 - Feb 5, 2019

• Validate AlamofireSessionManager requests.

v0.3.0 - Nov 10, 2018

  • Add JSON coding customization
  • Implement URL session manager

v0.2.1 - Oct 21, 2018

  • Fix ambiguity when calling execute(request:completion:)
  • Map header fields to the request inside the Alamofire driver