Latest 0.0.3
Homepage https://github.com/theddnc/iService
License MIT
Platforms ios 8.0
Dependencies iPromise
Authors

build
CocoaPods
CocoaPods
CocoaPods
CocoaPods

Communication with RESTful interfaces made simple.

Installation

Copy this line into your podfile:

pod 'iService', '~> 0.0'

Make sure to also add !use_frameworks

Description

iService provides two classes: Service which is a representation of a RESTful Service
and a ServiceRealm which is a container for shared request configuration.

Service

Service class provides a CRUD (Create, Retrieve, Update, Destroy) interface for interacting
with RESTful APIs. Each CRUD method returns a Promise from iPromise

let userService = Service(baseUrl: NSURL(string: "jsonplaceholder.typicode.com/users")!)

// does a GET on https://jsonplaceholder.typicode.com/users/1?
userService.retrieve("1").then({ result in
    let request = result.request    // NSURLRequest
    let response = result.response  // NSHTTPURLResponse
    let data = result.data          // NSData
    let error = result.error        // NSError
})

Services can register themselves in a Realm to use shared request cofiguration:

ServiceRealm.get("jsonplaceholder").register(userService)

// or 
ServiceRealm.register(userService, forRealmAtKey: "jsonplaceholder")

// or 
userService.registerForRealm(ServiceRealm.get("jsonplaceholder"))

Configuration from ServiceRealm can be overriden on a per-request basis.

let user = "{"userId": 1}".dataUsingEncoding(NSUTF8StringEncoding)!

// let's add some headers
userService
    .override({ configuration
        configuration.addHeader("Content-Type", withValue: "application/json")
    })
    .create(user)
    .then({ result in
        //...
    })

// this request will not inherit headers from previous call
userService
    .retrieve("1")
    .then({ result in
        //...
    })

ServiceRealm

ServiceRealm is a container for common configuration, e.g.:

  • request authorization
  • content types
  • accepted languages

ServiceRealm provides a map of CRUD-HTTP methods, that can be overriden:

static public let defaultCRUDMap: CRUDMap = [
    .CREATE:    .POST,
    .RETRIEVE:  .GET,
    .UPDATE:    .PUT,
    .DESTROY:   .DELETE
]

// override by calling
public func overrideCrudMap(map: CRUDMap) -> ServiceRealm

//or
public func overrideCrudMethod(method: Service.CRUDMethod, withHTTPMethod httpMethod: Service.HTTPMethod) -> ServiceRealm

HTTP headers can be configured globally, or per CRUD method:

public typealias HTTPHeaderDictionary = [String: String]

public func addHeader(header: String, withValue value: String) -> ServiceRealm 
public func addHeaders(headers: HTTPHeaderDictionary) -> ServiceRealm
public func addHeader(header: String, withValue value: String, specificForCRUDMethod crudMethod: Service.CRUDMethod) -> ServiceRealm
public func addHeaders(headers: HTTPHeaderDictionary, specificForCRUDMethod crudMethod: Service.CRUDMethod) -> ServiceRealm

Same goes for cache policy:

public func overrideGlobalCachePolicy(cachePolicy: NSURLRequestCachePolicy) -> ServiceRealm
public func overrideCachePolicy(cachePolicy: NSURLRequestCachePolicy, forCrudMethod method: Service.CRUDMethod) -> ServiceRealm

Configuration calls can be chained, so that full workflow looks like this:

let userService = Service(baseUrl: NSURL(string: "jsonplaceholder.typicode.com/users")!)

ServiceRealm.get("jsonplaceholder")
    .addHeaders([
        "Authorization": "Token sagsrbiusd90322sdf4f3gd4",
        "Content-Type": "application/json",
    ])
    .addHeader("Accept-Language", withValue: "en", specificForCRUDMethod: .RETRIEVE)
    .overrideGlobalCachePolicy(NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData)
    .register(userService)

userService.retrieve("1").then({ result in
    // ...
})

Documentation

Documentation is available here

Licence

See LICENCE

Latest podspec

{
    "name": "iService",
    "version": "0.0.3",
    "summary": "Communication with RESTful interfaces made easy",
    "description": "iService provides two classes that simplify communication with RESTful APIs.nService provides a CRUD (Create, Retrieve, Update, Destroy) interface, ServiceRealmnis a container for shared configuration.",
    "homepage": "https://github.com/theddnc/iService",
    "license": {
        "type": "MIT",
        "file": "LICENCE"
    },
    "authors": {
        "Jakub Zaczek": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/theddnc/iService.git",
        "tag": "0.0.3"
    },
    "source_files": "iService/*",
    "dependencies": {
        "iPromise": [
            "~> 1.1"
        ]
    }
}

Pin It on Pinterest

Share This