mherod
9/12/2016 - 11:23 AM

AlamofireBrightFutures.swift

//
//  AlamofireBrightFutures.swift
//
//  Created by Matthew Herod on 27/04/2016.
//  Copyright © 2016. All rights reserved.
//

import BrightFutures
import Alamofire
import ObjectMapper
import AlamofireObjectMapper

extension Alamofire.DataRequest {

    func responseStringFuture(promise: BrightFutures.Promise<String, NSError>) -> Self {
        return self.responseString { response in
            switch (response.result) {
            case let .failure(error): promise.tryFailure(error as NSError); return
            case let .success(value): promise.trySuccess(value); return
            }
        }
    }

    func responseObjectFuture<T:Mappable>(promise: BrightFutures.Promise<T, NSError>) -> Self {
        return self.responseObject { (response: Alamofire.DataResponse<T>) in
            switch (response.result) {
            case let .failure(error): promise.tryFailure(error as NSError); return
            case let .success(model): promise.trySuccess(model as T); return
            }
        }
    }

    func responseArrayFuture<T:Mappable>(promise: BrightFutures.Promise<[T], NSError>) -> Self {
        return self.responseArray { (response: Alamofire.DataResponse<[T]>) in
            switch (response.result) {
            case let .failure(error): promise.tryFailure(error as NSError); return
            case let .success(model): promise.trySuccess(model as [T]); return
            }
        }
    }
}