Migrating from jquery.ajax to request

This commit is contained in:
James Barnsley
2019-09-10 07:40:26 +12:00
parent c08cc71c47
commit 26b38cd0f9
6 changed files with 135 additions and 1908 deletions

View File

@ -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) {