From b285d5c1a3867d7eb011af4ab6c9816ca69029d0 Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Fri, 6 Oct 2017 09:07:28 +1300 Subject: [PATCH] Proxy fully operational; Initial API requests to Genius --- mopidy_iris/core.py | 25 ++++++++++-- mopidy_iris/handlers.py | 7 ++++ src/js/components/Modal/TrackInfoModal.js | 10 +++-- src/js/services/core/actions.js | 1 + src/js/services/genius/actions.js | 49 +++++++++++++---------- src/js/services/lastfm/actions.js | 1 + src/js/services/mopidy/middleware.js | 4 +- src/js/services/pusher/actions.js | 2 +- src/js/services/pusher/middleware.js | 4 +- src/js/services/spotify/actions.js | 1 + 10 files changed, 71 insertions(+), 33 deletions(-) diff --git a/mopidy_iris/core.py b/mopidy_iris/core.py index 1b3f60a3..24cf8563 100755 --- a/mopidy_iris/core.py +++ b/mopidy_iris/core.py @@ -563,7 +563,7 @@ class IrisCore(object): req = urllib2.Request(url, data_encoded) try: - response = urllib2.urlopen(req, timeout=15).read() + response = urllib2.urlopen(req, timeout=30).read() response_dict = json.loads(response) self.spotify_token = response_dict @@ -626,9 +626,12 @@ class IrisCore(object): target_request_headers = {} # Adjust headers + target_request_headers["Accept-Language"] = "*" target_request_headers["Accept-Encoding"] = "deflate" if "Content-Type" in target_request_headers: del target_request_headers["Content-Type"] + if "Host" in target_request_headers: + del target_request_headers["Host"] # Our request includes data, so make sure we POST the data if ('data' in data and data['data']): @@ -636,10 +639,17 @@ class IrisCore(object): # No data, so just a simple GET request else: + + # Strip out our origin content-length otherwise this confuses + # the target server as content-length doesn't apply to GET requests + if "Content-Length" in target_request_headers: + del target_request_headers["Content-Length"] + target_request = urllib2.Request(data['url'], headers=target_request_headers) + # Now actually attempt the request try: - target_response = urllib2.urlopen(target_request, timeout=15) + target_response = urllib2.urlopen(target_request, timeout=30) target_response_body = target_response.read() try: @@ -654,7 +664,6 @@ class IrisCore(object): } except urllib2.HTTPError as e: - self.raven_client.captureException() return { 'status': 0, 'message': 'Could not complete proxy request', @@ -665,7 +674,6 @@ class IrisCore(object): } except urllib2.URLError as e: - self.raven_client.captureException() return { 'status': 0, 'message': 'Could not complete proxy request', @@ -673,3 +681,12 @@ class IrisCore(object): 'response_code': int(e.code), 'original_request': data } + + else: + return { + 'status': 0, + 'message': 'Could not complete proxy request', + 'source': 'proxy_request', + 'response_code': null, + 'original_request': data + } diff --git a/mopidy_iris/handlers.py b/mopidy_iris/handlers.py index a49193aa..c7d67ade 100755 --- a/mopidy_iris/handlers.py +++ b/mopidy_iris/handlers.py @@ -122,10 +122,17 @@ class HttpHandler(tornado.web.RequestHandler): def set_default_headers(self): self.set_header("Access-Control-Allow-Origin", "*") + self.set_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Client-Security-Token, Accept-Encoding") def initialize(self, core, config): self.core = core self.config = config + + # Options request + # This is a preflight request for CORS requests + def options(self, slug=None): + self.set_status(204) + self.finish() def get(self, slug=None): diff --git a/src/js/components/Modal/TrackInfoModal.js b/src/js/components/Modal/TrackInfoModal.js index 8d33c49e..0a1e74c4 100755 --- a/src/js/components/Modal/TrackInfoModal.js +++ b/src/js/components/Modal/TrackInfoModal.js @@ -19,13 +19,17 @@ export default class TrackInfoModal extends React.Component{ } render(){ - var track = this.props.current_track; + if (this.props.current_track){ + var track = this.props.current_track; + } else { + var track = null + } return (

Track info

-

{track.name} by

