Switch to a multi-request/response API to make batching possible.
This commit is contained in:
128
api.py
128
api.py
@@ -26,74 +26,82 @@ from cosmopolite.lib import utils
|
|||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
class SetValue(webapp2.RequestHandler):
|
@db.transactional()
|
||||||
@utils.chaos_monkey
|
def SetValue(google_user, client, args):
|
||||||
@utils.returns_json
|
entry_key = args['key']
|
||||||
@utils.local_namespace
|
entry_value = args['value']
|
||||||
@security.google_user_xsrf_protection
|
public = (args['public'] == 'true')
|
||||||
@security.weak_security_checks
|
|
||||||
@session.session_required
|
entries = (models.StateEntry.all()
|
||||||
@db.transactional()
|
.ancestor(client.parent_key())
|
||||||
def post(self):
|
.filter('entry_key =', entry_key)
|
||||||
entry_key = self.request.get('key')
|
.fetch(1))
|
||||||
entry_value = self.request.get('value')
|
if entries:
|
||||||
public = (self.request.get('public') == 'true')
|
entry = entries[0]
|
||||||
|
entry.entry_value = entry_value
|
||||||
entries = (models.StateEntry.all()
|
entry.public = public
|
||||||
.ancestor(self.client.parent_key())
|
else:
|
||||||
.filter('entry_key =', entry_key)
|
entry = models.StateEntry(
|
||||||
.fetch(1))
|
parent=client.parent_key(),
|
||||||
if entries:
|
entry_key=entry_key,
|
||||||
entry = entries[0]
|
entry_value=entry_value,
|
||||||
entry.entry_value = entry_value
|
public=public)
|
||||||
entry.public = public
|
|
||||||
else:
|
entry.put()
|
||||||
entry = models.StateEntry(
|
msg = entry.ToMessage()
|
||||||
parent=self.client.parent_key(),
|
clients = (models.Client.all()
|
||||||
entry_key=entry_key,
|
.ancestor(client.parent_key()))
|
||||||
entry_value=entry_value,
|
for client in clients:
|
||||||
public=public)
|
client.SendMessage(msg)
|
||||||
|
|
||||||
entry.put()
|
return {}
|
||||||
msg = entry.ToMessage()
|
|
||||||
clients = (models.Client.all()
|
|
||||||
.ancestor(self.client.parent_key()))
|
def CreateChannel(google_user, client, args):
|
||||||
for client in clients:
|
token = channel.create_channel(
|
||||||
client.SendMessage(msg)
|
client_id=str(client.key()),
|
||||||
|
duration_minutes=config.CHANNEL_DURATION_SECONDS / 60)
|
||||||
return {}
|
messages = [x.ToMessage()
|
||||||
|
for x in client.parent().GetStateEntries()]
|
||||||
|
if google_user:
|
||||||
class CreateChannel(webapp2.RequestHandler):
|
messages.append({
|
||||||
|
'message_type': 'login',
|
||||||
|
'google_user': google_user.email(),
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
messages.append({
|
||||||
|
'message_type': 'logout',
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
'token': token,
|
||||||
|
'messages': messages,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class APIWrapper(webapp2.RequestHandler):
|
||||||
|
|
||||||
|
_COMMANDS = {
|
||||||
|
'createChannel': CreateChannel,
|
||||||
|
'setValue': SetValue,
|
||||||
|
}
|
||||||
|
|
||||||
@utils.chaos_monkey
|
@utils.chaos_monkey
|
||||||
|
@utils.expects_json
|
||||||
@utils.returns_json
|
@utils.returns_json
|
||||||
@utils.local_namespace
|
@utils.local_namespace
|
||||||
@security.google_user_xsrf_protection
|
@security.google_user_xsrf_protection
|
||||||
@security.weak_security_checks
|
@security.weak_security_checks
|
||||||
@session.session_required
|
@session.session_required
|
||||||
def post(self):
|
def post(self):
|
||||||
token = channel.create_channel(
|
ret = []
|
||||||
client_id=str(self.client.key()),
|
for command in self.request_json['commands']:
|
||||||
duration_minutes=config.CHANNEL_DURATION_SECONDS / 60)
|
callback = self._COMMANDS[command['command']]
|
||||||
messages = [x.ToMessage()
|
result = callback(self.verified_google_user, self.client, command.get('arguments', {}))
|
||||||
for x in self.client.parent().GetStateEntries()]
|
ret.append(result)
|
||||||
if self.verified_google_user:
|
return ret
|
||||||
messages.append({
|
|
||||||
'message_type': 'login',
|
|
||||||
'google_user': self.verified_google_user.email(),
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
messages.append({
|
|
||||||
'message_type': 'logout',
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
'token': token,
|
|
||||||
'messages': messages,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
app = webapp2.WSGIApplication([
|
app = webapp2.WSGIApplication([
|
||||||
(config.URL_PREFIX + '/api/createChannel', CreateChannel),
|
(config.URL_PREFIX + '/api', APIWrapper),
|
||||||
(config.URL_PREFIX + '/api/setValue', SetValue),
|
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
handlers:
|
handlers:
|
||||||
- url: /cosmopolite/api/.*
|
- url: /cosmopolite/api
|
||||||
script: cosmopolite.api.app
|
script: cosmopolite.api.app
|
||||||
secure: always
|
secure: always
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ def google_user_xsrf_protection(handler):
|
|||||||
if not google_user:
|
if not google_user:
|
||||||
return handler(self)
|
return handler(self)
|
||||||
|
|
||||||
google_user_id = auth.Parse(self.request.get('google_user_id', None))
|
google_user_id = auth.Parse(self.request_json.get('google_user_id', None))
|
||||||
if (not google_user_id or
|
if (not google_user_id or
|
||||||
google_user_id != google_user.user_id()):
|
google_user_id != google_user.user_id()):
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ def session_required(handler):
|
|||||||
|
|
||||||
@functools.wraps(handler)
|
@functools.wraps(handler)
|
||||||
def FindOrCreateSession(self):
|
def FindOrCreateSession(self):
|
||||||
client_key = auth.ParseKey(self.request.get('client_id', None))
|
client_key = auth.ParseKey(self.request_json.get('client_id', None))
|
||||||
|
|
||||||
# The hunt for a Profile begins.
|
# The hunt for a Profile begins.
|
||||||
if client_key:
|
if client_key:
|
||||||
@@ -77,7 +77,7 @@ def session_required(handler):
|
|||||||
|
|
||||||
ret = {
|
ret = {
|
||||||
'status': 'ok',
|
'status': 'ok',
|
||||||
'response': handler(self),
|
'responses': handler(self),
|
||||||
}
|
}
|
||||||
if client_key != self.client.key():
|
if client_key != self.client.key():
|
||||||
# Tell the client that this changed
|
# Tell the client that this changed
|
||||||
|
|||||||
10
lib/utils.py
10
lib/utils.py
@@ -25,6 +25,16 @@ from cosmopolite import config
|
|||||||
from cosmopolite.lib import auth
|
from cosmopolite.lib import auth
|
||||||
|
|
||||||
|
|
||||||
|
def expects_json(handler):
|
||||||
|
|
||||||
|
@functools.wraps(handler)
|
||||||
|
def ParseInput(self):
|
||||||
|
self.request_json = json.load(self.request.body_file)
|
||||||
|
return handler(self)
|
||||||
|
|
||||||
|
return ParseInput
|
||||||
|
|
||||||
|
|
||||||
def returns_json(handler):
|
def returns_json(handler):
|
||||||
|
|
||||||
@functools.wraps(handler)
|
@functools.wraps(handler)
|
||||||
|
|||||||
@@ -75,17 +75,39 @@ cosmopolite.Client.prototype.registerMessageHandlers_ = function() {
|
|||||||
}, this));
|
}, this));
|
||||||
};
|
};
|
||||||
|
|
||||||
cosmopolite.Client.prototype.sendRPC_ = function(command, data, onSuccess, delay) {
|
cosmopolite.Client.prototype.sendRPC_ = function(command, args, onSuccess) {
|
||||||
|
this.sendRPCs_([
|
||||||
|
{
|
||||||
|
'command': command,
|
||||||
|
'arguments': args,
|
||||||
|
'onSuccess': onSuccess,
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
cosmopolite.Client.prototype.sendRPCs_ = function(commands, delay) {
|
||||||
|
var request = {
|
||||||
|
'commands': [],
|
||||||
|
};
|
||||||
|
commands.forEach(function(command) {
|
||||||
|
var request_command = {
|
||||||
|
'command': command['command'],
|
||||||
|
};
|
||||||
|
if ('arguments' in command) {
|
||||||
|
request_command['arguments'] = command['arguments'];
|
||||||
|
}
|
||||||
|
request.commands.push(request_command);
|
||||||
|
});
|
||||||
if (this.namespace_ + ':client_id' in localStorage) {
|
if (this.namespace_ + ':client_id' in localStorage) {
|
||||||
data['client_id'] = localStorage[this.namespace_ + ':client_id'];
|
request['client_id'] = localStorage[this.namespace_ + ':client_id'];
|
||||||
}
|
}
|
||||||
if (this.namespace_ + ':google_user_id' in localStorage) {
|
if (this.namespace_ + ':google_user_id' in localStorage) {
|
||||||
data['google_user_id'] = localStorage[this.namespace_ + ':google_user_id'];
|
request['google_user_id'] = localStorage[this.namespace_ + ':google_user_id'];
|
||||||
}
|
}
|
||||||
this.$.ajax({
|
this.$.ajax({
|
||||||
url: this.urlPrefix_ + '/api/' + command,
|
url: this.urlPrefix_ + '/api',
|
||||||
type: 'post',
|
type: 'post',
|
||||||
data: data,
|
data: JSON.stringify(request),
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
context: this,
|
context: this,
|
||||||
})
|
})
|
||||||
@@ -99,7 +121,7 @@ cosmopolite.Client.prototype.sendRPC_ = function(command, data, onSuccess, delay
|
|||||||
}
|
}
|
||||||
if (data['status'] == 'retry') {
|
if (data['status'] == 'retry') {
|
||||||
// Discard delay
|
// Discard delay
|
||||||
this.sendRPC_(command, data, onSuccess);
|
this.sendRPCs_(commands, onSuccess);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data['status'] != 'ok') {
|
if (data['status'] != 'ok') {
|
||||||
@@ -109,8 +131,10 @@ cosmopolite.Client.prototype.sendRPC_ = function(command, data, onSuccess, delay
|
|||||||
// TODO(flamingcow): Refresh the page? Show an alert?
|
// TODO(flamingcow): Refresh the page? Show an alert?
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (onSuccess) {
|
for (var i = 0; i < data.responses.length; i++) {
|
||||||
this.$.proxy(onSuccess, this)(data.response);
|
if (commands[i]['onSuccess']) {
|
||||||
|
this.$.proxy(commands[i]['onSuccess'], this)(data.responses[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fail(function(xhr) {
|
.fail(function(xhr) {
|
||||||
@@ -118,31 +142,14 @@ cosmopolite.Client.prototype.sendRPC_ = function(command, data, onSuccess, delay
|
|||||||
xhr.getResponseHeader('Retry-After') ||
|
xhr.getResponseHeader('Retry-After') ||
|
||||||
Math.min(32, Math.max(2, delay || 2));
|
Math.min(32, Math.max(2, delay || 2));
|
||||||
console.log(
|
console.log(
|
||||||
'RPC ' + command + ' failed. Will retry in ' + intDelay + ' seconds');
|
'RPC failed. Will retry in ' + intDelay + ' seconds');
|
||||||
function retry() {
|
function retry() {
|
||||||
this.sendRPC_(command, data, onSuccess, Math.pow(intDelay, 2));
|
this.sendRPCs_(commands, Math.pow(intDelay, 2));
|
||||||
}
|
}
|
||||||
window.setTimeout(this.$.proxy(retry, this), intDelay * 1000);
|
window.setTimeout(this.$.proxy(retry, this), intDelay * 1000);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
cosmopolite.Client.prototype.getUser_ = function() {
|
|
||||||
this.sendRPC_('getUser', {}, function(data) {
|
|
||||||
if ('google_user' in data) {
|
|
||||||
if ('onLogin' in this.callbacks_) {
|
|
||||||
this.callbacks_['onLogin'](
|
|
||||||
data['google_user'],
|
|
||||||
this.urlPrefix_ + '/auth/logout');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ('onLogout' in this.callbacks_) {
|
|
||||||
this.callbacks_['onLogout'](
|
|
||||||
this.urlPrefix_ + '/auth/login');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
cosmopolite.Client.prototype.setValue = function(key, value, is_public) {
|
cosmopolite.Client.prototype.setValue = function(key, value, is_public) {
|
||||||
this.sendRPC_('setValue', {
|
this.sendRPC_('setValue', {
|
||||||
'key': key,
|
'key': key,
|
||||||
|
|||||||
Reference in New Issue
Block a user