 public typealias stringcompletion = (success:Bool, response:Response<String, NSError>?, json:SwiftyJSON.JSON?, error:NSError!) -> Void
 public typealias paramType = [String: AnyObject]?
 
+
+private let waterwheelErrorString = "waterhwheel error: "
+
 /**
  Responsible for storing state and variables for waterwheel.
  */

        "Content-Type":"application/json",
        "Accept":"application/json",
        ]
    public var URL : String
    public let endpoint : String
    public var basicUsername : String
    public var basicPassword : String
    public var CSRFToken : String
    public var signRequestsBasic : Bool = false
    public var signCSRFToken : Bool = false

    // MARK: - Main

    /**
     Initializes the `waterwheel` instance with the our defaults.

     - returns: The new `waterwheel` instance.
     */
    public init() {
        self.URL =  ""
        self.basicUsername = ""
        self.basicPassword = ""
        self.signRequestsBasic = false
        self.CSRFToken = ""
        self.signCSRFToken = false;
        self.endpoint = ""

    }
}

public func setDrupalURL(drupalURL: String) {
    waterwheelManager.sharedInstance.URL = drupalURL;
}
/**
 Allows a username and password to be set for Basic Auth
 */
public func setBasicAuthUsernameAndPassword(username:String, password:String) {
    waterwheelManager.sharedInstance.basicUsername = username
    waterwheelManager.sharedInstance.basicPassword = password
    waterwheelManager.sharedInstance.signRequestsBasic = true
}

/**
 Login with user-login-form and application/x-www-form-urlencoded
 Since this request is very unique, we customize it here, and this will change when CORE gets a real API login method.

 - parameter username:          The username to login with.
 - parameter entityId:          The password to login with
 - parameter completionHandler: A completion handler that your delegate method should call if you want the response.


 */
public func loginWithUserLoginForm(username:String, password:String, completionHandler:stringcompletion) {
    let urlString = waterwheelManager.sharedInstance.URL + "/user/login"
    let body = [
        "name":username,
        "pass":password,
        "form_id":"user_login_form",
        ]
    var headers = [
        "Content-Type":"application/x-www-form-urlencoded",
        ]

    // Fetch Request
    Alamofire.request(.POST, urlString, headers: headers, parameters: body, encoding: .URL)
        .validate(statusCode: 200..<300)
        .responseString { response in
            if (response.result.error == nil) {
                getCSRFToken({ (success, response, json, error) in
                    if (success) {
                        completionHandler(success: true, response: response, json: nil, error: nil)

                })
            }
            else {
                waterwheel.setIsLoggedIn(false)
                completionHandler(success: false, response: response, json: nil, error: response.result.error)
            }
    }
}



/**
 Logout a user

 - parameter completionHandler: A completion handler that your delegate method should call if you want the response.

 */

public func logout(completionHandler:stringcompletion) {
    if waterwheelManager.sharedInstance.signRequestsBasic  {
        waterwheelManager.sharedInstance.basicUsername = ""
        waterwheelManager.sharedInstance.basicPassword = ""
        waterwheelManager.sharedInstance.signRequestsBasic = false
        return
    }
    let urlString = waterwheelManager.sharedInstance.URL + "/user/logout"
    Alamofire.request(.GET, urlString)
        .validate(statusCode: 200..<300)
        .responseString { response in
            if (response.result.error == nil) {
                waterwheelManager.sharedInstance.CSRFToken = ""
                waterwheelManager.sharedInstance.signCSRFToken = false
                completionHandler(success: true, response: response, json: nil, error: response.result.error)
            }
            else {
                completionHandler(success: false, response: response, json: nil, error: response.result.error)
            }
    }

}

/**
 
 Private function to get the CSRF Token
 
 */
private func getCSRFToken(completionHandler:stringcompletion) {
    let urlString = waterwheelManager.sharedInstance.URL + "/rest/session/token"
    Alamofire.request(.GET, urlString)
        .validate(statusCode: 200..<300)
