Latest 1.0.0
Homepage https://github.com/anlaital/Alamorest
License MIT
Platforms ios 10.0
Dependencies Alamofire, PromisesSwift
Authors

Alamorest provides an easy way to interface with RESTful services using Alamofire and Promises.

Provides out-of-the box support for JSON and (optionally) Protobuf requests.

Requirements

  • iOS 10.0+
  • Swift 4.2+

Dependencies

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamorest into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'Alamorest'

To add support for Protobuf, include the following subspec in your Podfile:

pod 'Alamorest/Protobuf'

Usage

  1. Implement the API (struct based approach preferred)
struct JSONPlaceholderAPI {

    let server: Server

    init(baseURL: String) {
        server = Server(baseURL: URL(string: baseURL)!)
    }

    struct Post: Decodable {
        let body: String
        let id: Int
        let title: String
        let userId: Int
    }

    func posts() -> Promise<[Post]> {
        return server.json(path: "posts", method: .get)
    }

    func post(id: Int) -> Promise<Post> {
        return server.json(path: "posts/(id)", method: .get)
    }

    func createPost(title: String, body: String, userId: Int) -> Promise<Post> {
        struct Payload: Encodable {
            let title: String
            let body: String
            let userId: Int
        }
        let object = Payload(title: title, body: body, userId: userId)
        return server.json(object: object, path: "posts", method: .post)
    }

}
  1. Create a Server pointing to where the API resides
let server = Server(baseURL: URL("https://jsonplaceholder.typicode.com")!)
  1. Start making requests
let api = JSONPlaceholderAPI(server: server)

api.posts().then {
  print($0) // Prints out the posts.
}.catch {
  print($0) // Prints out the error.
}

Latest podspec

{
    "name": "Alamorest",
    "version": "1.0.0",
    "summary": "Alamorest provides an easy way to interface with RESTful services using Alamofire and Promises.",
    "homepage": "https://github.com/anlaital/Alamorest",
    "license": "MIT",
    "authors": {
        "Antti Laitala": "[email protected]"
    },
    "source": {
        "git": "https://github.com/anlaital/Alamorest.git",
        "tag": "v1.0.0"
    },
    "platforms": {
        "ios": "10.0"
    },
    "swift_version": "4.2",
    "source_files": "Alamorest/*.swift",
    "dependencies": {
        "Alamofire": [
            "~> 4.8.1"
        ],
        "PromisesSwift": [
            "~> 1.2.6"
        ]
    },
    "default_subspecs": "Lite",
    "subspecs": [
        {
            "name": "Lite"
        },
        {
            "name": "Protobuf",
            "dependencies": {
                "SwiftProtobuf": [
                    "~> 1.3.1"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This