function run (Query, endpoint) {
    var curl = "curl";
    //validate parameters
    try {
      //verify that the query object is valid
      if(!(Query instanceof Usergrid.Query)) {
        throw(new Error('Query is not a valid object.'));
      }
      //for timing, call start
      Query.setQueryStartTime();
      //peel the data out of the query object
      var method = Query.getMethod().toUpperCase();
      var path = Query.getResource();
      var jsonObj = Query.getJsonObj() || {};
      var params = Query.getQueryParams() || {};

      var formData = Query.getJsonObj();

      //method - should be GET, POST, PUT, or DELETE only
      if (method != 'GET' && method != 'POST' && method != 'PUT' && method != 'DELETE') {
        throw(new Error('Invalid method - should be GET, POST, PUT, or DELETE.'));
      }
      //curl - add the method to the command (no need to add anything for GET)
      if (method == "POST") {curl += " -X POST"; }
      else if (method == "PUT") { curl += " -X PUT"; }
      else if (method == "DELETE") { curl += " -X DELETE"; }
      else { curl += " -X GET"; }

      //curl - append the bearer token if this is not the sandbox app
      var application_name = Usergrid.ApiClient.getApplicationName();
      if (application_name) {
        application_name = application_name.toUpperCase();
      }
      //if (application_name != 'SANDBOX' && Usergrid.ApiClient.getToken()) {
      if ( Usergrid.ApiClient.getToken() ) { // || (getQueryType() == Usergrid.M && Usergrid.ApiClient.getToken())) {
        curl += ' -i -H "Authorization: Bearer ' + Usergrid.ApiClient.getToken() + '"';
        Query.setToken(true);
      }

      //params - make sure we have a valid json object
      _params = JSON.stringify(params);
      if (!JSON.parse(_params)) {
        throw(new Error('Params object is not valid.'));
      }

      //add in the cursor if one is available
      if (Query.getCursor()) {
        params.cursor = Query.getCursor();
      } else {
        delete params.cursor;
      }

      //strip off the leading slash of the endpoint if there is one
      endpoint = endpoint.indexOf('/') == 0 ? endpoint.substring(1) : endpoint;

      //add the endpoint to the path
      path = endpoint + path;

      //make sure path never has more than one / together
      if (path) {
        //regex to strip multiple slashes
        while(path.indexOf('//') != -1){
          path = path.replace('//', '/');
        }
      }

      //add the http:// bit on the front
      path = Usergrid.ApiClient.getApiUrl() + path;

      //curl - append the path
      curl += ' "' + path;

      //curl - append params to the path for curl prior to adding the timestamp
      var curl_encoded_params = encodeParams(params);
      if (curl_encoded_params) {
        curl += "?" + curl_encoded_params;
      }
      curl += '"';

      //add in a timestamp for gets and deletes - to avoid caching by the browser
      if ((method == "GET") || (method == "DELETE")) {
        params['_'] = new Date().getTime();
      }

      if (Usergrid.ApiClient.getToken() && Query.getToken()) {
        params['access_token'] = Usergrid.ApiClient.getToken();
      }

      //append params to the path
      var encoded_params = encodeParams(params);
      if (encoded_params) {
        path += "?" + encoded_params;
      }

      //jsonObj - make sure we have a valid json object
      jsonObj = JSON.stringify(jsonObj)
      if (!JSON.parse(jsonObj)) {
        throw(new Error('JSON object is not valid.'));
      }
      if (jsonObj == '{}') {
        jsonObj = null;
      } else {
        //curl - add in the json obj
        curl += " -d '" + jsonObj + "'";
      }

    } catch (e) {
      //parameter was invalid
      console.log('error occured running query -' + e.message);
      return false;
    }
    //log the curl command to the console
    console.log(curl);
    //store the curl command back in the object
    Query.setCurl(curl);

    var filePost = Query.getFilePost();
    
    //so far so good, so run the query
    var xD = window.XDomainRequest ? true : false;
    var xhr = getXHR(method, path, jsonObj, filePost);

    // Handle response.
    /*
    xhr.onerror = function() {
      //for timing, call end
      Query.setQueryEndTime();
      //for timing, log the total call time
      console.log(Query.getQueryTotalTime());
      //network error
      clearTimeout(timeout);
      console.log('API call failed at the network level.');
      //send back an error (best we can do with what ie gives back)
      Query.callFailureCallback(response.innerText);
    };*/
    xhr.xdomainOnload = function (response) {
      //for timing, call end
      Query.setQueryEndTime();
      //for timing, log the total call time
      console.log('Call timing: ' + Query.getQueryTotalTime());
      //call completed
      clearTimeout(timeout);
      //decode the response
      response = JSON.parse(xhr.responseText);
      //if a cursor was present, grab it
      try {
        var cursor = response.cursor || null;
        Query.saveCursor(cursor);
      }catch(e) {}
      Query.callSuccessCallback(response);
    };
    xhr.onload = function(response) {
      //for timing, call end
      Query.setQueryEndTime();
      //for timing, log the total call time
      console.log('Call timing: ' + Query.getQueryTotalTime());
      //call completed
      clearTimeout(timeout);
      //decode the response
      response = JSON.parse(xhr.responseText);
      if (xhr.status != 200 && !xD)   {
        //there was an api error
        try {
          var error = response.error;
          console.log('API call failed: (status: '+xhr.status+') ' + response.error_description);
          if ( (error == "auth_expired_session_token") ||
               (error == "auth_missing_credentials")   ||
               (error == "auth_unverified_oath")       ||
               (error == "expired_token")              ||
               (error == "unauthorized")               ||
               (error == "auth_invalid")) {
            //this error type means the user is not authorized. If a logout function is defined, call it
            console.log('Auth error: ' + error);
            callLogoutCallback();
          }
        } catch(e){}
        //otherwise, just call the failure callback
        Query.callFailureCallback(response.error);
        return;
      } else {
        //query completed succesfully, so store cursor
        var cursor = response.cursor || null;
        Query.saveCursor(cursor);
        //then call the original callback
        Query.callSuccessCallback(response);
     }
    };

    var timeout = setTimeout(
      function() {
        xhr.abort();
        if ( typeof(Usergrid.ApiClient.getCallTimeoutCallback()) === 'function') {
          Usergrid.ApiClient.callTimeoutCallback('API CALL TIMEOUT');
        } else if (typeof(Query.getFailureCallback()) === 'function'){
          Query.callFailureCallback('API CALL TIMEOUT');
        }
      },
      Usergrid.ApiClient.getCallTimeout()); //set for 30 seconds


    if (filePost) {
      xhr.send(formData);
    } else {
      xhr.send(jsonObj);
    }
  }
