             return nil
         }
     }
-}
+    
+    // MARK: NSURLSessionDelegate
+    public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
+        completionHandler(.PerformDefaultHandling, nil)
+    }
+    
+    // MARK: NSURLSessionTaskDelegate
+    public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError connectionError: NSError?) {
+        if let dataTask = task as? NSURLSessionDataTask {
+            if let completionHandler = dataTask.completionHandler?.unbox {
+                completionHandler(dataTask.responseBuffer, dataTask.response, connectionError)
+            }
+        }
+    }
+
+    // MARK: NSURLSessionDataDelegate
+    public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
+        completionHandler(.Allow)
+    }
+    
+    public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
+        dataTask.responseBuffer.appendData(data)
+    }
+}


                }
                dispatch_async(mainQueue, { handler(mappedResponse) })
            }
            
            task.resume()


    case CONNECT = "CONNECT"
}

private var dataTaskResponseBufferKey = 0
private var dataTaskCompletionHandlerKey = 0

private extension NSURLSessionDataTask {
    private var responseBuffer: NSMutableData {
        if let responseBuffer = objc_getAssociatedObject(self, &dataTaskResponseBufferKey) as? NSMutableData {
            return responseBuffer
        } else {
            let responseBuffer = NSMutableData()
            objc_setAssociatedObject(self, &dataTaskResponseBufferKey, responseBuffer, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
            return responseBuffer
        }
    }
    
    private var completionHandler: Box<(NSData!, NSURLResponse!, NSError!) -> Void>? {
        get {
            return objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData!, NSURLResponse!, NSError!) -> Void>
        }
        
        set {
            objc_setAssociatedObject(self, &dataTaskCompletionHandlerKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }
}

// use private, global scope variable until we can use stored class var in Swift 1.2
private var instancePairDictionary = [String: (API, NSURLSession)]()
private let instancePairSemaphore = dispatch_semaphore_create(1)

public class API: NSObject, NSURLSessionDelegate, NSURLSessionDataDelegate {
    // configurations
    public class func baseURL() -> NSURL {
        fatalError("API.baseURL() must be overrided in subclasses.")
