Skip to content
Snippets Groups Projects
Commit 3a235278 authored by scmoritz's avatar scmoritz
Browse files

Updated amivcore.js

parent 47201ab9
No related branches found
No related tags found
No related merge requests found
......@@ -32,7 +32,7 @@
<script src="lib/bootstrap/js/bootstrap.min.js"></script>
<script>
// set the api url for the amivcore js library
var api_url_config = "https://amiv-apidev.vsos.ethz.ch";
var api_url_config = "https://amiv-api.ethz.ch";
var spec_url_config = "lib/amiv/spec.json";
</script>
<script src="lib/amiv/amivcore.js"></script>
......
......@@ -35,8 +35,9 @@
return 'application/json'
},
'Authorization': function() {
if (get('cur_token') != null)
return 'Basic ' + btoa(get('cur_token') + ':');
var token = get('cur_token');
if (token != null)
return token;
return '';
},
'If-Match': function() {
......@@ -77,8 +78,12 @@
* @param {string} cname
* @param {string} cvalue
*/
function set(cname, cvalue, exdays) {
window.localStorage.setItem('glob-' + cname, cvalue);
function set(cname, cvalue) {
if (lib.shortSession) {
window.sessionStorage.setItem('glob-' + cname, cvalue);
}
else
window.localStorage.setItem('glob-' + cname, cvalue);
}
/**
......@@ -87,7 +92,10 @@
* @param {string} cname
*/
function get(cname) {
return window.localStorage.getItem('glob-' + cname);
if (lib.shortSession)
return window.sessionStorage.getItem('glob-' + cname);
else
return window.localStorage.getItem('glob-' + cname);
}
/**
......@@ -95,23 +103,55 @@
* @param {string} cname
*/
function remove(cname) {
if (window.localStorage.getItem('gloc-' + cname) === null)
window.localStorage.removeItem('glob-' + cname);
if (lib.shortSession) {
if (window.sessionStorage.getItem('glob-' + cname) !== null)
window.sessionStorage.removeItem('glob-' + cname);
}
else {
if (window.localStorage.getItem('glob-' + cname) !== null)
window.localStorage.removeItem('glob-' + cname);
}
}
/**
* Make general request with all request parameters in attr
/**
* Make JSON request with all request parameters in attr
* @constructor
* @param {} attr - all request parameters (attr.path, attr.data, attr.method ...)
* @param {} callback
*/
function req(attr, callback) {
callback = callback || function(msg) {
console.log(msg);
};
$.ajax({
url: core.lib.api_url + attr.path,
data: JSON.stringify(attr.data),
method: attr.method,
dataType: "json",
timeout: core.lib.req_time_out,
headers: attr.headers,
error: function(res) {
if (core.lib.show_errors) console.log(res);
callback(res);
},
}).done(function(res) {
callback(res);
});
}
/**
* Make FormData request with all request parameters in attr
* @constructor
* @param {} attr - all request parameters (attr.path, attr.data, attr.method ...)
* @param {} callback
*/
function reqFormData(attr, callback) {
callback = callback || function(msg) {
console.log(msg);
};
// put the json object into form-data
var form = new FormData();
console.log(attr);
for (var key in attr['data'])
form.append(key, attr['data'][key]);
$.ajax({
......@@ -162,6 +202,23 @@
curLink += '/{_id}';
}
// handle where, sort, projection, embedded
var urlParams = "";
var urlTypes = ['where', 'sort', 'projection', 'embedded'];
if (m === 'GET') {
for (var curUrlType of urlTypes) {
if (attr[curUrlType] != undefined) {
urlParams += ((urlParams != "") ? "&" + curUrlType + "=": curUrlType + "=");
if (typeof attr[curUrlType] === 'object')
urlParams += JSON.stringify(attr[curUrlType]);
else
urlParams += attr[curUrlType];
}
}
}
// append urlParams
curPath += "?" + urlParams;
if (get('cur_token') != null)
hdr['Authorization'] = 'Basic ' + btoa(get('cur_token') + ':');
......@@ -174,12 +231,22 @@
// hdr['Content-Type'] = 'application/json';
// curLib = JSON.stringify(curLib);
}
req({
path: curPath,
method: m,
data: curLib,
headers: hdr,
}, callback);
if (m != 'POST' && m != 'PATCH') {
req({
path: curPath,
method: m,
data: curLib,
headers: hdr,
}, callback);
}
else {
reqFormData({
path: curPath,
method: m,
data: curLib,
headers: hdr,
}, callback);
}
return true;
};
}
......@@ -271,6 +338,18 @@
return tmp;
}
/**
* Get the time converted to the format the api understands
* @param {Date} d - date. If none is given then the NOW is used
* @example
* amivcore.getTime() // "2016-12-20T14:12:55Z"
* amivcore.getTime(new Date(2011, 0, 1, 2, 3, 4, 567)) // "2011-01-01T01:03:04Z"
*/
lib.getTime = function(d) {
d = d || new Date();
return core.adapter['datetime'](d.toISOString());
}
/**
* Get the etag
* @constructor
......@@ -299,21 +378,23 @@
}
/**
* Login function
* @constructor
* @param {} curUser
* @param {} curPass
* @param {} callback
* Login function
* @constructor
* @param {String} curUser
* @param {String} curPass
* @param {function} callback
* @param {boolean} shortSession - if user is on a public computer
*/
lib.login = function(curUser, curPass, callback) {
lib.login = function(curUser, curPass, callback, shortSession = false) {
lib.shortSession = shortSession || false;
callback = callback || dummy;
req({
path: '/sessions/',
method: 'POST',
data: JSON.stringify({
data: {
username: curUser.toLowerCase(),
password: curPass
}),
},
headers: {
'Content-Type': 'application/json',
},
......@@ -323,14 +404,16 @@
lib['cur_' + reqVar[i]] = msg[reqVar[i]];
}
if (msg['_status'] == 'OK') {
set('cur_token_id', msg['_id'], 1);
set('cur_token', msg['token'], 1);
set('cur_user_id', msg['user'], 1);
set('cur_token_id', msg['_id']);
set('cur_token', msg['token']);
set('cur_user_id', msg['user']);
set('cur_session_etag', msg['_etag']);
callback(true);
} else {
remove('cur_token_id');
remove('cur_token');
remove('cur_user_id');
remove('cur_session_etag');
callback(false);
}
});
......@@ -343,11 +426,13 @@
lib.logout = function() {
// Deleting token from api and unsetting the vars
lib.sessions.DELETE({
id: get('cur_token_id')
id: get('cur_token_id'),
header: {"if-match": get('cur_session_etag')}
}, function(res) {
remove('cur_token');
remove('cur_token_id');
remove('cur_user_id');
remove('cur_session_etag');
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment