iService 0.0.3

iService 0.0.3

LangLanguage SwiftSwift
License MIT
ReleasedLast Release Dec 2015
SPMSupports SPM

Maintained by Jakub Zaczek.

iService 0.0.3

  • By
  • Jakub Zaczek


Communication with RESTful interfaces made simple.


Copy this line into your podfile:

pod 'iService', '~> 0.0'

Make sure to also add !use_frameworks


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 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: "")!)

// does a GET on
userService.retrieve("1").then({ result in
    let request = result.request    // NSURLRequest
    let response = result.response  // NSHTTPURLResponse
    let data =          // NSData
    let error = result.error        // NSError

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


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

// or 

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

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

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

// this request will not inherit headers from previous call
    .then({ result in


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,
    .UPDATE:    .PUT,

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

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: "")!)

        "Authorization": "Token sagsrbiusd90322sdf4f3gd4",
        "Content-Type": "application/json",
    .addHeader("Accept-Language", withValue: "en", specificForCRUDMethod: .RETRIEVE)

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


Documentation is available here