Refresh token into loader queue
This commit is contained in:
@ -15,7 +15,7 @@ var helpers = require('../../helpers')
|
||||
* @param data mixed = request payload
|
||||
* @return Promise
|
||||
**/
|
||||
const sendRequest = (dispatch, getState, endpoint, method = 'GET', data = false) => {
|
||||
const request = (dispatch, getState, endpoint, method = 'GET', data = false) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getToken(dispatch, getState )
|
||||
@ -51,7 +51,7 @@ const sendRequest = (dispatch, getState, endpoint, method = 'GET', data = false)
|
||||
|
||||
// add reference to loader queue
|
||||
var loader_key = helpers.generateGuid()
|
||||
dispatch(uiActions.startLoading(loader_key, 'spotify_'+endpoint))
|
||||
dispatch(uiActions.startLoading(loader_key, 'spotify_'+endpoint));
|
||||
|
||||
$.ajax(config).then(
|
||||
response => {
|
||||
@ -120,6 +120,11 @@ function getToken(dispatch, getState){
|
||||
function refreshToken(dispatch, getState){
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
// add reference to loader queue
|
||||
var loader_key = helpers.generateGuid();
|
||||
dispatch(uiActions.startLoading(loader_key, 'spotify_refresh_token'));
|
||||
|
||||
// Fully-authorized, so we can use the local Spotify credentials
|
||||
if (getState().spotify.authorization){
|
||||
|
||||
var config = {
|
||||
@ -132,6 +137,8 @@ function refreshToken(dispatch, getState){
|
||||
$.ajax(config)
|
||||
.then(
|
||||
response => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
|
||||
response.token_expiry = new Date().getTime() + (response.expires_in * 1000 )
|
||||
response.source = 'spotify'
|
||||
dispatch({
|
||||
@ -141,6 +148,8 @@ function refreshToken(dispatch, getState){
|
||||
resolve(response)
|
||||
},
|
||||
(xhr, status, error) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
|
||||
reject({
|
||||
config: config,
|
||||
xhr: xhr,
|
||||
@ -149,7 +158,9 @@ function refreshToken(dispatch, getState){
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
// Server-side authorized (with limited scope) so we need to refresh
|
||||
// using the Mopidy-Spotify credentials
|
||||
} else {
|
||||
|
||||
var config = {
|
||||
@ -162,6 +173,8 @@ function refreshToken(dispatch, getState){
|
||||
$.ajax(config)
|
||||
.then(
|
||||
(response, status, xhr) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
|
||||
if (response.error){
|
||||
reject({
|
||||
config: config,
|
||||
@ -184,6 +197,8 @@ function refreshToken(dispatch, getState){
|
||||
|
||||
},
|
||||
(xhr, status, error) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
|
||||
reject({
|
||||
config: config,
|
||||
xhr: xhr,
|
||||
@ -252,7 +267,7 @@ export function importAuthorization(authorization){
|
||||
**/
|
||||
export function getMe(){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'me' )
|
||||
request(dispatch, getState, 'me' )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -278,7 +293,7 @@ export function getMe(){
|
||||
**/
|
||||
export function getTrack(uri){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'tracks/'+ helpers.getFromUri('trackid', uri))
|
||||
request(dispatch, getState, 'tracks/'+ helpers.getFromUri('trackid', uri))
|
||||
.then(
|
||||
response => {
|
||||
dispatch(coreActions.trackLoaded(response));
|
||||
@ -295,7 +310,7 @@ export function getTrack(uri){
|
||||
|
||||
export function getLibraryTracks(){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'me/tracks?limit=50')
|
||||
request(dispatch, getState, 'me/tracks?limit=50')
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -334,7 +349,7 @@ export function getFeaturedPlaylists(){
|
||||
|
||||
var timestamp = year+'-'+month+'-'+day+'T'+hour+':'+min+':'+sec;
|
||||
|
||||
sendRequest(dispatch, getState, 'browse/featured-playlists?limit=50&country='+getState().spotify.country+'&locale='+getState().spotify.locale+'timestamp='+timestamp)
|
||||
request(dispatch, getState, 'browse/featured-playlists?limit=50&country='+getState().spotify.country+'&locale='+getState().spotify.locale+'timestamp='+timestamp)
|
||||
.then(
|
||||
response => {
|
||||
var playlists = []
|
||||
@ -379,7 +394,7 @@ export function getFeaturedPlaylists(){
|
||||
|
||||
export function getCategories(){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'browse/categories?limit=50&country='+getState().spotify.country+'&locale='+getState().spotify.locale )
|
||||
request(dispatch, getState, 'browse/categories?limit=50&country='+getState().spotify.country+'&locale='+getState().spotify.locale )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -399,7 +414,7 @@ export function getCategories(){
|
||||
|
||||
export function getCategory(id){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'browse/categories/'+id+'?country='+getState().spotify.country+'&locale='+getState().spotify.locale )
|
||||
request(dispatch, getState, 'browse/categories/'+id+'?country='+getState().spotify.country+'&locale='+getState().spotify.locale )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -425,7 +440,7 @@ export function getCategory(id){
|
||||
|
||||
export function getCategoryPlaylists(id){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'browse/categories/'+id+'/playlists?limit=50&country='+getState().spotify.country+'&locale='+getState().spotify.locale )
|
||||
request(dispatch, getState, 'browse/categories/'+id+'/playlists?limit=50&country='+getState().spotify.country+'&locale='+getState().spotify.locale )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -446,7 +461,7 @@ export function getCategoryPlaylists(id){
|
||||
|
||||
export function getNewReleases(){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'browse/new-releases?country='+getState().spotify.country+'&limit=50' )
|
||||
request(dispatch, getState, 'browse/new-releases?country='+getState().spotify.country+'&limit=50' )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -466,7 +481,7 @@ export function getNewReleases(){
|
||||
|
||||
export function getURL(url, action_name, key = false){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, url )
|
||||
request(dispatch, getState, url )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -487,7 +502,7 @@ export function getURL(url, action_name, key = false){
|
||||
|
||||
export function getMore(url, core_action = null, custom_action = null){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, url)
|
||||
request(dispatch, getState, url)
|
||||
.then(
|
||||
response => {
|
||||
if (core_action){
|
||||
@ -538,7 +553,7 @@ export function getSearchResults(type, query, limit = 50, offset = 0){
|
||||
url += '&limit='+limit
|
||||
url += '&offset='+offset
|
||||
|
||||
sendRequest(dispatch, getState, url )
|
||||
request(dispatch, getState, url )
|
||||
.then(
|
||||
response => {
|
||||
if (response.tracks !== undefined){
|
||||
@ -628,7 +643,7 @@ export function getAutocompleteResults(field_id, query, types = ['album','artist
|
||||
endpoint += '&type='+types.join(',')
|
||||
endpoint += '&country='+getState().spotify.country
|
||||
|
||||
sendRequest(dispatch, getState, endpoint)
|
||||
request(dispatch, getState, endpoint)
|
||||
.then(
|
||||
response => {
|
||||
var genres = []
|
||||
@ -740,7 +755,7 @@ export function following(uri, method = 'GET'){
|
||||
break;
|
||||
}
|
||||
|
||||
sendRequest(dispatch, getState, endpoint, method, data)
|
||||
request(dispatch, getState, endpoint, method, data)
|
||||
.then(
|
||||
response => {
|
||||
if (response ) is_following = response
|
||||
@ -777,7 +792,7 @@ export function resolveRadioSeeds(radio){
|
||||
artist_ids += helpers.getFromUri('artistid', radio.seed_artists[i])
|
||||
}
|
||||
|
||||
sendRequest(dispatch, getState, 'artists?ids='+ artist_ids )
|
||||
request(dispatch, getState, 'artists?ids='+ artist_ids )
|
||||
.then(
|
||||
response => {
|
||||
if (response && response.artists){
|
||||
@ -805,7 +820,7 @@ export function resolveRadioSeeds(radio){
|
||||
track_ids += helpers.getFromUri('trackid', radio.seed_tracks[i])
|
||||
}
|
||||
|
||||
sendRequest(dispatch, getState, 'tracks?ids='+ track_ids )
|
||||
request(dispatch, getState, 'tracks?ids='+ track_ids )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -842,8 +857,8 @@ export function getFavorites(limit = 50, term = 'long_term'){
|
||||
dispatch({type: 'SPOTIFY_FAVORITES_LOADED', artists: [], tracks: []})
|
||||
|
||||
$.when(
|
||||
sendRequest(dispatch, getState, 'me/top/artists?limit='+limit+'&time_range='+term),
|
||||
sendRequest(dispatch, getState, 'me/top/tracks?limit='+limit+'&time_range='+term)
|
||||
request(dispatch, getState, 'me/top/artists?limit='+limit+'&time_range='+term),
|
||||
request(dispatch, getState, 'me/top/tracks?limit='+limit+'&time_range='+term)
|
||||
|
||||
).then(
|
||||
(artists_response, tracks_response) => {
|
||||
@ -918,7 +933,7 @@ export function getRecommendations(uris = [], limit = 20, tunabilities = null){
|
||||
}
|
||||
}
|
||||
|
||||
sendRequest(dispatch, getState, endpoint)
|
||||
request(dispatch, getState, endpoint)
|
||||
.then(
|
||||
response => {
|
||||
var tracks = Object.assign([], response.tracks);
|
||||
@ -991,7 +1006,7 @@ export function getRecommendations(uris = [], limit = 20, tunabilities = null){
|
||||
**/
|
||||
export function getGenres(){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'recommendations/available-genre-seeds')
|
||||
request(dispatch, getState, 'recommendations/available-genre-seeds')
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1032,7 +1047,7 @@ export function getArtist(uri, full = false){
|
||||
|
||||
// We need our artist, obviously
|
||||
var requests = [
|
||||
sendRequest(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) )
|
||||
request(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) )
|
||||
.then(
|
||||
response => {
|
||||
Object.assign(artist, response);
|
||||
@ -1050,7 +1065,7 @@ export function getArtist(uri, full = false){
|
||||
if (full){
|
||||
|
||||
requests.push(
|
||||
sendRequest(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/top-tracks?country='+getState().spotify.country )
|
||||
request(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/top-tracks?country='+getState().spotify.country )
|
||||
.then(
|
||||
response => {
|
||||
Object.assign(artist, response);
|
||||
@ -1065,7 +1080,7 @@ export function getArtist(uri, full = false){
|
||||
);
|
||||
|
||||
requests.push(
|
||||
sendRequest(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/related-artists' )
|
||||
request(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/related-artists' )
|
||||
.then(
|
||||
response => {
|
||||
dispatch(coreActions.artistsLoaded(response.artists));
|
||||
@ -1094,7 +1109,7 @@ export function getArtist(uri, full = false){
|
||||
|
||||
// Now go get our artist albums
|
||||
if (full){
|
||||
sendRequest(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/albums?market='+getState().spotify.country )
|
||||
request(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/albums?market='+getState().spotify.country )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1125,7 +1140,7 @@ export function getArtists(uris){
|
||||
ids += helpers.getFromUri('artistid', uris[i] );
|
||||
}
|
||||
|
||||
sendRequest(dispatch, getState, 'artists/?ids='+ids )
|
||||
request(dispatch, getState, 'artists/?ids='+ids )
|
||||
.then(
|
||||
response => {
|
||||
for (var i = i; i < response.length; i++){
|
||||
@ -1163,7 +1178,7 @@ export function playArtistTopTracks(uri){
|
||||
|
||||
// We need to load the artist's top tracks first
|
||||
} else {
|
||||
sendRequest(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/top-tracks?country='+getState().spotify.country )
|
||||
request(dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/top-tracks?country='+getState().spotify.country )
|
||||
.then(
|
||||
response => {
|
||||
const uris = helpers.arrayOf('uri',response.tracks)
|
||||
@ -1189,7 +1204,7 @@ export function playArtistTopTracks(uri){
|
||||
|
||||
export function getUser(uri){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'users/'+ helpers.getFromUri('userid',uri))
|
||||
request(dispatch, getState, 'users/'+ helpers.getFromUri('userid',uri))
|
||||
.then(
|
||||
response => {
|
||||
dispatch(coreActions.userLoaded(helpers.formatUser(response)));
|
||||
@ -1208,7 +1223,7 @@ export function getUserPlaylists(uri){
|
||||
return (dispatch, getState) => {
|
||||
|
||||
// get the first page of playlists
|
||||
sendRequest(dispatch, getState, 'users/'+ helpers.getFromUri('userid', uri) +'/playlists?limit=40' )
|
||||
request(dispatch, getState, 'users/'+ helpers.getFromUri('userid', uri) +'/playlists?limit=40' )
|
||||
.then(
|
||||
response => {
|
||||
var playlists = [];
|
||||
@ -1260,7 +1275,7 @@ export function getAlbum(uri){
|
||||
return (dispatch, getState) => {
|
||||
|
||||
// get the album
|
||||
sendRequest(dispatch, getState, 'albums/'+ helpers.getFromUri('albumid', uri) )
|
||||
request(dispatch, getState, 'albums/'+ helpers.getFromUri('albumid', uri) )
|
||||
.then(
|
||||
response => {
|
||||
|
||||
@ -1299,7 +1314,7 @@ export function getAlbum(uri){
|
||||
}
|
||||
|
||||
// get all album artists as full objects
|
||||
sendRequest(dispatch, getState, 'artists/?ids='+artist_ids )
|
||||
request(dispatch, getState, 'artists/?ids='+artist_ids )
|
||||
.then(
|
||||
response => {
|
||||
dispatch(coreActions.artistsLoaded(response.artists));
|
||||
@ -1328,7 +1343,7 @@ export function toggleAlbumInLibrary(uri, method){
|
||||
if (method == 'DELETE' ) var new_state = 0
|
||||
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'me/albums?ids='+ helpers.getFromUri('albumid',uri), method )
|
||||
request(dispatch, getState, 'me/albums?ids='+ helpers.getFromUri('albumid',uri), method )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1366,7 +1381,7 @@ export function createPlaylist(name, description, is_public, is_collaborative){
|
||||
collaborative: is_collaborative
|
||||
}
|
||||
|
||||
sendRequest(dispatch, getState, 'users/'+ getState().spotify.me.id +'/playlists/', 'POST', data)
|
||||
request(dispatch, getState, 'users/'+ getState().spotify.me.id +'/playlists/', 'POST', data)
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1411,7 +1426,7 @@ export function savePlaylist(uri, name, description, is_public, is_collaborative
|
||||
}
|
||||
|
||||
// Update the playlist fields
|
||||
sendRequest(
|
||||
request(
|
||||
dispatch, getState, 'users/'+ getState().spotify.me.id +'/playlists/'+ helpers.getFromUri('playlistid',uri), 'PUT', data)
|
||||
.then(
|
||||
response => {
|
||||
@ -1419,7 +1434,7 @@ export function savePlaylist(uri, name, description, is_public, is_collaborative
|
||||
|
||||
// Save the image
|
||||
if (image){
|
||||
sendRequest(dispatch, getState, 'users/'+ getState().spotify.me.id +'/playlists/'+ helpers.getFromUri('playlistid',uri)+'/images', 'PUT', image)
|
||||
request(dispatch, getState, 'users/'+ getState().spotify.me.id +'/playlists/'+ helpers.getFromUri('playlistid',uri)+'/images', 'PUT', image)
|
||||
.then(
|
||||
|
||||
response => {
|
||||
@ -1470,7 +1485,7 @@ export function getPlaylist(uri){
|
||||
return (dispatch, getState) => {
|
||||
|
||||
// get the main playlist object
|
||||
sendRequest(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) +'?market='+getState().spotify.country )
|
||||
request(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) +'?market='+getState().spotify.country )
|
||||
.then(
|
||||
response => {
|
||||
|
||||
@ -1533,7 +1548,7 @@ export function getLibraryTracksAndPlay(uri){
|
||||
|
||||
export function getLibraryTracksAndPlayProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
request(dispatch, getState, data.next)
|
||||
.then(
|
||||
response => {
|
||||
|
||||
@ -1614,7 +1629,7 @@ export function getPlaylistTracksAndPlay(uri, shuffle){
|
||||
|
||||
export function getPlaylistTracksAndPlayProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
request(dispatch, getState, data.next)
|
||||
.then(
|
||||
response => {
|
||||
|
||||
@ -1685,7 +1700,7 @@ export function toggleFollowingPlaylist(uri, method){
|
||||
if (method == 'DELETE' ) var new_state = 0
|
||||
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/followers', method )
|
||||
request(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/followers', method )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1706,7 +1721,7 @@ export function toggleFollowingPlaylist(uri, method){
|
||||
|
||||
export function addTracksToPlaylist(uri, tracks_uris){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/tracks', 'POST', { uris: tracks_uris } )
|
||||
request(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/tracks', 'POST', { uris: tracks_uris } )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1728,7 +1743,7 @@ export function addTracksToPlaylist(uri, tracks_uris){
|
||||
|
||||
export function deleteTracksFromPlaylist(uri, snapshot_id, tracks_indexes){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/tracks', 'DELETE', { snapshot_id: snapshot_id, positions: tracks_indexes } )
|
||||
request(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/tracks', 'DELETE', { snapshot_id: snapshot_id, positions: tracks_indexes } )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1750,7 +1765,7 @@ export function deleteTracksFromPlaylist(uri, snapshot_id, tracks_indexes){
|
||||
|
||||
export function reorderPlaylistTracks(uri, range_start, range_length, insert_before, snapshot_id){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/tracks', 'PUT', { uri: uri, range_start: range_start, range_length: range_length, insert_before: insert_before, snapshot_id: snapshot_id } )
|
||||
request(dispatch, getState, 'playlists/'+ helpers.getFromUri('playlistid',uri) + '/tracks', 'PUT', { uri: uri, range_start: range_start, range_length: range_length, insert_before: insert_before, snapshot_id: snapshot_id } )
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1808,7 +1823,7 @@ export function getLibraryPlaylists(){
|
||||
|
||||
export function getLibraryPlaylistsProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
request(dispatch, getState, data.next)
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1879,7 +1894,7 @@ export function getLibraryArtists(){
|
||||
|
||||
export function getLibraryArtistsProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
request(dispatch, getState, data.next)
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
@ -1949,7 +1964,7 @@ export function getLibraryAlbums(){
|
||||
|
||||
export function getLibraryAlbumsProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
request(dispatch, getState, data.next)
|
||||
.then(
|
||||
response => {
|
||||
dispatch({
|
||||
|
||||
Reference in New Issue
Block a user