Using lambda to append info to callbacks; Only async for httprequests
This commit is contained in:
@ -7,7 +7,7 @@ import tornado.websocket
|
||||
import tornado.ioloop
|
||||
import tornado.httpclient
|
||||
import requests
|
||||
from promise import Promise
|
||||
import time
|
||||
from mopidy import config, ext
|
||||
from mopidy.core import CoreListener
|
||||
from pkg_resources import parse_version
|
||||
@ -664,19 +664,6 @@ class IrisCore(object):
|
||||
request = tornado.httpclient.HTTPRequest(data['url'], headers=headers, validate_cert=False)
|
||||
http_client.fetch(request, callback=callback)
|
||||
|
||||
|
||||
# Attempt to decode body as JSON, otherwise just return plain text
|
||||
try:
|
||||
return {
|
||||
'response_code': int(response.status_code),
|
||||
'response': response.json()
|
||||
}
|
||||
except:
|
||||
return {
|
||||
'response_code': int(response.status_code),
|
||||
'response': response.text
|
||||
}
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
return {
|
||||
'status': 0,
|
||||
@ -689,22 +676,19 @@ class IrisCore(object):
|
||||
|
||||
|
||||
def test(self, *args, **kwargs):
|
||||
|
||||
print "running test 1"
|
||||
response = requests.get("http://platform.james.plasticstudio.co/test.php?sleep=1")
|
||||
|
||||
return Promise(
|
||||
lambda resolve, reject: resolve(response.text)
|
||||
)
|
||||
|
||||
callback = kwargs.get('callback', None)
|
||||
time.sleep(1)
|
||||
callback({
|
||||
'status': 1,
|
||||
'message': "Slept for one"
|
||||
})
|
||||
|
||||
def test2(self, *args, **kwargs):
|
||||
|
||||
print "running test 5"
|
||||
response = requests.get("http://test.barnsley.nz/test.php?sleep=5")
|
||||
|
||||
return Promise(
|
||||
lambda resolve, reject: resolve(response.text)
|
||||
)
|
||||
callback = kwargs.get('callback', None)
|
||||
time.sleep(5)
|
||||
callback({
|
||||
'status': 1,
|
||||
'message': "Slept for FIVE!"
|
||||
})
|
||||
|
||||
|
||||
|
||||
@ -76,33 +76,31 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
data['connection_id'] = self.connection_id
|
||||
|
||||
if 'request_id' in message:
|
||||
data['request_id'] = message['request_id']
|
||||
request_id = message['request_id']
|
||||
else:
|
||||
data['request_id'] = False
|
||||
request_id = False
|
||||
|
||||
# call the method, as specified in payload
|
||||
if 'method' in message:
|
||||
|
||||
# make sure the method exists
|
||||
if hasattr(mem.iris, message['method']):
|
||||
getattr(mem.iris, message['method'])(data=data, callback=self.handle_response)
|
||||
getattr(mem.iris, message['method'])(data=data, callback=lambda response: self.handle_response(response=response, request_id=request_id))
|
||||
|
||||
else:
|
||||
mem.iris.raven_client.captureMessage("Method "+message['method']+" does not exist")
|
||||
response = {
|
||||
self.handle_response({
|
||||
'status': 0,
|
||||
'message': 'Method "'+message['method']+'" does not exist',
|
||||
'request_id': request_id
|
||||
}
|
||||
mem.iris.send_message(connection_id=self.connection_id, data=response)
|
||||
})
|
||||
else:
|
||||
mem.iris.raven_client.captureMessage("Method key missing from request")
|
||||
response = {
|
||||
self.handle_response({
|
||||
'status': 0,
|
||||
'message': 'Method key missing from request',
|
||||
'request_id': request_id
|
||||
}
|
||||
mem.iris.send_message(connection_id=self.connection_id, data=response)
|
||||
})
|
||||
|
||||
|
||||
def on_close(self):
|
||||
@ -112,9 +110,12 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
# Handle a response from our core
|
||||
# This is just our callback from an Async request
|
||||
##
|
||||
def handle_response(self, response):
|
||||
def handle_response(self, *args, **kwargs):
|
||||
response = kwargs.get('response', None)
|
||||
request_id = kwargs.get('request_id', False)
|
||||
|
||||
if isinstance(response, tornado.httpclient.HTTPResponse):
|
||||
response_obj = {
|
||||
response = {
|
||||
'body': response.body,
|
||||
'request_id': request_id
|
||||
}
|
||||
@ -123,7 +124,6 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
mem.iris.send_message(connection_id=self.connection_id, data=response)
|
||||
|
||||
mem.iris.send_message(connection_id=self.connection_id, data=response)
|
||||
self.finish()
|
||||
|
||||
|
||||
|
||||
@ -151,7 +151,7 @@ class HttpHandler(tornado.web.RequestHandler):
|
||||
|
||||
# make sure the method exists
|
||||
if hasattr(mem.iris, slug):
|
||||
self.handle_request(getattr(mem.iris, slug)(request=self.request))
|
||||
getattr(mem.iris, slug)(request=self.request, callback=self.handle_response)
|
||||
|
||||
else:
|
||||
mem.iris.raven_client.captureMessage("Method "+slug+" does not exist")
|
||||
@ -182,15 +182,6 @@ class HttpHandler(tornado.web.RequestHandler):
|
||||
})
|
||||
self.finish()
|
||||
|
||||
##
|
||||
# Handle a response from our core
|
||||
# This is just our callback from an Async request
|
||||
##
|
||||
def handle_request(self, request):
|
||||
response = request.then(lambda response: response).get()
|
||||
self.write(response)
|
||||
self.finish()
|
||||
|
||||
##
|
||||
# Handle a response from our core
|
||||
# This is just our callback from an Async request
|
||||
|
||||
Reference in New Issue
Block a user