UNPKG

telstra-api

Version:

A node.js module for interfacing with the Telstra Dev APIs

146 lines (133 loc) 4.84 kB
//© Copyright 2016 Joshua 'JD' Davison - MIT License. // Generated by CoffeeScript 1.10.0 (function() { var https, querystring, tAuthURL, telstraAuth, url; tAuthURL = "https://api.telstra.com/v1/oauth/token"; https = require('https'); url = require('url'); querystring = require('querystring'); telstraAuth = (function() { function telstraAuth(CONSUMER_KEY, CONSUMER_SECRET, SCOPE) { this.CONSUMER_KEY = CONSUMER_KEY; this.CONSUMER_SECRET = CONSUMER_SECRET; this.SCOPE = SCOPE; if ((this.CONSUMER_KEY == null) || (this.CONSUMER_SECRET == null)) { throw Error("[T.AUTH] Init Error: CONSUMER_KEY or CONSUMER_SECRET missing"); } this.access_token = ''; this.expires_at = 0; this._pending = null; this.getToken()["catch"]((function(_this) { return function(e) { console.error(e.stack); return process.exit(0); }; })(this)); } telstraAuth.prototype.getToken = function() { if ((this._pending != null)) { return this._pending; } else if (this.expires_at <= this._getTime() + 60) { return this._pending = new Promise((function(_this) { return function(resolve, reject) { var body; body = { client_id: _this.CONSUMER_KEY, client_secret: _this.CONSUMER_SECRET, grant_type: 'client_credentials', scope: _this.SCOPE }; return _this.doSecurePost(tAuthURL, body, false, 'application/x-www-form-urlencoded').then(function(result) { var headers, ref, ref2, ref3, statusCode; statusCode = result[0], headers = result[1], body = result[2]; if ((ref = body.error) != null) { return reject(Error("[T.AUTH] Authentication Error: " + ref)); } else if (((ref2 = body.access_token) != null) && (ref3 = body.expires_in)) { _this.expires_at = ref3 + _this._getTime; _this._pending = null; return resolve(_this.access_token = ref2); } else { return reject(Error("[T.AUTH] Unexpected Result")); } }, function(e) { return reject(e); }); }; })(this)); } else { return Promise.resolve(this.access_token); } }; telstraAuth.prototype.doSecurePost = function(uri, body, useToken, contentType) { if (body == null) { body = ''; } if (useToken == null) { useToken = true; } if (contentType == null) { contentType = "application/json; charset=utf-8"; } if (useToken === true) { return this.getToken().then((function(_this) { return function(msg) { return _this.doSecurePost(uri, body, msg); }; })(this)); } return new Promise(function(resolve, reject) { var e, error, error1, queryString, req, uriObj; try { uriObj = url.parse(uri); } catch (error) { e = error; throw Error("[T.AUTH] URL Parse Error: " + e.message); } if (contentType === 'application/x-www-form-urlencoded') { queryString = querystring.stringify(body); } else { queryString = JSON.stringify(body); } uriObj.method = body !== '' ? 'post' : 'get'; uriObj.headers = { 'Accept': 'application/json', 'Content-Length': queryString.length, 'Content-Type': contentType }; if (typeof useToken === 'string') { uriObj.headers['Authorization'] = "Bearer " + useToken; } try { req = https.request(uriObj, function(res) { var replyBody; replyBody = ''; return res.setEncoding('utf8').on('data', function(chunk) { return replyBody += chunk; }).on('end', function() { var error1, replyJSON; try { replyJSON = JSON.parse(replyBody); } catch (error1) { e = error1; throw Error("[T.AUTH] Parsing Error: " + e.message); } return resolve([res.statusCode, res.headers, replyJSON]); }); }); } catch (error1) { e = error1; throw Error("[T.AUTH] Request Error: " + e.message); } req.on('error', function(e) { throw Error("[T.AUTH] Connection Error: " + e.message); }); return req.end(queryString); }); }; telstraAuth.prototype._getTime = function() { return Math.trunc(new Date().getTime() / 1000); }; return telstraAuth; })(); module.exports = telstraAuth; }).call(this);