Commit e5ff2cc2 authored by Thomas Lechauve's avatar Thomas Lechauve

Authentication added when send request to REST API

parent d0b7358f
;(function($) { ;(function($) {
var methods = { var methods = {
defaults: {
host: ''
},
init: function( options ){ init: function( options ){
var settings = $.extend({ var settings = $.extend({
'host': '' 'host': '',
'access_token': '',
'clientID': ''
}, options); }, options);
return this.each(function(){ return this.each(function(){
methods['store'] = Modernizr.localstorage ? methods.lStore : methods.cStore; 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 */ /* Local storage method */
lStore: function(name, value){ lStore: function( name, value ) {
if(Modernizr.localstorage) if(Modernizr.localstorage)
return value == undefined ? window.localStorage[name] : window.localStorage[name] = value; return value == undefined ? window.localStorage[name] : window.localStorage[name] = value;
return false; return false;
...@@ -44,16 +55,23 @@ ...@@ -44,16 +55,23 @@
} }
}, },
request: function(type, url, callback, statusEvent, data){ request: function( type, url, callback, statusEvent, data ) {
data = data || ''; data = data || '';
statusEvent = statusEvent || this.statusDefault; statusEvent = statusEvent || this.statusDefault;
return this.each(function(){ return this.each(function(){
$.ajax({ $.ajax({
url: methods.store('host')+url, url: "http://"+$(this).slapos('host')+url,
type: type,
contentType: 'application/octet-stream',
data: JSON.stringify(data),
dataType: 'json', dataType: 'json',
data: data,
context: $(this), 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, statusCode: statusEvent,
success: callback success: callback
}); });
...@@ -61,7 +79,7 @@ ...@@ -61,7 +79,7 @@
}, },
newInstance: function(data, callback, statusEvent){ 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){ 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