Snippets
Created by
Robert Poz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | (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)
You can clone a snippet to your computer for local editing. Learn more.