Adding Genius as a full authenticator
This commit is contained in:
@ -4,41 +4,112 @@ var uiActions = require('../ui/actions')
|
||||
var helpers = require('../../helpers')
|
||||
|
||||
/**
|
||||
* Send an ajax request
|
||||
* Send an ajax request to the Genius API
|
||||
*
|
||||
* @param dispatch obj
|
||||
* @param getState obj
|
||||
* @param endpoint = the action
|
||||
* @param params = string for any URL prarams to pass on
|
||||
* @param endpoint string = the url to query (ie /song/:id)
|
||||
* @param method string
|
||||
* @param data mixed = request payload
|
||||
* @return Promise
|
||||
**/
|
||||
const sendRequest = (dispatch, getState, endpoint) => {
|
||||
const sendRequest = (dispatch, getState, endpoint, method = 'GET', data = false) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getToken(dispatch, getState )
|
||||
.then(
|
||||
response => {
|
||||
|
||||
var loader_key = helpers.generateGuid();
|
||||
dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint));
|
||||
// create our ajax request config
|
||||
var config = {
|
||||
method: method,
|
||||
url: 'https://api.genius.com/'+endpoint,
|
||||
cached: true,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
Authorization: 'Bearer '+ response,
|
||||
Accept: 'application/json'
|
||||
}
|
||||
}
|
||||
|
||||
var config = {
|
||||
method: 'GET',
|
||||
url: getState().genius.provider_url+endpoint,
|
||||
timeout: 10000
|
||||
};
|
||||
// only if we've got data do we add it to the request (this prevents appending of "&false" to the URL)
|
||||
if (data){
|
||||
if (typeof(data) === 'string'){
|
||||
config.data = data
|
||||
} else {
|
||||
config.data = JSON.stringify(data)
|
||||
}
|
||||
}
|
||||
|
||||
$.ajax(config).then(
|
||||
response => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
resolve(response);
|
||||
},
|
||||
(xhr, status, error) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
reject({
|
||||
config: config,
|
||||
xhr: xhr,
|
||||
status: status,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
)
|
||||
})
|
||||
// add reference to loader queue
|
||||
var loader_key = helpers.generateGuid()
|
||||
dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint))
|
||||
|
||||
$.ajax(config).then(
|
||||
response => {
|
||||
dispatch(uiActions.stopLoading(loader_key))
|
||||
resolve(response)
|
||||
},
|
||||
(xhr, status, error) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
|
||||
reject({
|
||||
config: config,
|
||||
xhr: xhr,
|
||||
status: status,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
)
|
||||
},
|
||||
error => {
|
||||
reject(error)
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle authorization process
|
||||
**/
|
||||
|
||||
export function authorizationGranted(data){
|
||||
return {
|
||||
type: 'GENIUS_AUTHORIZATION_GRANTED',
|
||||
data: data
|
||||
}
|
||||
}
|
||||
|
||||
export function revokeAuthorization(){
|
||||
return {
|
||||
type: 'GENIUS_AUTHORIZATION_REVOKED'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get current user
|
||||
**/
|
||||
export function getMe(){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'me')
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
type: 'GENIUS_ME_LOADED',
|
||||
data: response
|
||||
});
|
||||
},
|
||||
error => {
|
||||
dispatch(coreActions.handleException(
|
||||
'Could not load your profile',
|
||||
error
|
||||
));
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,7 +140,7 @@ export function getTrackLyrics(uri, path){
|
||||
if (lyrics.length > 0){
|
||||
|
||||
lyrics = lyrics.first();
|
||||
lyrics.find('a').replaceWith(function(){
|
||||
lyrics.find('a').replaceWith(function(){
|
||||
return this.innerHTML;
|
||||
});
|
||||
|
||||
|
||||
@ -937,10 +937,7 @@ const MopidyMiddleware = (function(){
|
||||
var albums_uris = helpers.arrayOf('uri',albums)
|
||||
albums_uris = helpers.removeDuplicates(albums_uris)
|
||||
|
||||
store.dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
albums: albums
|
||||
})
|
||||
store.dispatch(coreActions.albumsLoaded(albums));
|
||||
|
||||
// and plug in their URIs
|
||||
store.dispatch({
|
||||
@ -1197,10 +1194,7 @@ const MopidyMiddleware = (function(){
|
||||
var albums_uris = helpers.arrayOf('uri',albums)
|
||||
albums_uris = helpers.removeDuplicates(albums_uris)
|
||||
|
||||
store.dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
albums: albums
|
||||
})
|
||||
store.dispatch(coreActions.albumsLoaded(albums));
|
||||
|
||||
// and plug in their URIs
|
||||
store.dispatch({
|
||||
@ -1396,11 +1390,7 @@ const MopidyMiddleware = (function(){
|
||||
}
|
||||
)
|
||||
|
||||
store.dispatch({
|
||||
type: 'PLAYLIST_LOADED',
|
||||
key: playlist.uri,
|
||||
playlist: playlist
|
||||
});
|
||||
store.dispatch(coreActions.playlistLoaded(playlist));
|
||||
})
|
||||
}
|
||||
})
|
||||
@ -1431,11 +1421,7 @@ const MopidyMiddleware = (function(){
|
||||
});
|
||||
}
|
||||
|
||||
store.dispatch({
|
||||
type: 'PLAYLIST_LOADED',
|
||||
key: playlist.uri,
|
||||
playlist: playlist
|
||||
})
|
||||
store.dispatch(coreActions.playlistLoaded(playlist));
|
||||
})
|
||||
break;
|
||||
|
||||
@ -1470,7 +1456,7 @@ const MopidyMiddleware = (function(){
|
||||
type: 'PLAYLIST_TRACKS',
|
||||
tracks: tracks,
|
||||
key: action.key
|
||||
})
|
||||
});
|
||||
})
|
||||
break
|
||||
|
||||
@ -1538,11 +1524,7 @@ const MopidyMiddleware = (function(){
|
||||
request(socket, store, 'playlists.save', { playlist: playlist })
|
||||
.then(response => {
|
||||
|
||||
store.dispatch({
|
||||
type: 'PLAYLIST_LOADED',
|
||||
key: action.key,
|
||||
playlist: playlist
|
||||
});
|
||||
store.dispatch(coreActions.playlistLoaded(playlist));
|
||||
|
||||
// When we rename a playlist, the URI also changes to reflect the name change
|
||||
// We need to update our index, as well as redirect our current page URL
|
||||
@ -1607,11 +1589,7 @@ const MopidyMiddleware = (function(){
|
||||
.then(response => {
|
||||
store.dispatch(uiActions.createNotification({type: 'info', content: 'Created playlist'}))
|
||||
|
||||
store.dispatch({
|
||||
type: 'PLAYLIST_LOADED',
|
||||
key: action.uri,
|
||||
playlist: response
|
||||
})
|
||||
store.dispatch(coreActions.playlistLoaded(response));
|
||||
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_LIBRARY_PLAYLIST_CREATED',
|
||||
@ -1725,10 +1703,7 @@ const MopidyMiddleware = (function(){
|
||||
}
|
||||
}
|
||||
|
||||
store.dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
albums: albums
|
||||
})
|
||||
store.dispatch(coreActions.albumsLoaded(albums));
|
||||
|
||||
// Re-run any consequential processes in 100ms. This allows a small window for other
|
||||
// server requests before our next batch. It's a little crude but it means the server isn't
|
||||
|
||||
Reference in New Issue
Block a user