Passing method to response

This commit is contained in:
James Barnsley
2018-03-13 08:57:40 +13:00
parent 6ec5fd388e
commit f615c6c8d0
7 changed files with 1931 additions and 976 deletions

View File

@ -305,10 +305,12 @@ class IrisCore(object):
message={ message={
'method': "pusher_connection_added", 'method': "pusher_connection_added",
'params': { 'params': {
'connection_id': connection_id, 'connection': {
'client_id': client['client_id'], 'connection_id': connection_id,
'username': client['username'], 'client_id': client['client_id'],
'ip': client['ip'] 'username': client['username'],
'ip': client['ip']
}
} }
} }
) )

View File

@ -75,19 +75,22 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
if 'jsonrpc' not in message: if 'jsonrpc' not in message:
self.handle_response(id=id, error={'id': id, 'code': 32602, 'message': 'Invalid JSON-RPC request (missing property "jsonrpc")'}) self.handle_response(id=id, error={'id': id, 'code': 32602, 'message': 'Invalid JSON-RPC request (missing property "jsonrpc")'})
params = message['params'] if 'params' in message:
params = message['params']
# Handle hard-coded connection_id in messages # Handle hard-coded connection_id in messages
# Otherwise include the origin connection of this message # Otherwise include the origin connection of this message
if 'connection_id' not in params: if 'connection_id' not in params:
params['connection_id'] = self.connection_id message['params']['connection_id'] = self.connection_id
else:
params = {}
# call the method, as specified in payload # call the method, as specified in payload
if 'method' in message: if 'method' in message:
# make sure the method exists # make sure the method exists
if hasattr(mem.iris, message['method']): if hasattr(mem.iris, message['method']):
getattr(mem.iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_response(id=id, response=response, error=error)) getattr(mem.iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_response(id=id, method=message['method'], response=response, error=error))
else: else:
self.handle_response(error={'id': id, 'code': 32601, 'message': 'Method "'+message['method']+'" does not exist'}, id=id) self.handle_response(error={'id': id, 'code': 32601, 'message': 'Method "'+message['method']+'" does not exist'}, id=id)
@ -105,12 +108,14 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
# This is just our callback from an Async request # This is just our callback from an Async request
## ##
def handle_response(self, *args, **kwargs): def handle_response(self, *args, **kwargs):
id = kwargs.get('id', False)
method = kwargs.get('method', None)
response = kwargs.get('response', None) response = kwargs.get('response', None)
error = kwargs.get('error', None) error = kwargs.get('error', None)
id = kwargs.get('id', False)
request_response = { request_response = {
'id': id, 'id': id,
'jsonrpc': '2.0' 'jsonrpc': '2.0',
'method': method
} }
# We've been given an error # We've been given an error
@ -131,7 +136,7 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
request_response['result'] = response request_response['result'] = response
# Respond to the original request # Respond to the original request
mem.iris.send_message(connection_id=self.connection_id, data=request_response) mem.iris.send_message(connection_id=self.connection_id, message=request_response)
@ -161,7 +166,7 @@ class HttpHandler(tornado.web.RequestHandler):
# make sure the method exists # make sure the method exists
if hasattr(mem.iris, slug): if hasattr(mem.iris, slug):
getattr(mem.iris, slug)(request=self.request, callback=lambda response, error=False: self.handle_response(id=id, response=response, error=error)) getattr(mem.iris, slug)(request=self.request, callback=lambda response, error=False: self.handle_response(id=id, method=slug, response=response, error=error))
else: else:
self.handle_response(id=self.request_id, error={'code': 32601, 'message': "Method "+slug+" does not exist"}) self.handle_response(id=self.request_id, error={'code': 32601, 'message': "Method "+slug+" does not exist"})
@ -181,7 +186,7 @@ class HttpHandler(tornado.web.RequestHandler):
# make sure the method exists # make sure the method exists
if hasattr(mem.iris, slug): if hasattr(mem.iris, slug):
try: try:
getattr(mem.iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_response(id=id, response=response, error=error)) getattr(mem.iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_response(id=id, method=slug, response=response, error=error))
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
self.handle_response(id=id, error={'code': 32601, 'message': "Invalid JSON payload"}) self.handle_response(id=id, error={'code': 32601, 'message': "Invalid JSON payload"})
@ -197,11 +202,13 @@ class HttpHandler(tornado.web.RequestHandler):
## ##
def handle_response(self, *args, **kwargs): def handle_response(self, *args, **kwargs):
id = kwargs.get('id', None) id = kwargs.get('id', None)
method = kwargs.get('method', None)
response = kwargs.get('response', None) response = kwargs.get('response', None)
error = kwargs.get('error', None) error = kwargs.get('error', None)
request_response = { request_response = {
'id': id, 'id': id,
'jsonrpc': '2.0' 'jsonrpc': '2.0',
'method': method
} }
if error: if error:

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -38,7 +38,7 @@
// Release details // Release details
// These are automatically injected to built HTML // These are automatically injected to built HTML
var build = "1520840408"; var build = "1520880835";
var version = "3.14.2"; var version = "3.14.2";
// Construct the script tag // Construct the script tag

View File

