Revamp of backend method structure; Proxy for any URL

This commit is contained in:
James Barnsley
2017-10-04 16:47:15 +13:00
parent dcdecd989a
commit b2c7478cf9
12 changed files with 370 additions and 96 deletions

View File

@ -1,13 +1,14 @@
import ReactGA from 'react-ga'
var coreActions = require('./actions.js')
var uiActions = require('../ui/actions.js')
var pusherActions = require('../pusher/actions.js')
var mopidyActions = require('../mopidy/actions.js')
var spotifyActions = require('../spotify/actions.js')
var lastfmActions = require('../lastfm/actions.js')
var helpers = require('../../helpers.js')
var coreActions = require('./actions.js');
var uiActions = require('../ui/actions.js');
var pusherActions = require('../pusher/actions.js');
var mopidyActions = require('../mopidy/actions.js');
var spotifyActions = require('../spotify/actions.js');
var lastfmActions = require('../lastfm/actions.js');
var geniusActions = require('../genius/actions.js');
var helpers = require('../../helpers.js');
const CoreMiddleware = (function(){
@ -71,9 +72,8 @@ const CoreMiddleware = (function(){
break;
case 'CORE_START_SERVICES':
store.dispatch(mopidyActions.connect())
store.dispatch(pusherActions.connect())
store.dispatch(lastfmActions.connect())
store.dispatch(mopidyActions.connect());
store.dispatch(pusherActions.connect());
next(action)
break

View File

@ -0,0 +1,73 @@
var coreActions = require('../core/actions')
var uiActions = require('../ui/actions')
var helpers = require('../../helpers')
/**
* Send an ajax request to the Spotify API
*
* @param dispatch obj
* @param getState obj
* @param endpoint params = the url params to send
**/
const sendRequest = (dispatch, getState, endpoint) => {
return new Promise( (resolve, reject) => {
var loader_key = helpers.generateGuid();
dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint));
var config = {
method: 'GET',
cache: false,
url: 'https://api.genius.com/'+endpoint+'&access_token=2AGP9sfzKQcxfKSZuGa_3lqsDIpuOiTGT7-vhJYcKaaDjHIIA2HICsxXCiC30Xxi'
};
$.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
});
}
)
})
}
export function getTrackInfo(track){
return (dispatch, getState) => {
var query = '';
for (var i = 0; i < track.artists.length; i++){
query += track.artists[i].name+' ';
}
query += track.name;
sendRequest(dispatch, getState, 'search?q='+query)
.then(
response => {
if (response.hits && response.hits.length > 0){
dispatch({
type: 'TRACK_LOADED',
key: track.uri,
track: {
annotations: response.hits[0].result
}
});
}
},
error => {
dispatch(coreActions.handleException(
'Could not get track info',
error
));
}
)
}
}

View File

@ -0,0 +1,18 @@
export default function reducer(genius = {}, action){
switch (action.type) {
case 'GENIUS_CONNECT':
case 'GENIUS_CONNECTING':
return Object.assign({}, genius, { connected: false, connecting: true });
case 'GENIUS_CONNECTED':
return Object.assign({}, genius, { connected: true, connecting: false });
default:
return genius
}
}

View File

@ -1685,10 +1685,14 @@ const MopidyMiddleware = (function(){
if (action.data && action.data.track){
// Fire off our universal track index loader
store.dispatch({ type: 'TRACK_LOADED', key: action.data.track.uri, track: action.data.track })
store.dispatch({
type: 'TRACK_LOADED',
key: action.data.track.uri,
track: action.data.track
});
// We've got Spotify running, and it's a spotify track - go straight to the source!
if (helpers.uriSource(action.data.track.uri) == 'spotify' && store.getState().spotify.access != 'none'){
if (helpers.uriSource(action.data.track.uri) == 'spotify' && store.getState().spotify.enabled){
store.dispatch(spotifyActions.getTrack(action.data.track.uri))
// Some other source, rely on Mopidy backends to do their work

View File

@ -24,7 +24,9 @@ const sendRequest = ( dispatch, getState, endpoint, method = 'GET', data = false
// prepend the API baseurl, unless the endpoint already has it (ie pagination requests)
var url = 'https://api.spotify.com/v1/'+endpoint
if (endpoint.startsWith('https://api.spotify.com/')) url = endpoint;
if (endpoint.startsWith('https://api.spotify.com/')){
url = endpoint;
}
// create our ajax request config
var config = {