Swiftpack.co - Package - slightair/GarageClientSwift

GarageClientSwift

Swift client library for the Garage application API

Requirements

  • Swift 3
  • Mac OS 10.10+
  • iOS 8.0+
  • watchOS 2.0+
  • tvOS 9.0+

Installation

Carthage

  1. Add github "slightair/GarageClientSwift" ~> 2.0 to Cartfile
  2. Run carthage update

Usage

1. Define Garage resource model

struct User: Decodable {
    let id: Int
    let name: String
    let email: String

    static func decode(_ e: Extractor) throws -> User {
        return try User(
            id: e <| "id",
            name: e <| "name",
            email: e <| "email"
        )
    }
}

2. Define Garage request

struct GetUsersRequest: GarageRequest {
    typealias Resource = [User]

    var method: HTTPMethod {
        return .get
    }

    var path: String {
        return "/users"
    }

    var queryParameters: [String: Any]? {
        return [
            "per_page": 1,
            "page": 2,
        ]
    }
}

3. Define Garage configuration

struct Configuration: GarageConfiguration {
    let endpoint: URL
    let accessToken: String
}

let configuration = Configuration(
    endpoint: URL(string: "http://localhost:3000")!,
    accessToken: "YOUR ACCESS TOKEN"
)

4. Send request

let garageClient = GarageClient(configuration: configuration)
garageClient.sendRequest(GetUsersRequest()) { result in
    switch result {
    case .success(let response):
        debugPrint(response)

        let users = response.resource
        debugPrint(users)
    case .failure(let error):
        debugPrint(error)
    }
}

License

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

Github

link
Stars: 15
Help us keep the lights on

Used By

Total: 0

Releases

2.1.0 - Jan 10, 2017

  • Support Himotoki Decodable rootKeyPath - https://github.com/slightair/GarageClientSwift/pull/3
    • Thanks! 😸 @hatuyuki4

2.0.0 - Dec 13, 2016

  • Swift 3 support

1.1.0 - Jun 9, 2016

  • Use queryParameters, bodyParameters

1.0.0 - Jun 8, 2016

  • Initial public release.