-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchData.js
57 lines (44 loc) · 1.19 KB
/
fetchData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use strict";
export class FetchData {
constructor(data_in) {
this.fetch_data_obj = {
'dataset': 'GHCND',
'data': undefined,
'startdate': data_in.min_date,
'enddate': data_in.min_date,
'stations': data_in.station_ids,
'token': undefined,
'url': 'https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=',
};
this.addParametersToUrl();
this.fetchData();
}
addParametersToUrl() {
let new_url = this.fetch_data_obj.url+this.fetch_data_obj.dataset+
'&startdate='+this.fetch_data_obj.startdate+'&enddate='+
this.fetch_data_obj.enddate
this.fetch_data_obj.stations.forEach(function(o) {
new_url = new_url + '&stationid='+o
});
console.log(new_url);
this.fetch_data_obj.url = new_url;
}
fetchData(){
let options = {
method: 'GET',
headers: {
'token': this.token,
},
}
this.fetch_data_obj.data = fetch(this.fetch_data_obj.url, options)
.then(function(response){
return response.text();
})
.then(function(rtext) {
return JSON.parse(rtext);
})
.catch(function(error) {
console.log(error.message);
});
}
};