Skip to content

Commit

Permalink
Merge branch 'release/v1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaxolotl committed Oct 16, 2018
2 parents 2c99df3 + e29e8e1 commit bdb6a17
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Javier Valderrama",
"name": "advanced_wdc_step_by_step",
"description": "Spotify Web Data Connector for Tableau",
"version": "1.4.0",
"version": "1.5.0",
"dependencies": {
"jquery": "~3.1.0",
"q": "~1.5.1"
Expand Down
101 changes: 91 additions & 10 deletions src/modules/Requestor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

import ADVANCED_SCHEMA from '../schemas/advancedSchemas';
import ErrorHelper from './ErrorHelper';
import SpotifyWebApi from 'spotify-web-api-node';
import Q from 'q';
import _ from 'lodash';
import TERMS from './termsDictionary';

export const DEFAULT_TIME_RANGE = 'short_term';
export const DEFAULT_OFFSET = 0;
Expand Down Expand Up @@ -29,6 +33,83 @@ class Requestor {
this.apiLib.setAccessToken(authentication.getAccessToken());
}

/**
*
* @param {Object} $0
* @param {String} $0.name
* @param {String} $0.message
* @param {Number} $0.statusCode
* @returns {Object}
*/
statusCodeInterceptor ({ name, message, statusCode } = {}) {

let handledErrors = {
'400': {
action: null,
customMessage: TERMS.STATUS_CODES['400']
},
'401': {
/**
* we could implement a REAUTH action with this error
*/
action: 'REAUTH',
customMessage: TERMS.STATUS_CODES['401']
},
'403': {
action: null,
customMessage: TERMS.STATUS_CODES['403']
},
'404': {
action: null,
customMessage: TERMS.STATUS_CODES['404']
},
'429': {
/**
* we could implement a RETRY action with this error
* @see https://developer.spotify.com/documentation/web-api/#rate-limiting
*/
action: 'RETRY',
customMessage: TERMS.STATUS_CODES['429']
},
'500': {
action: null,
customMessage: TERMS.STATUS_CODES['500']
},
'502': {
action: null,
customMessage: TERMS.STATUS_CODES['502']
},
'503': {
action: null,
customMessage: TERMS.STATUS_CODES['503']
}
};

let defaultError = {
customMessage: `${name}: ${message} (${statusCode})`,
action: null
};

return _.get(handledErrors, statusCode, defaultError);
}

/**
*
* @param {Object} reason
* @returns {Object} Promise/A+ a Rejected Promise
*/
responseErrorCapturing (reason = {}) {

let capturedError = this.statusCodeInterceptor(reason);

if (capturedError.action) {
ErrorHelper.createError('Requestor.responseErrorCapturing ->', `We could take an action for this error ${reason.statusCode}`).log();
}

return Q.reject(capturedError);

}

/**
*
* @param {Object} $0
Expand All @@ -40,9 +121,9 @@ class Requestor {
*
* @returns {Object} Promise/A+
*/
getTopArtists ({ timeRange = DEFAULT_TIME_RANGE, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) { // eslint-disable-line no-unused-vars
getTopArtists ({ timeRange = DEFAULT_TIME_RANGE, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) {

return this.apiLib.getMyTopArtists({ time_range: timeRange, limit, offset });
return this.apiLib.getMyTopArtists({ time_range: timeRange, limit, offset }).catch(this.responseErrorCapturing.bind(this));

}

Expand All @@ -57,9 +138,9 @@ class Requestor {
*
* @returns {Object} Promise/A+
*/
getTopTracks ({ timeRange = DEFAULT_TIME_RANGE, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) { // eslint-disable-line no-unused-vars
getTopTracks ({ timeRange = DEFAULT_TIME_RANGE, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) {

return this.apiLib.getMyTopTracks({ time_range: timeRange, limit, offset });
return this.apiLib.getMyTopTracks({ time_range: timeRange, limit, offset }).catch(this.responseErrorCapturing.bind(this));

}

Expand All @@ -74,9 +155,9 @@ class Requestor {
*
* @returns {Object} Promise/A+
*/
getAlbums ({ market, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) { // eslint-disable-line no-unused-vars
getAlbums ({ market, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) {

return this.apiLib.getMySavedAlbums({ market, limit, offset });
return this.apiLib.getMySavedAlbums({ market, limit, offset }).catch(this.responseErrorCapturing.bind(this));

}

Expand All @@ -91,9 +172,9 @@ class Requestor {
*
* @returns {Object} Promise/A+
*/
getTracks ({ market, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) { // eslint-disable-line no-unused-vars
getTracks ({ market, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT } = {}) {

return this.apiLib.getMySavedTracks({ market, limit, offset });
return this.apiLib.getMySavedTracks({ market, limit, offset }).catch(this.responseErrorCapturing.bind(this));

}

Expand All @@ -111,7 +192,7 @@ class Requestor {
*/
getTracksFeatures ({ ids = [] } = {}) {

return this.apiLib.getAudioFeaturesForTracks(ids);
return this.apiLib.getAudioFeaturesForTracks(ids).catch(this.responseErrorCapturing.bind(this));

}

Expand All @@ -129,7 +210,7 @@ class Requestor {
*/
getArtists ({ ids = [] } = {}) {

return this.apiLib.getArtists(ids);
return this.apiLib.getArtists(ids).catch(this.responseErrorCapturing.bind(this));

}

Expand Down
9 changes: 5 additions & 4 deletions src/modules/SpotifyConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UI from './UI';
import TERMS from './termsDictionary';
import Requestor from './Requestor';
import Schema from './Schema';
import ErrorHelper from './ErrorHelper';

import TopArtists from './dataviews/TopArtists';
import TopTracks from './dataviews/TopTracks';
Expand Down Expand Up @@ -215,7 +216,7 @@ class SpotifyConnector extends Connector {
*/

// error for developers to be logged
TableauShim.log(`Connector.schema -> ${reason} `);
ErrorHelper.createError('Connector.schema ->', JSON.stringify(reason)).log();

// error to the user
TableauShim.abortWithError(TERMS.ERROR.DEFAULT_ERROR);
Expand Down Expand Up @@ -310,10 +311,10 @@ class SpotifyConnector extends Connector {
*/

// error for developers to be logged
TableauShim.log(`Connector.data -> ${reason} `);
ErrorHelper.createError('Connector.data ->', JSON.stringify(reason)).log();

// error to the user
TableauShim.abortWithError(TERMS.ERROR.DEFAULT_ERROR);
TableauShim.abortWithError(reason.customMessage || TERMS.ERROR.DEFAULT_ERROR);
});

}
Expand Down Expand Up @@ -373,7 +374,7 @@ class SpotifyConnector extends Connector {
*/

// error for developers to be logged
TableauShim.log(`Connector.getDataViewInstance -> ${tableId} not found on Data View Classes`);
ErrorHelper.createError('Connector.getDataViewInstance ->', `${tableId} not found on Data View Classes`).log();

// error to the user
throw new Error(TERMS.ERROR.DEFAULT_ERROR);
Expand Down
13 changes: 13 additions & 0 deletions src/modules/termsDictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const TERMS_DICTIONARY = {
'SHORT_TERM': 'Last 4 weeks aprox. of data.',
'MEDIUM_TERM': 'Last 6 months aprox. of data.',
'LONG_TERM': 'Several years of data.'
},
/**
* @see https://developer.spotify.com/documentation/web-api/#response-status-codes
*/
'STATUS_CODES': {
'400': 'Bad Request - The request could not be understood by the server due to malformed syntax. The message body will contain more information; see Response Schema https://developer.spotify.com/documentation/web-api/#response-schema.',
'401': 'Unauthorized - The request requires user authentication or, if the request included authorization credentials, authorization has been refused for those credentials.',
'403': 'Forbidden - The server understood the request, but is refusing to fulfill it.',
'404': 'Not Found - The requested resource could not be found. This error can be due to a temporary or permanent condition.',
'429': 'Too Many Requests - Rate limiting has been applied. see https://developer.spotify.com/documentation/web-api/#rate-limiting',
'500': 'Internal Server Error. You should never receive this error because our clever coders catch them all … but if you are unlucky enough to get one, please report it to us through a comment at the bottom of this page.',
'502': 'Bad Gateway - The server was acting as a gateway or proxy and received an invalid response from the upstream server.',
'503': 'Service Unavailable - The server is currently unable to handle the request due to a temporary condition which will be alleviated after some delay. You can choose to resend the request again.'
}
};

Expand Down

0 comments on commit bdb6a17

Please sign in to comment.