Migrating from jquery.ajax to request
This commit is contained in:
@ -19,7 +19,19 @@ const sendRequest = (dispatch, getState, endpoint, params) => new Promise((resol
|
||||
const loader_key = helpers.generateGuid();
|
||||
dispatch(uiActions.startLoading(loader_key, `discogs_${endpoint}`));
|
||||
|
||||
const checkRateLimit = (request) => {
|
||||
const config = {
|
||||
method: 'GET',
|
||||
timeout: 30000,
|
||||
mode: 'cors',
|
||||
headers: {
|
||||
'User-Agent': 'Iris/1.0',
|
||||
'Authorization': `Discogs key=${key}, secret=${secret}`
|
||||
},
|
||||
};
|
||||
|
||||
function status(response) {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
|
||||
const rate = {
|
||||
limit: request.getResponseHeader('X-Discogs-Ratelimit'),
|
||||
remaining: request.getResponseHeader('X-Discogs-Ratelimit-Remaining'),
|
||||
@ -33,38 +45,23 @@ const sendRequest = (dispatch, getState, endpoint, params) => new Promise((resol
|
||||
description: `Discogs rate limit exceeded, try again in a few minutes.`
|
||||
}));
|
||||
}
|
||||
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
return Promise.resolve(response)
|
||||
} else {
|
||||
return Promise.reject(new Error(response.statusText))
|
||||
}
|
||||
}
|
||||
|
||||
const config = {
|
||||
method: 'GET',
|
||||
cache: true,
|
||||
timeout: 30000,
|
||||
url: url,
|
||||
crossDomain: true,
|
||||
headers: {
|
||||
'User-Agent': 'Iris/1.0',
|
||||
'Authorization': `Discogs key=${key}, secret=${secret}`
|
||||
},
|
||||
};
|
||||
|
||||
$.ajax(config).then(
|
||||
(data, textStatus, request) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
checkRateLimit(request);
|
||||
fetch(url, config)
|
||||
.then(status)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
resolve(data);
|
||||
},
|
||||
(xhr, status, error) => {
|
||||
dispatch(uiActions.stopLoading(loader_key));
|
||||
checkRateLimit(xhr);
|
||||
|
||||
reject({
|
||||
config,
|
||||
error,
|
||||
status,
|
||||
xhr,
|
||||
});
|
||||
},
|
||||
);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
export function getArtistImages(uri, artist) {
|
||||
|
||||
Reference in New Issue
Block a user