UNPKG

rofex

Version:

Primary REST API. Websocket API

64 lines (59 loc) 1.72 kB
const request = require('request'); class Auth { /** * * @param {*} user * @param {*} pass * @param {*} timeout * @param {*} test */ constructor(user,pass,timeout = 15000,test = false) { this.user = user; this.pass = pass; this.timeout = timeout; this._baseUrl = 'https://api.primary.com.ar/'; if (test) { this._baseUrl = 'http://demo-api.primary.com.ar/'; } this.token = ''; } /** * * @param {*} cb */ getToken(cb = () => {}) { return new Promise((resolve, reject) => { if (this.token !== '') { resolve(this.token); cb(this.token); } else { const options = { url: `${this._baseUrl}auth/getToken`, timeout: this.timeout }; options.headers = { 'X-Username': this.user, 'X-Password': this.pass } options.method = 'POST'; request(options, (err, response, body) => { let payload; payload = body; if (response.headers['x-auth-token']) { this.token = response.headers['x-auth-token']; resolve(this.token); } else { reject('empty token'); cb('empty token'); } }); } }); } /** * * @param {*} callback */ removeToken(callback) { this._makeRequest('', callback , 'auth/removeToken', 'GET'); } } module.exports = Auth;