diff --git a/mopidy_iris/core.py b/mopidy_iris/core.py index 26a63cd3..399dd915 100755 --- a/mopidy_iris/core.py +++ b/mopidy_iris/core.py @@ -1056,7 +1056,7 @@ class IrisCore(pykka.ThreadingActor): # passing token to frontend for javascript requests without use of the Authorization Code Flow. ## - def get_lyrics(self, *args, **kwargs): + async def get_lyrics(self, *args, **kwargs): callback = kwargs.get('callback', False) request = kwargs.get('request', False) error = False @@ -1089,15 +1089,19 @@ class IrisCore(pykka.ThreadingActor): } if error: - if (callback): - callback(False, error) - return - else: - return error + return error + + try: + http_client = AsyncHTTPClient() + http_response = await http_client.fetch(url) + callback(http_response, False) - http_request = tornado.httpclient.HTTPRequest(url) - http_client = tornado.httpclient.HTTPClient() - http_client.fetch(http_request, callback=callback) + except (urllib.error.HTTPError, urllib.error.URLError) as e: + error = json.loads(e.read()) + error = {'message': 'Could not fetch Spotify recommendations: '+error['error_description']} + logger.error('Could not fetch Spotify recommendations: '+error['error_description']) + logger.debug(error) + return error ## diff --git a/mopidy_iris/handlers.py b/mopidy_iris/handlers.py index fc4590f4..08a3245c 100755 --- a/mopidy_iris/handlers.py +++ b/mopidy_iris/handlers.py @@ -201,6 +201,9 @@ class HttpHandler(tornado.web.RequestHandler): 'method': method } + print(response) + print(error) + if error: request_response['error'] = error self.set_status(400) @@ -209,7 +212,7 @@ class HttpHandler(tornado.web.RequestHandler): # We've been handed an AsyncHTTPClient callback. This is the case # when our request calls subsequent external requests (eg Spotify, Genius). # We don't need to wrap non-HTTPResponse responses as these are dicts - elif isinstance(response, tornado.httpclient.HTTPResponse): + elif isinstance(response, tornado.httpclient.HTTPResponse) or isinstance(response, tornado.httpclient.AsyncHTTPResponse): # Digest JSON responses into JSON content_type = response.headers.get('Content-Type')