2020-03-22 11:21:55 +01:00
|
|
|
// @flow
|
2019-07-26 14:14:01 +02:00
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Class used to get json data from the web
|
|
|
|
*/
|
2019-07-26 14:14:01 +02:00
|
|
|
export default class WebDataManager {
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
FETCH_URL: string;
|
|
|
|
lastDataFetched: Object = {};
|
2019-07-26 14:14:01 +02:00
|
|
|
|
|
|
|
|
2020-03-22 11:21:55 +01:00
|
|
|
constructor(url: string) {
|
2019-07-26 14:14:01 +02:00
|
|
|
this.FETCH_URL = url;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Read data from FETCH_URL and return it.
|
|
|
|
* If no data was found, returns an empty object
|
|
|
|
*
|
|
|
|
* @return {Promise<Object>}
|
|
|
|
*/
|
2019-07-26 14:14:01 +02:00
|
|
|
async readData() {
|
2019-08-06 13:28:11 +02:00
|
|
|
let fetchedData: Object = {};
|
2019-07-26 14:14:01 +02:00
|
|
|
try {
|
|
|
|
let response = await fetch(this.FETCH_URL);
|
|
|
|
fetchedData = await response.json();
|
|
|
|
} catch (error) {
|
2019-08-08 18:40:42 +02:00
|
|
|
throw new Error('Could not read FetchedData from server');
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
this.lastDataFetched = fetchedData;
|
|
|
|
return fetchedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|