@ -20,24 +20,24 @@ const PusherMiddleware = (function(){
const handleMessage = (ws, store, message) => { const handleMessage = (ws, store, message) => {
if (store.getState().ui.log_pusher){ if (store.getState().ui.log_pusher){
console.log('Pusher', message); console.log('Pusher log', message);
} }
// Response with request_id // Response with request_id
if (message.request_id !== undefined && message.request_id){ if (message.id !== undefined && message.id){
// Response matches a pending request // Response matches a pending request
if (deferredRequests[message.request_id] !== undefined){ if (deferredRequests[message.id] !== undefined){
store.dispatch(uiActions.stopLoading(message.request_id)); store.dispatch(uiActions.stopLoading(message.id));
// Response is an error // Response is an error
if (message.status <= 0){ if (message.error !== undefined){
deferredRequests[message.request_id].reject(message); deferredRequests[message.id].reject(message.error);
// Successful response // Successful response
} else { } else {
deferredRequests[message.request_id].resolve(message); deferredRequests[message.id].resolve(message.result);
} }
// Hmm, the response doesn't appear to be for us? // Hmm, the response doesn't appear to be for us?
@ -52,27 +52,34 @@ const PusherMiddleware = (function(){
} else { } else {
// Broadcast of an error // Broadcast of an error
if (message.status !== undefined && message.status <= 0){ if (message.error !== undefined){
store.dispatch(coreActions.handleException( store.dispatch(coreActions.handleException(
'Pusher: '+message.message, 'Pusher: '+message.error.message,
message message
)); ));
} else { } else {
message.type = 'PUSHER_'+message.method.toUpperCase(); store.dispatch(Object.assign(
store.dispatch(message); {},
message.params,
{
type: message.method.toUpperCase()
}
));
} }
} }
} }
const request = (store, method, params = {}) => { const request = (store, method, params = null) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var id = helpers.generateGuid(); var id = helpers.generateGuid();
var message = { var message = {
jsonrpc: 2.0, jsonrpc: '2.0',
id: id, id: id,
method: method, method: method
params: params, }
if (params){
message.params = params;
} }
socket.send(JSON.stringify(message)); socket.send(JSON.stringify(message));
@ -83,13 +90,9 @@ const PusherMiddleware = (function(){
function(){ function(){
store.dispatch(uiActions.stopLoading(id)); store.dispatch(uiActions.stopLoading(id));
reject({ reject({
jsonrpc: 2.0,
id: id, id: id,
error: { code: 32300,
id: id, message: "Request timed out"
code: 32300,
message: "Request timed out"
}
}); });
}, },
30000 30000
@ -113,7 +116,7 @@ const PusherMiddleware = (function(){
socket.close(); socket.close();
} }
store.dispatch({ type: 'PUSHER_CONNECTING' }); store.dispatch({type: 'PUSHER_CONNECTING'});
var state = store.getState(); var state = store.getState();
var connection = { var connection = {
@ -131,9 +134,10 @@ const PusherMiddleware = (function(){
[ connection.client_id, connection.connection_id, connection.username ] [ connection.client_id, connection.connection_id, connection.username ]
); );
socket.onmessage = (message) => { socket.onopen = () => {
var message = JSON.parse(message.data); store.dispatch({
handleMessage(socket, store, message ) type: 'PUSHER_CONNECTED'
});
}; };
socket.onclose = () => { socket.onclose = () => {
@ -147,6 +151,11 @@ const PusherMiddleware = (function(){
}, 5000); }, 5000);
}; };
socket.onmessage = (message) => {
var message = JSON.parse(message.data);
handleMessage(socket, store, message);
};
break; break;
case 'PUSHER_CONNECTED': case 'PUSHER_CONNECTED':
@ -277,7 +286,7 @@ const PusherMiddleware = (function(){
response => { response => {
store.dispatch({ store.dispatch({
type: 'PUSHER_VERSION', type: 'PUSHER_VERSION',
version: response.result version: response.version
}) })
}, },
error => { error => {
@ -297,14 +306,14 @@ const PusherMiddleware = (function(){
console.log(response) console.log(response)
store.dispatch({ store.dispatch({
type: 'PUSHER_CONFIG', type: 'PUSHER_CONFIG',
config: response.result config: response.config
}); });
var core = store.getState().core; var core = store.getState().core;
if (!core.country || !core.locale){ if (!core.country || !core.locale){
store.dispatch(coreActions.set({ store.dispatch(coreActions.set({
country: response.result.country, country: response.config.country,
locale: response.result.locale locale: response.config.locale
})) }))
} }
}, },
@ -323,7 +332,7 @@ const PusherMiddleware = (function(){
response => { response => {
store.dispatch({ store.dispatch({
type: 'PUSHER_CONNECTIONS', type: 'PUSHER_CONNECTIONS',
connections: response.result connections: response.connections
}) })
}, },
error => { error => {
@ -346,7 +355,7 @@ const PusherMiddleware = (function(){
response => { response => {
store.dispatch({ store.dispatch({
type: 'PUSHER_RADIO', type: 'PUSHER_RADIO',
radio: response.result.radio radio: response.radio
}); });
}, },
error => { error => {

View File

@ -32,9 +32,9 @@ export default function reducer(pusher = {}, action){
return Object.assign({}, pusher, { connections: connections }); return Object.assign({}, pusher, { connections: connections });
case 'PUSHER_CONNECTION_ADDED': case 'PUSHER_CONNECTION_ADDED':
case 'PUSHER_CONNECTION_UPDATED': case 'PUSHER_CONNECTION_CHANGED':
var connections = Object.assign({}, pusher.connections) var connections = Object.assign({}, pusher.connections)
connections[action.connection.connection_id] = action.connection connections[action.connection.connection_id] = action.connection;
return Object.assign({}, pusher, { connections: connections }); return Object.assign({}, pusher, { connections: connections });
case 'PUSHER_CONNECTION_REMOVED': case 'PUSHER_CONNECTION_REMOVED':