Files
luos/workflow/public_html/lib/js/restclient.min.js
2025-04-04 12:19:34 +00:00

1 line
12 KiB
JavaScript

var RCBase64={keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t,r,s,o,i,n,a,h="",c=0;for(e=this.utf8_encode(e);c<e.length;)t=e.charCodeAt(c++),r=e.charCodeAt(c++),s=e.charCodeAt(c++),o=t>>2,i=(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(o)+this.keyStr.charAt(i)+this.keyStr.charAt(n)+this.keyStr.charAt(a);return h},decode:function(e){var t,r,s,o,i,n,a,h="",c=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");c<e.length;)o=this.keyStr.indexOf(e.charAt(c++)),i=this.keyStr.indexOf(e.charAt(c++)),n=this.keyStr.indexOf(e.charAt(c++)),a=this.keyStr.indexOf(e.charAt(c++)),t=o<<2|i>>4,r=(15&i)<<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(r>>6|192),s+=String.fromCharCode(63&r|128)):(s+=String.fromCharCode(r>>12|224),s+=String.fromCharCode(r>>6&63|128),s+=String.fromCharCode(63&r|128));return s},utf8_decode:function(e){for(var t="",r=0,s=0,o=0,i=0;r<e.length;)s=e.charCodeAt(r),128>s?(t+=String.fromCharCode(s),r++):s>191&&224>s?(o=e.charCodeAt(r+1),t+=String.fromCharCode((31&s)<<6|63&o),r+=2):(o=e.charCodeAt(r+1),i=e.charCodeAt(r+2),t+=String.fromCharCode((15&s)<<12|(63&o)<<6|63&i),r+=3);return t}},RestClient;if(RestClient=function(){this.VERSION="0.1.9\n",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.acceptType=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","201","202","204","207"],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.acceptType="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.setAcceptType=function(e){var t={plain:"text/plain",xhtml_xml:"application/xhtml+xml",json:"application/json",xml:"application/xml",all:"*/*"};return t[e]&&(this.acceptType=t[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.JSONParse=function(e){var t;try{t=e?""===e?"":JSON.parse(e):""}catch(r){t="ERROR_PARSE"}return t},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,o,i=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(c){return e.xhrfailure?e.xhrfailure(c,{}):this.XHRFailure(c,{}),n}if(r.onreadystatechange=function(){if(e.ready?e.ready(r):i.AuthorizeReady(r),4===r.readyState){if(o=i.JSONParse(r.responseText),-1!=i.HTTP_SUCCESS.indexOf(String(r.status))&&"ERROR_PARSE"!==o)if(i.autoStoreAccessToken&&(i.accessToken=o.token||{}),n=!0,e.success)try{e.success(r,o)}catch(t){throw new Error(t.message)}else i.AuthorizeSuccess(r,o);else if("ERROR_PARSE"===o&&(o={success:!1,error:{error:i.HTTP_BAD_REQUEST,error_description:"Response is not a valid JSON"}}),e.failure)try{e.failure(r,o)}catch(t){throw new Error(t.message)}else i.AuthorizeFailure(r,o);"function"==typeof e.complete&&e.complete(r,o)}},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("Accept",this.acceptType),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 o,i,n=this.acceptType,a=this.contentType,h=!1;if(this.restfulBehavior)switch(e){case"read":o=t,r&&(o+=r),"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(h=!0,o+="?access_token="+this.accessToken.access_token),s&&s!=={}&&(o+=h?"&":"?",o+=this.toParams(s)),i=null,null===a&&(a="application/json");break;case"create":o=t,i=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(i.access_token=this.accessToken.access_token),i=this.prepareBody(i);break;case"update":o=t,r&&(o+=r),i=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(i.access_token=this.accessToken.access_token),i=this.prepareBody(i);break;case"delete":o=t,r&&(o+=r),i=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(i.access_token=this.accessToken.access_token),i=this.prepareBody(i)}else o=this.backupAJAXURL,i={operation:e,url:t,id:r,data:s},i="data='"+encodeURIComponent(JSON.stringify(i))+"'",null===a&&(a="application/json");return n||(n="*/*"),{url:o,body:i,content_type:a,acceptType: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,o,i,n,a,h,c,u,p,l,d,f,y,R={},T=!0,C=[],A=!1;if(e.operation?s=e.operation:(T=!1,C.push("operation")),e.url?c=e.url:(T=!1,C.push("url")),u=e.data||null,p=e.id||null,!T)return e.failure?e.failure(null,this.prepareReqFields(C)):this.ConsumeFailure(null,this.prepareReqFields(C)),T;l=this.prepareConsumeUrl(s,c,p,u),n=l.url,i=l.body,f=l.content_type,y=l.acceptType,r=this.createXHR(),o=this.restfulBehavior?this.RESTMethods[s]:this.RESTMethods.create;try{switch(r.open(o,n,!1),this.authorizationType){case"none":break;case"basic":t=RCBase64.encode(this.authorization.basic_user+":"+this.authorization.basic_password),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))}}catch(k){return e.xhrfailure?e.xhrfailure(k,u):this.XHRFailure(k,u),!1}return a=this,r.onreadystatechange=function(){if(e.ready?e.ready(r):a.ConsumeReady(r),4===r.readyState){if(R=a.JSONParse(r.responseText),-1!=a.HTTP_SUCCESS.indexOf(String(r.status))&&"ERROR_PARSE"!==R)if(a.autoStoreAccessToken&&(a.accessToken=R.token||{}),T=!0,e.success)try{e.success(r,R)}catch(t){throw new Error(t.message)}else a.AuthorizeSuccess(r,R);else if(T=!1,"ERROR_PARSE"===R?R={success:!1,error:{error:a.HTTP_BAD_REQUEST,error_description:"Response is not a valid JSON"}}:(R.error===a.OAUTH2_INVALID_GRANT&&R.error_description===a.expiredAccessTokenMessage&&(A=!0),r.status===a.HTTP_UNAUTHORIZED&&a.autoUseRefreshToken&&A&&(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,R={success:!1,error:{error:a.HTTP_UNAUTHORIZED,error_description:"Refresh token is not defined"}}))),e.failure)try{e.failure(r,R)}catch(t){throw new Error(t.message)}else a.AuthorizeFailure(r,R);"function"==typeof e.complete&&e.complete(r,R)}},r.setRequestHeader("Accept",y),r.setRequestHeader("Content-Type",f),_.each(this.headers,function(e,t){r.setRequestHeader(t,e)}),r.send(i),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")}