/**
   *  A private method to validate, prepare,, and make the calls to the API
   *  Use runAppQuery or runManagementQuery to make your calls!
   *
   *  @method run
   *  @private
   *  @params {object} Usergrid.Query - {method, path, jsonObj, params, successCallback, failureCallback}
   *  @params {string} endpoint - used to differentiate between management and app queries
   *  @return {response} callback functions return API response object
   */
A private method to validate, prepare,, and make the calls to the API
Use runAppQuery or runManagementQuery to make your calls!


A private method to validate prepare and make the calls to the API Use runAppQuery or runManagementQuery to make your calls!
function run (Query, endpoint) {
    var curl = "curl";
    try {
      if(!(Query instanceof Usergrid.Query)) {
        throw(new Error('Query is not a valid object.'));
      }
      Query.setQueryStartTime();
      var method = Query.getMethod().toUpperCase();
      var path = Query.getResource();
      var jsonObj = Query.getJsonObj() || {};
      var params = Query.getQueryParams() || {};
      var formData = Query.getJsonObj();
      if (method != 'GET' && method != 'POST' && method != 'PUT' && method != 'DELETE') {
        throw(new Error('Invalid method - should be GET, POST, PUT, or DELETE.'));
      }
      if (method == "POST") {curl += " -X POST"; }
      else if (method == "PUT") { curl += " -X PUT"; }
      else if (method == "DELETE") { curl += " -X DELETE"; }
      else { curl += " -X GET"; }
      var application_name = Usergrid.ApiClient.getApplicationName();
      if (application_name) {
        application_name = application_name.toUpperCase();
      }
      if ( Usergrid.ApiClient.getToken() ) { 
        curl += ' -i -H "Authorization: Bearer ' + Usergrid.ApiClient.getToken() + '"';
        Query.setToken(true);
      }
      _params = JSON.stringify(params);
      if (!JSON.parse(_params)) {
        throw(new Error('Params object is not valid.'));
      }
      if (Query.getCursor()) {
        params.cursor = Query.getCursor();
      } else {
        delete params.cursor;
      }
      endpoint = endpoint.indexOf('/') == 0 ? endpoint.substring(1) : endpoint;
      path = endpoint + path;
      if (path) {
        while(path.indexOf('//') != -1){
          path = path.replace('//', '/');
        }
      }
      path = Usergrid.ApiClient.getApiUrl() + path;
      curl += ' "' + path;
      var curl_encoded_params = encodeParams(params);
      if (curl_encoded_params) {
        curl += "?" + curl_encoded_params;
      }
      curl += '"';
      if ((method == "GET") || (method == "DELETE")) {
        params['_'] = new Date().getTime();
      }
      if (Usergrid.ApiClient.getToken() && Query.getToken()) {
        params['access_token'] = Usergrid.ApiClient.getToken();
      }
      var encoded_params = encodeParams(params);
      if (encoded_params) {
        path += "?" + encoded_params;
      }
      jsonObj = JSON.stringify(jsonObj)
      if (!JSON.parse(jsonObj)) {
        throw(new Error('JSON object is not valid.'));
      }
      if (jsonObj == '{}') {
        jsonObj = null;
      } else {
        curl += " -d '" + jsonObj + "'";
      }
    } catch (e) {
      console.log('error occured running query -' + e.message);
      return false;
    }
    console.log(curl);
    Query.setCurl(curl);
    var filePost = Query.getFilePost();
    var xD = window.XDomainRequest ? true : false;
    var xhr = getXHR(method, path, jsonObj, filePost);
    xhr.xdomainOnload = function (response) {
      Query.setQueryEndTime();
      console.log('Call timing: ' + Query.getQueryTotalTime());
      clearTimeout(timeout);
      response = JSON.parse(xhr.responseText);
      try {
        var cursor = response.cursor || null;
        Query.saveCursor(cursor);
      }catch(e) {}
      Query.callSuccessCallback(response);
    };
    xhr.onload = function(response) {
      Query.setQueryEndTime();
      console.log('Call timing: ' + Query.getQueryTotalTime());
      clearTimeout(timeout);
      response = JSON.parse(xhr.responseText);
      if (xhr.status != 200 && !xD)   {
        try {
          var error = response.error;
          console.log('API call failed: (status: '+xhr.status+') ' + response.error_description);
          if ( (error == "auth_expired_session_token") ||
               (error == "auth_missing_credentials")   ||
               (error == "auth_unverified_oath")       ||
               (error == "expired_token")              ||
               (error == "unauthorized")               ||
               (error == "auth_invalid")) {
            console.log('Auth error: ' + error);
            callLogoutCallback();
          }
        } catch(e){}
        Query.callFailureCallback(response.error);
        return;
      } else {
        var cursor = response.cursor || null;
        Query.saveCursor(cursor);
        Query.callSuccessCallback(response);
     }
    };
    var timeout = setTimeout(
      function() {
        xhr.abort();
        if ( typeof(Usergrid.ApiClient.getCallTimeoutCallback()) === 'function') {
          Usergrid.ApiClient.callTimeoutCallback('API CALL TIMEOUT');
        } else if (typeof(Query.getFailureCallback()) === 'function'){
          Query.callFailureCallback('API CALL TIMEOUT');
        }
      },
      Usergrid.ApiClient.getCallTimeout()); 
    if (filePost) {
      xhr.send(formData);
    } else {
      xhr.send(jsonObj);
    }
  }
function run ( Query , endpoint ) { var curl = "curl" ; try { if ( ! ( Query instanceof Usergrid . Query ) ) { throw ( new Error ( 'Query is not a valid object.' ) ) ; } Query . setQueryStartTime ( ) ; var method = Query . getMethod ( ) . toUpperCase ( ) ; var path = Query . getResource ( ) ; var jsonObj = Query . getJsonObj ( ) || { } ; var params = Query . getQueryParams ( ) || { } ; var formData = Query . getJsonObj ( ) ; if ( method != 'GET' && method != 'POST' && method != 'PUT' && method != 'DELETE' ) { throw ( new Error ( 'Invalid method - should be GET, POST, PUT, or DELETE.' ) ) ; } if ( method == "POST" ) { curl += " -X POST" ; } else if ( method == "PUT" ) { curl += " -X PUT" ; } else if ( method == "DELETE" ) { curl += " -X DELETE" ; } else { curl += " -X GET" ; } var application_name = Usergrid . ApiClient . getApplicationName ( ) ; if ( application_name ) { application_name = application_name . toUpperCase ( ) ; } if ( Usergrid . ApiClient . getToken ( ) ) { curl += ' -i -H "Authorization: Bearer ' + Usergrid . ApiClient . getToken ( ) + '"' ; Query . setToken ( true ) ; } _params = JSON . stringify ( params ) ; if ( ! JSON . parse ( _params ) ) { throw ( new Error ( 'Params object is not valid.' ) ) ; } if ( Query . getCursor ( ) ) { params . cursor = Query . getCursor ( ) ; } else { delete params . cursor ; } endpoint = endpoint . indexOf ( '/' ) == 0 ? endpoint . substring ( 1 ) : endpoint ; path = endpoint + path ; if ( path ) { while ( path . indexOf ( '//' ) != - 1 ) { path = path . replace ( '//' , '/' ) ; } } path = Usergrid . ApiClient . getApiUrl ( ) + path ; curl += ' "' + path ; var curl_encoded_params = encodeParams ( params ) ; if ( curl_encoded_params ) { curl += "?" + curl_encoded_params ; } curl += '"' ; if ( ( method == "GET" ) || ( method == "DELETE" ) ) { params [ '_' ] = new Date ( ) . getTime ( ) ; } if ( Usergrid . ApiClient . getToken ( ) && Query . getToken ( ) ) { params [ 'access_token' ] = Usergrid . ApiClient . getToken ( ) ; } var encoded_params = encodeParams ( params ) ; if ( encoded_params ) { path += "?" + encoded_params ; } jsonObj = JSON . stringify ( jsonObj ) if ( ! JSON . parse ( jsonObj ) ) { throw ( new Error ( 'JSON object is not valid.' ) ) ; } if ( jsonObj == '{}' ) { jsonObj = null ; } else { curl += " -d '" + jsonObj + "'" ; } } catch ( e ) { console . log ( 'error occured running query -' + e . message ) ; return false ; } console . log ( curl ) ; Query . setCurl ( curl ) ; var filePost = Query . getFilePost ( ) ; var xD = window . XDomainRequest ? true : false ; var xhr = getXHR ( method , path , jsonObj , filePost ) ; xhr . xdomainOnload = function ( response ) { Query . setQueryEndTime ( ) ; console . log ( 'Call timing: ' + Query . getQueryTotalTime ( ) ) ; clearTimeout ( timeout ) ; response = JSON . parse ( xhr . responseText ) ; try { var cursor = response . cursor || null ; Query . saveCursor ( cursor ) ; } catch ( e ) { } Query . callSuccessCallback ( response ) ; } ; xhr . onload = function ( response ) { Query . setQueryEndTime ( ) ; console . log ( 'Call timing: ' + Query . getQueryTotalTime ( ) ) ; clearTimeout ( timeout ) ; response = JSON . parse ( xhr . responseText ) ; if ( xhr . status != 200 && ! xD ) { try { var error = response . error ; console . log ( 'API call failed: (status: ' + xhr . status + ') ' + response . error_description ) ; if ( ( error == "auth_expired_session_token" ) || ( error == "auth_missing_credentials" ) || ( error == "auth_unverified_oath" ) || ( error == "expired_token" ) || ( error == "unauthorized" ) || ( error == "auth_invalid" ) ) { console . log ( 'Auth error: ' + error ) ; callLogoutCallback ( ) ; } } catch ( e ) { } Query . callFailureCallback ( response . error ) ; return ; } else { var cursor = response . cursor || null ; Query . saveCursor ( cursor ) ; Query . callSuccessCallback ( response ) ; } } ; var timeout = setTimeout ( function ( ) { xhr . abort ( ) ; if ( typeof ( Usergrid . ApiClient . getCallTimeoutCallback ( ) ) === 'function' ) { Usergrid . ApiClient . callTimeoutCallback ( 'API CALL TIMEOUT' ) ; } else if ( typeof ( Query . getFailureCallback ( ) ) === 'function' ) { Query . callFailureCallback ( 'API CALL TIMEOUT' ) ; } } , Usergrid . ApiClient . getCallTimeout ( ) ) ; if ( filePost ) { xhr . send ( formData ) ; } else { xhr . send ( jsonObj ) ; } }
A private method to validate, prepare,, and make the calls to the API
  Use runAppQuery or runManagementQuery to make your calls!
