Move message handling in RPC responses out of individual commands and to a general property of the response.

This commit is contained in:
Ian Gulliver
2014-05-06 13:47:57 -07:00
parent cf05c0f620
commit bb5c0752b1
3 changed files with 17 additions and 10 deletions

16
api.py
View File

@@ -94,11 +94,21 @@ class APIWrapper(webapp2.RequestHandler):
@security.weak_security_checks @security.weak_security_checks
@session.session_required @session.session_required
def post(self): def post(self):
ret = [] ret = {
'status': 'ok',
'responses': [],
'messages': [],
}
for command in self.request_json['commands']: for command in self.request_json['commands']:
callback = self._COMMANDS[command['command']] callback = self._COMMANDS[command['command']]
result = callback(self.verified_google_user, self.client, command.get('arguments', {})) result = callback(
ret.append(result) self.verified_google_user,
self.client,
command.get('arguments', {}))
# Magic: if result contains "messages", haul them up a level so the
# client can see them as a single stream.
ret['messages'].extend(result.pop('messages', []))
ret['responses'].append(result)
return ret return ret

View File

@@ -75,10 +75,7 @@ def session_required(handler):
else: else:
self.client = models.Client.FromGoogleUser(self.verified_google_user) self.client = models.Client.FromGoogleUser(self.verified_google_user)
ret = { ret = handler(self)
'status': 'ok',
'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
ret['client_id'] = auth.Sign(self.client.key()) ret['client_id'] = auth.Sign(self.client.key())

View File

@@ -136,6 +136,9 @@ cosmopolite.Client.prototype.sendRPCs_ = function(commands, delay) {
this.$.proxy(commands[i]['onSuccess'], this)(data.responses[i]); this.$.proxy(commands[i]['onSuccess'], this)(data.responses[i]);
} }
} }
// Handle messages that were immediately available as if they came over the
// channel.
data['messages'].forEach(this.onServerMessage_, this);
}) })
.fail(function(xhr) { .fail(function(xhr) {
var intDelay = var intDelay =
@@ -181,9 +184,6 @@ cosmopolite.Client.prototype.onCreateChannel_ = function(data) {
onmessage: this.$.proxy(this.onSocketMessage_, this), onmessage: this.$.proxy(this.onSocketMessage_, this),
onerror: this.$.proxy(this.onSocketError_, this), onerror: this.$.proxy(this.onSocketError_, this),
}); });
// Handle messages that were immediately available as if they came over the
// channel.
data['messages'].forEach(this.onServerMessage_, this);
}; };
cosmopolite.Client.prototype.onSocketOpen_ = function() { cosmopolite.Client.prototype.onSocketOpen_ = function() {