turtlestitch/cloud.js

228 wiersze
6.4 KiB
JavaScript
Czysty Zwykły widok Historia

/*
cloud.js
a backend API for SNAP!
2017-09-28 11:26:46 +00:00
written by Bernat Romagosa
inspired in old cloud API by Jens Mönig
2017-09-28 11:26:46 +00:00
Copyright (C) 2017 by Bernat Romagosa
Copyright (C) 2015 by Jens Mönig
This file is part of Snap!.
Snap! is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Global settings /////////////////////////////////////////////////////
2017-09-28 11:26:46 +00:00
/*global modules, IDE_Morph, SnapSerializer, nop, localize*/
modules.cloud = '2015-December-15';
// Global stuff
var Cloud;
// Cloud /////////////////////////////////////////////////////////////
function Cloud(url) {
2017-09-28 11:26:46 +00:00
this.init(url);
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.init = function (url) {
this.url = url;
this.username = null;
2017-09-28 11:26:46 +00:00
this.checkCredentials();
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.clear = function () {
this.username = null;
};
2017-09-28 11:26:46 +00:00
// Dictionary handling
2017-09-28 11:26:46 +00:00
Cloud.prototype.parseDict = function (src) {
var dict = {};
if (!src) {return dict; }
src.split("&").forEach(function (entry) {
var pair = entry.split("="),
key = decodeURIComponent(pair[0]),
val = decodeURIComponent(pair[1]);
dict[key] = val;
});
return dict;
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.encodeDict = function (dict) {
var str = '',
pair,
key;
if (!dict) {return null; }
for (key in dict) {
if (dict.hasOwnProperty(key)) {
pair = encodeURIComponent(key)
+ '='
+ encodeURIComponent(dict[key]);
if (str.length > 0) {
str += '&';
}
2017-09-28 11:26:46 +00:00
str += pair;
}
}
2017-09-28 11:26:46 +00:00
return str;
};
2017-09-28 11:26:46 +00:00
// Low level functionality
Cloud.prototype.get = function (path, onSuccess, onError, errorMsg) {
var request = new XMLHttpRequest(),
myself = this;
try {
request.open(
2017-09-28 11:26:46 +00:00
'GET',
this.url + path,
true
);
request.setRequestHeader(
2017-09-28 11:26:46 +00:00
'Content-Type',
'application/json; charset=utf-8'
);
request.withCredentials = true;
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
2017-09-28 11:26:46 +00:00
var response = JSON.parse(request.responseText);
if (response.errors) {
onError.call(
null,
2017-09-28 11:26:46 +00:00
response.errors[0],
errorMsg
);
2017-09-28 11:26:46 +00:00
} else {
onSuccess.call(null, response.message || response);
}
} else {
2017-09-28 11:26:46 +00:00
onError.call(
null,
2017-09-28 11:26:46 +00:00
myself.url,
errorMsg
);
}
}
};
2017-09-28 11:26:46 +00:00
request.send();
} catch (err) {
2017-09-28 11:26:46 +00:00
onError.call(this, err.toString(), 'Cloud Error');
}
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.post = function (path, body, onSuccess, onError, errorMsg) {
var request = new XMLHttpRequest(),
myself = this;
try {
request.open(
2017-09-28 11:26:46 +00:00
'POST',
this.url + path,
true
);
request.setRequestHeader(
2017-09-28 11:26:46 +00:00
'Content-Type',
'application/json'
);
request.withCredentials = true;
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.responseText) {
2017-09-28 11:26:46 +00:00
var response = JSON.parse(request.responseText);
if (response.errors) {
onError.call(
this,
2017-09-28 11:26:46 +00:00
response.errors[0],
'Cloud Error'
);
} else {
2017-09-28 11:26:46 +00:00
onSuccess.call(null, response.message || response);
}
} else {
2017-09-28 11:26:46 +00:00
onError.call(
null,
2017-09-28 11:26:46 +00:00
myself.url + path,
localize('could not connect to:')
);
}
}
};
2017-09-28 11:26:46 +00:00
request.send(body);
} catch (err) {
2017-09-28 11:26:46 +00:00
onError.call(this, err.toString(), 'Cloud Error');
}
};
2017-09-28 11:26:46 +00:00
// Credentials management
Cloud.prototype.checkCredentials = function (onSuccess, onError) {
var myself = this;
this.getCurrentUser(
function (user) {
if (user.username) {
myself.username = user.username;
}
2017-09-28 11:26:46 +00:00
if (onSuccess) { onSuccess.call(null, user.username); }
},
onError);
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.getCurrentUser = function (onSuccess, onError) {
this.get('/users/c', onSuccess, onError, 'Could not retrieve current user');
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.logout = function (onSuccess, onError) {
this.post(
'/users/' + this.username + '/logout',
null,
onSuccess,
onError,
'logout failed');
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.login = function (username, password, onSuccess, onError) {
var myself = this;
2017-09-28 11:26:46 +00:00
this.post(
'/users/' + username + '/login?' + this.encodeDict({ password: password }),
null,
function () {
2017-09-28 11:26:46 +00:00
myself.checkCredentials(onSuccess, onError);
},
2017-09-28 11:26:46 +00:00
onError,
'login failed');
};
2017-09-28 11:26:46 +00:00
Cloud.prototype.signup = function (username, password, password_repeat, email, onSuccess, onError){
this.post(
'/users/' + username + '?' + this.encodeDict({
email: email,
password: password,
password_repeat: password_repeat
}),
null,
onSuccess,
onError,
'signup failed');
};
2017-09-28 11:26:46 +00:00
var SnapCloud = new Cloud('http://localhost:8080');