Advertisement
Guest User

Clarizen Protoype Methods for SO Post

a guest
Feb 10th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Clarizen.prototype.convertToObject = function(json)
  2. {
  3.     console.log("convertToObject: " + json);
  4.     return JSON.parse(json);
  5. };
  6.  
  7. Clarizen.prototype.debug = function (message) {
  8.     console.log("clarizen object debug " + message);
  9. };
  10.  
  11. Clarizen.prototype.validateMethod = function (method) {
  12.     if (method === "GET" || method === "POST" || method === "HEAD") {
  13.         return true;
  14.     }
  15.     return false;
  16. };
  17.  
  18. Clarizen.prototype.request = function (options) {
  19.     var url = (options.url !== null) ? options.url : null;
  20.     var callback = (typeof options.callback === "function") ? options.callback : null;
  21.     var method = (options.method !== null) ? options.method : "GET";
  22.  
  23.     if (url === null)
  24.     { throw new Error("url missing"); }
  25.  
  26.     if (callback === null)
  27.     { throw new Error("callback is missing/not a function type"); }
  28.  
  29.     if (!Clarizen.prototype.validateMethod(options.method)) {
  30.         throw new Error("method can isnt supported currently " + method)
  31.     }
  32.  
  33.     var util = new Bradaz();
  34.     var xhr = util.createXHR(url, method, true);
  35.     xhr.onreadystatechange = function () {
  36.         if (xhr.readyState === 4 && xhr.status === 200) {
  37.             var response = callback(xhr.responseText);
  38.             return response;
  39.         }
  40.     };
  41.  
  42.     if (options.body !== null) {
  43.         xhr.send(options.body)
  44.     }
  45.     else {
  46.  
  47.         xhr.send();
  48.     }
  49. };
  50.  
  51. Clarizen.prototype.getSessionId = function(response)
  52. {
  53.     var loginResponse = JSON.parse(response);
  54.     console.log("getSessionId " + loginResponse.serverLocation);
  55.    
  56. //The followin ERRORS
  57.     var r = this.request (
  58.         {
  59.             url: this.serverLocation + ClarizenConstants.Urls.authentication,
  60.             method: "POST",
  61.             callback: this.convertToObject,
  62.             body: this.jsonUser
  63.         }
  64.         );  
  65. };
  66.  
  67. Clarizen.prototype.login = function () {
  68.  
  69.     var obj = new ClarizenObject();
  70.     var user = obj.factory({
  71.         type: 'User',
  72.         userName: "username",
  73.         password: "password"
  74.     });
  75.  
  76.     var jsonBody = JSON.stringify(user);
  77.     this.jsonUser = jsonBody;
  78.     var response = this.request (
  79.         {
  80.             url: ClarizenConstants.Urls.getServerDefinition,
  81.             method: "POST",
  82.             callback: this.getSessionId,
  83.             body: jsonBody
  84.         }
  85.         );  
  86. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement