Switch to a multi-request/response API to make batching possible.

This commit is contained in:
Ian Gulliver
2014-05-06 13:38:40 -07:00
parent 0369266d60
commit cf05c0f620
6 changed files with 116 additions and 91 deletions

64
api.py
View File

@@ -26,21 +26,14 @@ from cosmopolite.lib import utils
import config import config
class SetValue(webapp2.RequestHandler):
@utils.chaos_monkey
@utils.returns_json
@utils.local_namespace
@security.google_user_xsrf_protection
@security.weak_security_checks
@session.session_required
@db.transactional() @db.transactional()
def post(self): def SetValue(google_user, client, args):
entry_key = self.request.get('key') entry_key = args['key']
entry_value = self.request.get('value') entry_value = args['value']
public = (self.request.get('public') == 'true') public = (args['public'] == 'true')
entries = (models.StateEntry.all() entries = (models.StateEntry.all()
.ancestor(self.client.parent_key()) .ancestor(client.parent_key())
.filter('entry_key =', entry_key) .filter('entry_key =', entry_key)
.fetch(1)) .fetch(1))
if entries: if entries:
@@ -49,7 +42,7 @@ class SetValue(webapp2.RequestHandler):
entry.public = public entry.public = public
else: else:
entry = models.StateEntry( entry = models.StateEntry(
parent=self.client.parent_key(), parent=client.parent_key(),
entry_key=entry_key, entry_key=entry_key,
entry_value=entry_value, entry_value=entry_value,
public=public) public=public)
@@ -57,30 +50,23 @@ class SetValue(webapp2.RequestHandler):
entry.put() entry.put()
msg = entry.ToMessage() msg = entry.ToMessage()
clients = (models.Client.all() clients = (models.Client.all()
.ancestor(self.client.parent_key())) .ancestor(client.parent_key()))
for client in clients: for client in clients:
client.SendMessage(msg) client.SendMessage(msg)
return {} return {}
class CreateChannel(webapp2.RequestHandler): def CreateChannel(google_user, client, args):
@utils.chaos_monkey
@utils.returns_json
@utils.local_namespace
@security.google_user_xsrf_protection
@security.weak_security_checks
@session.session_required
def post(self):
token = channel.create_channel( token = channel.create_channel(
client_id=str(self.client.key()), client_id=str(client.key()),
duration_minutes=config.CHANNEL_DURATION_SECONDS / 60) duration_minutes=config.CHANNEL_DURATION_SECONDS / 60)
messages = [x.ToMessage() messages = [x.ToMessage()
for x in self.client.parent().GetStateEntries()] for x in client.parent().GetStateEntries()]
if self.verified_google_user: if google_user:
messages.append({ messages.append({
'message_type': 'login', 'message_type': 'login',
'google_user': self.verified_google_user.email(), 'google_user': google_user.email(),
}) })
else: else:
messages.append({ messages.append({
@@ -93,7 +79,29 @@ class CreateChannel(webapp2.RequestHandler):
} }
class APIWrapper(webapp2.RequestHandler):
_COMMANDS = {
'createChannel': CreateChannel,
'setValue': SetValue,
}
@utils.chaos_monkey
@utils.expects_json
@utils.returns_json
@utils.local_namespace
@security.google_user_xsrf_protection
@security.weak_security_checks
@session.session_required
def post(self):
ret = []
for command in self.request_json['commands']:
callback = self._COMMANDS[command['command']]
result = callback(self.verified_google_user, self.client, command.get('arguments', {}))
ret.append(result)
return ret
app = webapp2.WSGIApplication([ app = webapp2.WSGIApplication([
(config.URL_PREFIX + '/api/createChannel', CreateChannel), (config.URL_PREFIX + '/api', APIWrapper),
(config.URL_PREFIX + '/api/setValue', SetValue),
]) ])

View File

@@ -1,5 +1,5 @@
handlers: handlers:
- url: /cosmopolite/api/.* - url: /cosmopolite/api
script: cosmopolite.api.app script: cosmopolite.api.app
secure: always secure: always

View File

@@ -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 {

View File

@@ -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

View File

@@ -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)

View File

@@ -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,