Commit e5ff2cc2 authored by Thomas Lechauve's avatar Thomas Lechauve

Authentication added when send request to REST API

parent d0b7358f
;(function($) {
var methods = {
defaults: {
host: ''
},
init: function( options ){
var settings = $.extend({
'host': ''
'host': '',
'access_token': '',
'clientID': ''
}, options);
return this.each(function(){
methods['store'] = Modernizr.localstorage ? methods.lStore : methods.cStore;
$(this).slapos('store', 'host', settings.host);
for(var setting in settings){
$(this).slapos('store', setting, settings[setting]);
}
});
},
/* Getters & Setters shortcuts */
access_token: function( value ){
return $(this).slapos('store', 'access_token', value);
},
host: function( value ){
return $(this).slapos('store', 'host', value);
},
clientID: function( value ){
return $(this).slapos('store', 'clientID', value);
},
/* Local storage method */
lStore: function(name, value){
lStore: function( name, value ) {
if(Modernizr.localstorage)
return value == undefined ? window.localStorage[name] : window.localStorage[name] = value;
return false;
......@@ -44,16 +55,23 @@
}
},
request: function(type, url, callback, statusEvent, data){
request: function( type, url, callback, statusEvent, data ) {
data = data || '';
statusEvent = statusEvent || this.statusDefault;
return this.each(function(){
$.ajax({
url: methods.store('host')+url,
url: "http://"+$(this).slapos('host')+url,
type: type,
contentType: 'application/octet-stream',
data: JSON.stringify(data),
dataType: 'json',
data: data,
context: $(this),
type: type,
beforeSend: function(xhr){
if( $(this).slapos("access_token") ) {
xhr.setRequestHeader("Authorization", $(this).slapos("store","token_type") + " " + $(this).slapos("access_token"));
xhr.setRequestHeader("Accept", "application/json");
}
},
statusCode: statusEvent,
success: callback
});
......@@ -61,7 +79,7 @@
},
newInstance: function(data, callback, statusEvent){
return $(this).slapos('request', 'POST', '/request', callback, statusEvent, data);
return $(this).slapos('request', 'POST', '/instance', callback, statusEvent, data);
},
deleteInstance: function(id, callback, statusEvent){
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment