function BMXClient(e,t,n,r,i){function c(){s=e,o=t,u={"Accept-Language":r},i&&(l=i),a=null,f=null}function h(e){f=e}function p(e,t){if(!e||!t)return;(e.match(/^\s*https\:\/\/bose-([a-zA-Z0-9\.\_\-\$\%]+?)\.apigee\.net\//)||e.match(/^\s*https\:\/\/test.content\.api\.bose\.io\//)||e.match(/^\s*https\:\/\/content\.api\.bose\.io\//))&&l&&(t["x-bmx-api-key"]=l,t["x-software-version"]=gui_version)}function d(t,i,o,u){var a=n._links.bmx_token?y(n._links.bmx_token.href):"";if(a===""){u({errorCode:s.errorDefines.getErrorCode("BMX_INVALID_REQUEST_ERROR"),errorText:"Invalid token url."});return}var f={grant_type:t};switch(t){case"authorization_code":f.code=i.code;break;case"password":f.username=i.username||"",f.password=i.password||"",f.is_anonymous=i.isAnonymousAccount;break;case"refresh_token":f.refresh_token=i.refresh_token||"";break;default:var l={errorCode:e.errorDefines.getErrorCode("BMX_INVALID_REQUEST_ERROR"),errorText:"(bmx_client.requestToken): Invalid grantType."};u(l);return}var c={"Content-Type":"application/json","Accept-Language":r};p(a,c);var h=[];h.push({url:a,method:"POST",data:JSON.stringify(f),dataType:"json",headers:c});var d=function(e){v(e,t,o,u)};s.urlFetch(h,d)}function v(t,n,r,i){var l=new BMXAPIResponseValidator(s,t),c=l.getValidationResults();if(l.isValid()){a=t[0].responseJSON.access_token,u.Authorization=a;if(n==="password"||n==="authorization_code"){if(t[0].responseJSON.refresh_token==undefined){c.errorCode=s.errorDefines.getErrorCode("BMX_INVALID_REFRESH_TOKEN_ERROR"),c.errorText="Invalid response from server - "+t[0].responseText,i.call(null,c);return}var h=t[0].responseJSON;if(h&&h._embedded&&h._embedded.bmx_account){r.call(null,h,h._embedded.bmx_account);return}if(h._links&&h._links.bmx_account&&h._links.bmx_account.href){f=h.refresh_token||"";var p=function(e){r.call(null,h,e)};m(h._links.bmx_account.href,p,i);return}}else n=="refresh_token"&&r.call(null,t[0].responseJSON)}else i?(t[0].status==401&&(n=="refresh_token"?c.errorCode=e.errorDefines.getErrorCode("BMX_INVALID_REFRESH_TOKEN_ERROR"):c.errorCode=e.errorDefines.getErrorCode("BMX_REFRESH_TOKEN_REQUEST_ERROR")),i.call(null,c)):console.log(o+" - requestAccessTokenCB - Error:"+c.errorText)}function m(e,t,n,r){e=y(e),r!==!1&&(r=!0);var i=[],o=u;p(e,o),i.push({url:e,method:"GET",cache:!0,headers:o,dataType:"json"}),s.urlFetch(i,function(i){g(i,e,t,n,r)})}function g(e,t,n,r,i){var u=new BMXAPIResponseValidator(s,e),a=u.getValidationResults();if(u.isValid())n&&n.call(null,e[0].responseJSON);else{if(i&&e[0].status===401){var l=function(){m(t,n,r,!1)};d("refresh_token",{refresh_token:f},l,r);return}r?r.call(null,a):console.log(o+" - retrieveNodesCB - Error:"+a.errorText)}}function y(e){return e?e.match(/^http[s]?:\/\//)?e:n.baseUrl+e:(console.log("invalid bmx resource url."),"")}this.retrieveNodes=m,this.requestToken=d,this.setSecret=h;var s,o,u,a,f,l;c()};