- {track.annotations ? track.annotations.id : "No annotations"} + {track ?

{track.name} by

: null} + {track && track.annotations ? track.annotations.id : "No annotations"}
) } diff --git a/src/js/services/core/actions.js b/src/js/services/core/actions.js index 76b84123..15d54163 100755 --- a/src/js/services/core/actions.js +++ b/src/js/services/core/actions.js @@ -7,6 +7,7 @@ export function getBroadcasts(){ return (dispatch, getState) => { var config = { method: 'GET', + timeout: 15000, url: 'https://gist.githubusercontent.com/jaedb/b677dccf80daf3ccb2ef12e96e495677/raw' } $.ajax(config).then( diff --git a/src/js/services/genius/actions.js b/src/js/services/genius/actions.js index 466142f1..123ad700 100755 --- a/src/js/services/genius/actions.js +++ b/src/js/services/genius/actions.js @@ -4,7 +4,7 @@ var uiActions = require('../ui/actions') var helpers = require('../../helpers') /** - * Send an ajax request to the Spotify API + * Send an ajax request * * @param dispatch obj * @param getState obj @@ -17,26 +17,33 @@ const sendRequest = (dispatch, getState, endpoint) => { dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint)); var config = { - method: 'GET', + method: 'POST', cache: false, - url: 'https://api.genius.com/'+endpoint+'&access_token=2AGP9sfzKQcxfKSZuGa_3lqsDIpuOiTGT7-vhJYcKaaDjHIIA2HICsxXCiC30Xxi' + timeout: 15000, + headers: { + Authorization: 'Bearer 2AGP9sfzKQcxfKSZuGa_3lqsDIpuOiTGT7-vhJYcKaaDjHIIA2HICsxXCiC30Xxi' + }, + data: JSON.stringify({ + url: 'https://api.genius.com/'+endpoint + }), + url: '//'+getState().mopidy.host+':'+getState().mopidy.port+'/iris/http/proxy_request' }; $.ajax(config).then( - response => { - dispatch(uiActions.stopLoading(loader_key)); - resolve(response.response); - }, - (xhr, status, error) => { - dispatch(uiActions.stopLoading(loader_key)); - reject({ - config: config, - xhr: xhr, - status: status, - error: error - }); - } - ) + response => { + dispatch(uiActions.stopLoading(loader_key)); + resolve(response.response); + }, + (xhr, status, error) => { + dispatch(uiActions.stopLoading(loader_key)); + reject({ + config: config, + xhr: xhr, + status: status, + error: error + }); + } + ) }) } @@ -49,15 +56,15 @@ export function getTrackInfo(track){ } query += track.name; - sendRequest(dispatch, getState, 'search?q='+query) + sendRequest(dispatch, getState, 'search?q='+encodeURIComponent(query)) .then( response => { - if (response.hits && response.hits.length > 0){ + if (response.response.hits && response.response.hits.length > 0){ dispatch({ type: 'TRACK_LOADED', key: track.uri, track: { - annotations: response.hits[0].result + annotations: response.response.hits[0].result } }); } @@ -68,6 +75,6 @@ export function getTrackInfo(track){ error )); } - ) + ); } } diff --git a/src/js/services/lastfm/actions.js b/src/js/services/lastfm/actions.js index 2047b278..6b072ba1 100755 --- a/src/js/services/lastfm/actions.js +++ b/src/js/services/lastfm/actions.js @@ -19,6 +19,7 @@ const sendRequest = ( dispatch, getState, params ) => { var config = { method: 'GET', cache: true, + timeout: 15000, url: '//ws.audioscrobbler.com/2.0/?format=json&api_key=4320a3ef51c9b3d69de552ac083c55e3&'+params } diff --git a/src/js/services/mopidy/middleware.js b/src/js/services/mopidy/middleware.js index 0a1f24bd..a5f6be94 100755 --- a/src/js/services/mopidy/middleware.js +++ b/src/js/services/mopidy/middleware.js @@ -159,13 +159,13 @@ const MopidyMiddleware = (function(){ var loader_key = helpers.generateGuid() store.dispatch(uiActions.startLoading(loader_key, 'mopidy_'+property)) - // Start our 10 second timeout + // Start our 15 second timeout var timeout = setTimeout( function(){ store.dispatch(uiActions.stopLoading(loader_key)); reject({message: "Request timed out", call: call, value: value}); }, - 10000 + 15000 ); mopidyObject(value) diff --git a/src/js/services/pusher/actions.js b/src/js/services/pusher/actions.js index 1df09c0f..572c0742 100755 --- a/src/js/services/pusher/actions.js +++ b/src/js/services/pusher/actions.js @@ -59,7 +59,7 @@ export function sendAuthorization( recipient_connectionid, authorization, me ){ return { type: 'PUSHER_DELIVER_MESSAGE', data: { - to: recipient_connectionid, + connection_id: recipient_connectionid, message: { type: 'spotify_authorization', authorization: authorization, diff --git a/src/js/services/pusher/middleware.js b/src/js/services/pusher/middleware.js index bfe09b06..26a07d22 100755 --- a/src/js/services/pusher/middleware.js +++ b/src/js/services/pusher/middleware.js @@ -63,13 +63,13 @@ const PusherMiddleware = (function(){ store.dispatch(uiActions.startLoading(request_id, 'pusher_'+method)) - // Start our 10 second timeout + // Start our 15 second timeout var timeout = setTimeout( function(){ store.dispatch(uiActions.stopLoading(request_id)); reject({message: "Request timed out", method: method, data: data}); }, - 10000 + 15000 ); // add query to our deferred responses diff --git a/src/js/services/spotify/actions.js b/src/js/services/spotify/actions.js index 7c4fab26..6b9f0ae4 100755 --- a/src/js/services/spotify/actions.js +++ b/src/js/services/spotify/actions.js @@ -33,6 +33,7 @@ const sendRequest = ( dispatch, getState, endpoint, method = 'GET', data = false method: method, url: url, cached: true, + timeout: 15000, headers: { Authorization: 'Bearer '+ response }