initial commit
This commit is contained in:
46
server/BratislavaOpendata.js
Normal file
46
server/BratislavaOpendata.js
Normal file
@@ -0,0 +1,46 @@
|
||||
export const OPENDATA_URL = 'http://opendata.bratislava.sk/api/mhd';
|
||||
|
||||
export default class BratislavaOpendata {
|
||||
|
||||
constructor(apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
async fetchOneStop(stationId) {
|
||||
return this.request('/stationstop/' + stationId);
|
||||
}
|
||||
|
||||
async fetchAllStops() {
|
||||
return this.request('/stationstop');
|
||||
}
|
||||
|
||||
async fetchAllVehicles() {
|
||||
return this.request('/vehicle');
|
||||
}
|
||||
|
||||
async request(uri) {
|
||||
let url = OPENDATA_URL + '/' + uri;
|
||||
let resp = await fetch(url, {
|
||||
cache: 'no-cache',
|
||||
headers: {
|
||||
'User-Agent': 'node_mhdmap/1.0',
|
||||
'Key': this.apiKey,
|
||||
},
|
||||
});
|
||||
|
||||
if (!resp.ok) {
|
||||
console.warn('Error while fetching', url, 'status:', resp.status, resp.statusText);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
resp = await resp.json();
|
||||
return resp;
|
||||
} catch (e) {
|
||||
console.warn('Incorrect response for fetching', url, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user