Created
February 8, 2016 23:15
-
-
Save indolering/4c6a45a0db3848e6bfb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(typeof ISNODE === 'undefined'){ | |
//Enables running core script in browser or Node.js. | |
//Uglify defines ISNODE and dead code removal cleans everything when compiled. | |
ISNODE = typeof GLOBAL !== 'undefined'; | |
} | |
var SwaggerClient; | |
if(ISNODE){ | |
SwaggerClient = require('swagger-client'); | |
} else { | |
SwaggerClient = window.SwaggerClient; | |
} | |
function EasyAPI(){ | |
} | |
/** | |
* @typedef {Object} EasyAPIClient EasyDNS API connection. | |
*/ | |
/** | |
* Configuration Object for connecting to EasyDNS API. | |
* @typedef {Object} EasyAPIConf | |
* @property {string} token - EasyAPI API token. | |
* @property {string} key - Legacy EasyAPI API key. | |
* @property {string} env - production|sandbox|testing switch indicating desired API endpoint. | |
* @property {string} [url] - API endpoint that overrides env switch. | |
* @property {boolean} [url] - API endpoint that overrides env switch. | |
*/ | |
EasyAPI.prototype.SwaggerClient = SwaggerClient; | |
/** | |
* | |
* @param {EasyAPIConf} conf Configuration object containing API key and token and optional switch for sandbox. | |
* @param {function} [success] Function called after client has been initialized. | |
* @param {function} [failure] Function called in the case of an error. | |
* @return {Promise<EasyAPIClient|Error>|Object} EasyDNS REST API client. | |
*/ | |
EasyAPI.prototype.Client = function(conf, success, failure){ | |
var usePromise = success || failure ? false : true; | |
var error = null; | |
if(typeof conf !== "object" || !conf.token){ | |
error = new Error("You must pass a configuration object containing authorization info!"); | |
} else if(conf.key && !conf.token){ | |
error = new Error("Legacy API authentication requires a key and token!"); | |
} else if(conf.token.indexOf("SOME.NUMBER") > -1 || conf.key && conf.key.indexOf("SOME.NUMBER") > -1){ | |
error = new Error("You passed in *example* auth values, you need to supply valid API authorization info!"); | |
} else if(typeof conf.url !== "string"){ | |
conf.env = conf.env || 'production'; | |
switch(conf.env){ | |
case 'production': | |
conf.url = "https://rest.easydns.net/swagger.json"; | |
break; | |
case 'sandbox': | |
conf.url = "https://sandbox.rest.easydns.net/swagger.json"; | |
break; | |
case 'testing': | |
conf.url = "http://testing.rest.easydns.vpn/swagger.json"; //node doesn't like self-signed certs | |
if(!ISNODE){ //if test page is using a self-signed cert ... | |
conf.url = window.location.protocol + "//testing.rest.easydns.vpn/swagger.json"; | |
} | |
break; | |
default: | |
error = new Error("Invalid conf.env setting, try 'production' or 'sandbox'!"); | |
} | |
} | |
if(error){ | |
if(usePromise){ | |
return Promise.reject(error); | |
} else if(failure){ | |
failure(error); | |
} else { | |
throw error; | |
} | |
} else { | |
if(conf.cacheBust){ | |
conf.url = conf.url + "?_cache_bust=" + Date.now() + Math.random(); | |
} | |
if(usePromise){ | |
return new SwaggerClient({ | |
url: conf.url, | |
usePromise: usePromise | |
}).then(function(client){ | |
return addAuth(client, conf); | |
}.bind(this)) | |
} else { | |
//jquery succeed/fail + sync leads to crazy syntax. | |
var client = new SwaggerClient({ | |
url: conf.url, | |
usePromise: usePromise, | |
success: function(){ | |
if(success){ | |
success(addAuth(client, conf)) | |
} | |
}.bind(this), | |
failure: function(error){ | |
if(failure){ | |
failure(error); | |
} else { | |
throw error; | |
} | |
}.bind(this) | |
}); | |
return client; | |
} | |
} | |
function addAuth(client, conf){ | |
if(!conf.key){ | |
//temporary stand-in for oAuth system | |
client.clientAuthorizations.add("api_combined_key", new SwaggerClient.ApiKeyAuthorization("x-auth_combined", conf.token + ":" + conf.key, "header")); | |
} else { | |
client.clientAuthorizations.add("easyapi_basic", new SwaggerClient.PasswordAuthorization(conf.token, conf.key)); | |
} | |
return client; | |
} | |
}; | |
if(ISNODE){ | |
module.exports = exports = new EasyAPI(); | |
} else { | |
if(!window.EasyAPI){ | |
window.EasyAPI = new EasyAPI(); | |
} else { //There's probably a better way to do this... | |
if(!window.EasyAPI.Client){ | |
window.EasyAPI.Client = EasyAPI.prototype.Client; | |
} | |
if(!window.EasyAPI.SwaggerClient){ | |
window.EasyAPI.SwaggerClient = window.SwaggerClient; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment