2020-03-13 15:44:24 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import httpStatusCodes from './http_status';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Poll from './poll';
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Polls an endpoint until it returns either a 200 OK or a error status.
|
|
|
|
* The Poll-Interval header in the responses are used to determine how
|
|
|
|
* frequently to poll.
|
|
|
|
*
|
|
|
|
* Once a 200 OK is received, the promise resolves with that response. If an
|
|
|
|
* error status is received, the promise rejects with the error.
|
|
|
|
*
|
|
|
|
* @param {string} url - The URL to poll.
|
|
|
|
* @param {Object} [config] - The config to provide to axios.get().
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
export default (url, config = {}) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const eTagPoll = new Poll({
|
|
|
|
resource: {
|
|
|
|
axiosGet(data) {
|
|
|
|
return axios.get(data.url, {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
...data.config,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: { url, config },
|
|
|
|
method: 'axiosGet',
|
2021-03-08 18:12:59 +05:30
|
|
|
successCallback: (response) => {
|
2020-03-13 15:44:24 +05:30
|
|
|
if (response.status === httpStatusCodes.OK) {
|
|
|
|
resolve(response);
|
|
|
|
eTagPoll.stop();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
errorCallback: reject,
|
|
|
|
});
|
|
|
|
|
|
|
|
eTagPoll.makeRequest();
|
|
|
|
});
|