A private method to validate prepare and make the calls to the API Use runAppQuery or runManagementQuery to make your calls!
//validate parameters
 //verify that the query object is valid
 //for timing, call start
 //peel the data out of the query object
 //method - should be GET, POST, PUT, or DELETE only
 //curl - add the method to the command (no need to add anything for GET)
 //curl - append the bearer token if this is not the sandbox app
 //if (application_name != 'SANDBOX' && Usergrid.ApiClient.getToken()) {
 // || (getQueryType() == Usergrid.M && Usergrid.ApiClient.getToken())) {
 //params - make sure we have a valid json object
 //add in the cursor if one is available
 //strip off the leading slash of the endpoint if there is one
 //add the endpoint to the path
 //make sure path never has more than one / together
 //regex to strip multiple slashes
 //add the http:// bit on the front
 //curl - append the path
 //curl - append params to the path for curl prior to adding the timestamp
 //add in a timestamp for gets and deletes - to avoid caching by the browser
 //append params to the path
 //jsonObj - make sure we have a valid json object
 //curl - add in the json obj
 //parameter was invalid
 //log the curl command to the console
 //store the curl command back in the object
 //so far so good, so run the query
 // Handle response.
 /*
    xhr.onerror = function() {
      //for timing, call end
      Query.setQueryEndTime();
      //for timing, log the total call time
      console.log(Query.getQueryTotalTime());
      //network error
      clearTimeout(timeout);
      console.log('API call failed at the network level.');
      //send back an error (best we can do with what ie gives back)
      Query.callFailureCallback(response.innerText);
    };*/ //for timing, call end
 //for timing, log the total call time
 //call completed
 //decode the response
 //if a cursor was present, grab it
 //for timing, call end
 //for timing, log the total call time
 //call completed
 //decode the response
 //there was an api error
 //this error type means the user is not authorized. If a logout function is defined, call it
 //otherwise, just call the failure callback
 //query completed succesfully, so store cursor
 //then call the original callback
 //set for 30 seconds
