Files
luos/workflow/public_html/lib/js/restclient-min.js
2013-11-26 17:20:18 -04:00

1 line
12 KiB
JavaScript

var RCBase64={keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t,r,s,i,o,n,a,h="",u=0;for(e=this.utf8_encode(e);u<e.length;)t=e.charCodeAt(u++),r=e.charCodeAt(u++),s=e.charCodeAt(u++),i=t>>2,o=(3&t)<<4|r>>4,n=(15&r)<<2|s>>6,a=63&s,isNaN(r)?n=a=64:isNaN(s)&&(a=64),h=h+this.keyStr.charAt(i)+this.keyStr.charAt(o)+this.keyStr.charAt(n)+this.keyStr.charAt(a);return h},decode:function(e){var t,r,s,i,o,n,a,h="",u=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");u<e.length;)i=this.keyStr.indexOf(e.charAt(u++)),o=this.keyStr.indexOf(e.charAt(u++)),n=this.keyStr.indexOf(e.charAt(u++)),a=this.keyStr.indexOf(e.charAt(u++)),t=i<<2|o>>4,r=(15&o)<<4|n>>2,s=(3&n)<<6|a,h+=String.fromCharCode(t),64!==n&&(h+=String.fromCharCode(r)),64!==a&&(h+=String.fromCharCode(s));return h=this.utf8_decode(h)},utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t,r,s="";for(t=0;t<e.length;t++)r=e.charCodeAt(t),128>r?s+=String.fromCharCode(r):r>127&&2048>r?(s+=String.fromCharCode(192|r>>6),s+=String.fromCharCode(128|63&r)):(s+=String.fromCharCode(224|r>>12),s+=String.fromCharCode(128|63&r>>6),s+=String.fromCharCode(128|63&r));return s},utf8_decode:function(e){for(var t="",r=0,s=0,i=0,o=0;r<e.length;)s=e.charCodeAt(r),128>s?(t+=String.fromCharCode(s),r++):s>191&&224>s?(i=e.charCodeAt(r+1),t+=String.fromCharCode((31&s)<<6|63&i),r+=2):(i=e.charCodeAt(r+1),o=e.charCodeAt(r+2),t+=String.fromCharCode((15&s)<<12|(63&i)<<6|63&o),r+=3);return t}},RestClient;if(RestClient=function(){this.VERSION="0.1.4",this.authorization={},this.server={},this.response={},this.headers={},this.accessToken={},this.autoUseRefreshToken=!0,this.autoStoreAccessToken=!0,this.authorizationType="none",this.contentType="application/json",this.sendOAuthBearerAuthorization=!1,this.dataType="json",this.oauth2NeedsAuthorization=!0,this.expiredAccessTokenMessage="",this.restfulBehavior=!0,this.backupAJAXURL=null,this.RESTMethods={create:"POST",read:"GET",update:"PUT","delete":"DELETE"},this.OAUTH2GrantTypes={code:"authorization_code",implicit:"token",user:"password",client:"client_credentials",refresh:"refresh_token"},RestClient.prototype.initObject.call(this)},RestClient.prototype.HTTP_SUCCESS=200,RestClient.prototype.HTTP_BAD_REQUEST=400,RestClient.prototype.HTTP_UNAUTHORIZED=401,RestClient.prototype.OAUTH2_INVALID_GRANT="invalid_grant",RestClient.prototype.initObject=function(){this.authorization={},this.server={},this.response={},this.headers={},this.accessToken={},this.autoUseRefreshToken=!0,this.autoStoreAccessToken=!0,this.authorizationType="none",this.contentType="application/json",this.sendOAuthBearerAuthorization=!1,this.oauth2NeedsAuthorization=!0,this.dataType="json",this.expiredAccessTokenMessage="The access token provided has expired."},RestClient.prototype.setUseRefreshTokenAutomatically=function(e){return _.isBoolean(e)&&(this.autoUseRefreshToken=e),this},RestClient.prototype.setStoreAccessTokenAutomatically=function(e){return _.isBoolean(e)&&(this.autoStoreAccessToken=e),this},RestClient.prototype.setAuthorizationType=function(e){var t={none:1,basic:1,oauth2:1};return t[e]&&(this.authorizationType=e),this},RestClient.prototype.setContentType=function(e){return this.contentType=e,this},RestClient.prototype.setSendBearerAuthorization=function(e){return _.isBoolean(e)&&(this.sendOAuthBearerAuthorization=e),this},RestClient.prototype.setOAuth2NeedsAuthorization=function(e){return _.isBoolean(e)&&(this.oauth2NeedsAuthorization=e),this},RestClient.prototype.setDataType=function(e){var t={json:"application/json",plain:"text/plain",form:"application/x-www-form-urlencoded",html:"text/html"};return t[e]&&(this.dataType=e,this.contentType=t[e]),this},RestClient.prototype.setAccessTokenExpiredMessage=function(e){return this.expiredAccessTokenMessage=e,this},RestClient.prototype.getVersion=function(){return this.VERSION},RestClient.prototype.setClient=function(e,t,r){return this.authorization.client_id=e,this.authorization.client_secret=t,this.authorization.client_url="undefined"!==r?r:null,this},RestClient.prototype.setGrantType=function(e,t){return this.authorization.grant_type="undefined"!==this.OAUTH2GrantTypes[e]?this.OAUTH2GrantTypes[e]:null,this.authorization=_.extend(this.authorization,t),this},RestClient.prototype.setAuthorizationServer=function(e){var t,r=!0;return"undefined"==typeof e||null===e?r=!1:(t=/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,e.match(t)?this.server.rest_auth_uri=e:r=!1),r},RestClient.prototype.setHeader=function(e,t){var r,s=!0;return e&&t?(r=JSON.parse('{"'+e+'" : "'+t+'"}'),this.headers=_.extend(this.headers,r)):s=!1,s},RestClient.prototype.setBasicCredentials=function(e,t){return this.authorization.basic_user=e,this.authorization.basic_password=t,this},RestClient.prototype.setAccessToken=function(e){return"object"==typeof e&&(this.accessToken=e),this},RestClient.prototype.setRestfulBehavior=function(e){return _.isBoolean(e)&&(this.restfulBehavior=e),this},RestClient.prototype.setBackupAjaxUrl=function(e){return this.backupAJAXURL=e,this},RestClient.prototype.toParams=function(e){var t=_.keys(e),r=[];return _.each(t,function(t){r.push(t+"="+e[t])}),r.join("&")},RestClient.prototype.prepareBody=function(e){var t="";return"json"===this.dataType||"jsonp"===this.dataType?"object"==typeof e&&(t=JSON.stringify(e)):t=this.toParams(e),t},RestClient.prototype.createXHR=function(){var e;if(window.XMLHttpRequest)e=new XMLHttpRequest;else try{e=new ActiveXObject("MSXML2.XMLHTTP")}catch(t){try{e=new ActiveXObject("Microsoft.XMLHTTP")}catch(r){}}return e?e:!1},RestClient.prototype.authorize=function(e){var t,r,s,i,o=this,n=!1,a="create",h=this.RESTMethods[a];t=RCBase64.encode(this.authorization.client_id+":"+this.authorization.client_secret),r=this.createXHR();try{r.open(h,this.server.rest_auth_uri,!1)}catch(u){return e.xhrfailure?e.xhrfailure(u,{}):this.XHRFailure(u,{}),n}if(r.onreadystatechange=function(){if(e.ready?e.ready(r):o.AuthorizeReady(r),4===r.readyState)if(r.status===o.HTTP_SUCCESS)try{i=JSON.parse(r.responseText),o.autoStoreAccessToken&&(o.accessToken=i.token||{}),n=!0,e.success?e.success(r,i):o.AuthorizeSuccess(r,i)}catch(t){i={success:!1,error:{error:o.HTTP_BAD_REQUEST,error_description:"Response is not a valid JSON"}},e.failure?e.failure(r,i):o.AuthorizeFailure(r,i)}else{i={};try{i=JSON.parse(r.responseText)}catch(s){}e.failure?e.failure(r,i):o.AuthorizeFailure(r,i)}},s={},this.authorization.grant_type)switch(s.grant_type=this.authorization.grant_type,this.authorization.grant_type){case"authorization_code":s.code=this.authorization.code;break;case"token":s.token=this.authorization.token;break;case"password":s.username=this.authorization.username,s.password=this.authorization.password;break;case"client_credentials":s.client_id=this.authorization.client_id,s.client_id=this.authorization.client_secret;break;case"refresh_token":s.refresh_token=this.authorization.refresh_token}return this.oauth2NeedsAuthorization&&r.setRequestHeader("Authorization","Basic "+t),r.setRequestHeader("Content-Type",this.contentType),_.each(this.headers,function(e,t){r.setRequestHeader(t,e)}),r.send(this.prepareBody(s)),n},RestClient.prototype.prepareReqFields=function(e){var t;return t={success:!1,error:{error:this.HTTP_BAD_REQUEST,error_description:"Required fields not found"},fields:e}},RestClient.prototype.prepareConsumeUrl=function(e,t,r,s){var i,o,n=this.contentType,a=!1;if(this.restfulBehavior)switch(e){case"read":i=t,r&&(i+=r),"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(a=!0,i+="?access_token="+this.accessToken.access_token),s&&s!=={}&&(i+=a?"&":"?",i+=this.toParams(s)),o=null,n="application/x-www-form-urlencoded";break;case"create":i=t,o=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(o.access_token=this.accessToken.access_token),o=this.prepareBody(o);break;case"update":i=t,r&&(i+=r),o=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(o.access_token=this.accessToken.access_token),o=this.prepareBody(o);break;case"delete":i=t,r&&(i+=r),o=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(o.access_token=this.accessToken.access_token),o=this.prepareBody(o)}else i=this.backupAJAXURL,o={operation:e,url:t,id:r,data:s},o="data='"+encodeURIComponent(JSON.stringify(o))+"'",n="application/x-www-form-urlencoded";return{url:i,body:o,content_type:n}},RestClient.prototype.getCall=function(e){return e.operation="read",this.consume(e)},RestClient.prototype.postCall=function(e){return e.operation="create",this.consume(e)},RestClient.prototype.putCall=function(e){return e.operation="update",this.consume(e)},RestClient.prototype.deleteCall=function(e){return e.operation="delete",this.consume(e)},RestClient.prototype.consume=function(e){var t,r,s,i,o,n,a,h,u,c,p,l,d,f,y={},T=!0,C=[],R=!1;if(e.operation?s=e.operation:(T=!1,C.push("operation")),e.url?u=e.url:(T=!1,C.push("url")),c=e.data||null,p=e.id||null,!T)return e.failure?e.failure(null,this.prepareReqFields(C)):this.ConsumeFailure(null,this.prepareReqFields(C)),T;switch(l=this.prepareConsumeUrl(s,u,p,c),n=l.url,o=l.body,f=l.content_type,r=this.createXHR(),this.authorizationType){case"none":break;case"basic":t=RCBase64.encode(this.authorization.basic_user+":"+this.authorization.basic_user),r.setRequestHeader("Authorization","Basic "+t);break;case"oauth2":if(!this.accessToken.access_token)return T=!1,C.push("access_token"),h={success:!1,error:{error:this.HTTP_BAD_REQUEST,error_description:"Access Token not defined"}},e.failure?e.failure(null,this.prepareReqFields(C)):this.ConsumeFailure(null,this.prepareReqFields(C)),T;this.sendOAuthBearerAuthorization&&(d="Bearer: "+this.accessToken.access_token,r.setRequestHeader("Authorization",d))}i=this.restfulBehavior?this.RESTMethods[s]:this.RESTMethods.create;try{r.open(i,n,!1)}catch(A){return e.xhrfailure?e.xhrfailure(A,c):this.XHRFailure(A,c),!1}return a=this,r.onreadystatechange=function(){if(e.ready?e.ready(r):a.ConsumeReady(r),4===r.readyState)if(r.status===a.HTTP_SUCCESS){try{y=JSON.parse(r.responseText)}catch(t){y={success:!1,error:{error:a.HTTP_BAD_REQUEST,error_description:"Response is not a valid JSON"}},e.failure?e.failure(r,y):a.ConsumeFailure(r,y)}e.success?e.success(r,y):a.ConsumeSuccess(r,y)}else try{if(y=JSON.parse(r.responseText),y.error===a.OAUTH2_INVALID_GRANT&&y.error_description===a.expiredAccessTokenMessage&&(R=!0),r.status===a.HTTP_UNAUTHORIZED&&a.autoUseRefreshToken&&R)a.accessToken.refresh_token?(a.setGrantType("refresh",{refresh_token:a.accessToken.refresh_token}),a.authorize({success:function(){T=a.consume(e),T&&(e.autorefresh?e.autorefresh(a.accessToken):a.AuthorizeAutoRefresh(a.accessToken))},failure:function(t,r){T=!1,e.failure?e.failure(null,r):a.ConsumeFailure(null,r)}})):(T=!1,y={success:!1,error:{error:a.HTTP_UNAUTHORIZED,error_description:"Refresh token is not defined"}},e.failure?e.failure(r,y):a.ConsumeFailure(r,y));else{T=!1,y={};try{y=JSON.parse(r.responseText)}catch(s){}e.failure?e.failure(r,y):a.ConsumeFailure(r,y)}}catch(s){T=!1,y={};try{y=JSON.parse(r.responseText)}catch(t){}e.failure?e.failure(r,y):a.ConsumeFailure(r,y)}},r.setRequestHeader("Content-Type",f),_.each(this.headers,function(e,t){r.setRequestHeader(t,e)}),r.send(o),T},RestClient.prototype.XHRFailure=function(){},RestClient.prototype.AuthorizeSuccess=function(){},RestClient.prototype.AuthorizeFailure=function(){},RestClient.prototype.AuthorizeReady=function(){},RestClient.prototype.AuthorizeAutoRefresh=function(){},RestClient.prototype.ConsumeSuccess=function(){},RestClient.prototype.ConsumeFailure=function(){},RestClient.prototype.ConsumeReady=function(){},"undefined"!=typeof exports){module.exports={RestClient:RestClient,RCBase64:RCBase64};var _=require("underscore")}