Snippets

ESTI design App Gateway client library

Created by Robert Poz
(function() {

  /* Initial setup (NodeJS and browsers) */
  var XMLHttpRequest,  _;
  if (typeof exports !== 'undefined') {
      XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
      _ = require('underscore');
      btoa = require('btoa');
  } else 
      _ = window._;
  
  if (typeof window !== 'undefined' && typeof window.XMLHttpRequest !== 'undefined'){
      XMLHttpRequest = window.XMLHttpRequest;
  }

  var API_URL = 'http://66.109.17.117:4567';  

  var AuthApi = function(options) {
    options = options || {}

    /* Abstraction for HTTP requests */ 
    function _request(method, path, data, cb, raw, sync) {
      function getURL() {
        var url = path.indexOf('//') >= 0 ? path : API_URL + path;
        return url;
      }
      function serialize(obj) {
        var str = [];
        for(var p in obj)
          if (obj.hasOwnProperty(p)) 
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
      }

      var xhr = new XMLHttpRequest();
      if(!raw) xhr.dataType = "json";

      xhr.open(method, getURL(), !sync);
      if(!sync) {
        xhr.onreadystatechange = function () {
          if (this.readyState == 4 && typeof cb === 'function') {
            if (this.status >= 200 && this.status < 300 || this.status === 304) {
              cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this);
            } else {
              cb({path: path, request: this, error: this.status});
            }
          }
        };
      }
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      if(options.token) {
        var authorization = 'Basic ' + btoa(options.token);
        console.log(authorization)
        xhr.setRequestHeader('Authorization', authorization);
      }
      if(data)  xhr.send(serialize(data));
      else      xhr.send();

      if(sync) return xhr.response;
    }

    /* Alternative authentication for sandbox purpose only */
    this.login = function(login, password, cb) {
      _request("POST", '/sandbox/login', { login: login, password: password }, 
        function(err, rsp) {
          options.token = rsp.auth_token
          cb()
        }
      )
    };

    this.services = function(cb) {
      _request("GET", '/services', null, cb);
    }

    this.subscriptions = function(params, cb) {
      _request("GET", '/subscriptions', params, cb);
    } 

    this.createSession = function(service_id, cb) {
      var self = this;
      _request("POST", '/sessions', { service_id: service_id }, function(err, res) {
        if(!err) self.session_token = res.session_token;
        cb(err, res)
      });
    } 

    this.updateSession = function(params, cb) {
      _request("PUT", '/sessions/' + this.session_token, params, cb)
    }
    /* updateSession() wrappers */
    this.extendSession = function(cb) {
      this.updateSession({ action: 'extend' }, cb)
    }
    this.deactivateSession = function(cb) {
      this.updateSession({ action: 'deactivate' }, cb)
    }
    this.reportSession = function(cb) {
      this.updateSession({ action: 'error' }, cb)
    }

    this.updateSettings = function(params, cb) {
      _request("PUT", '/settings/' + this.session_token, params, cb)
    }
  };

  if (typeof exports !== 'undefined') {
    module.exports = AuthApi;
  } else {
    window.AuthApi = AuthApi;
  }

}).call(this);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.