js api refactoring
This commit is contained in:
parent
511a40fb5a
commit
9d8fbc808f
3 changed files with 20 additions and 33 deletions
|
@ -1436,5 +1436,5 @@ LevelControl = L.Control.extend({
|
||||||
|
|
||||||
|
|
||||||
if ($('#sidebar').length) {
|
if ($('#sidebar').length) {
|
||||||
c3nav_api.authenticated().then(() => editor.init());
|
editor.init();
|
||||||
}
|
}
|
||||||
|
|
|
@ -260,9 +260,6 @@ c3nav = {
|
||||||
|
|
||||||
//c3nav.test_location();
|
//c3nav.test_location();
|
||||||
},
|
},
|
||||||
get_csrf_token: function() {
|
|
||||||
return document.cookie.match(new RegExp('c3nav_csrftoken=([^;]+)'))[1];
|
|
||||||
},
|
|
||||||
test_location: function() {
|
test_location: function() {
|
||||||
c3nav_api.get('positioning/locate-test')
|
c3nav_api.get('positioning/locate-test')
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
@ -1967,7 +1964,7 @@ c3nav = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
$(document).ready(() => {
|
$(document).ready(() => {
|
||||||
c3nav_api.authenticated().then(c3nav.init);
|
c3nav.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
function nearby_stations_available() {
|
function nearby_stations_available() {
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate() {
|
authenticate() {
|
||||||
return fetch(this.base+'auth/session/', {
|
this.auth_promise = fetch(this.base+'auth/session/', {
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
})
|
})
|
||||||
|
@ -21,6 +21,7 @@
|
||||||
throw err;
|
throw err;
|
||||||
})
|
})
|
||||||
.then(_ => null);
|
.then(_ => null);
|
||||||
|
return this.auth_promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticated() {
|
authenticated() {
|
||||||
|
@ -35,44 +36,33 @@
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(path) {
|
async req(method, path, body) {
|
||||||
return fetch(this.make_url(path), {
|
await this.auth_promise;
|
||||||
|
const init = {
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
method: 'GET',
|
method: method,
|
||||||
headers: {
|
headers: {
|
||||||
'X-API-Key': this.key,
|
'X-API-Key': this.key,
|
||||||
'Accept': 'application/json'
|
'Accept': 'application/json'
|
||||||
}
|
}
|
||||||
})
|
};
|
||||||
.then(res => res.json())
|
if (typeof body !== 'undefined') {
|
||||||
|
init.body = JSON.stringify(body);
|
||||||
|
}
|
||||||
|
const res = await fetch(this.make_url(path), init);
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
get(path) {
|
||||||
|
return this.req('GET', path);
|
||||||
}
|
}
|
||||||
|
|
||||||
post(path, data) {
|
post(path, data) {
|
||||||
return fetch(this.make_url(path), {
|
return this.req('POST', path, data);
|
||||||
credentials: 'include',
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'X-API-Key': this.key,
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
})
|
|
||||||
.then(res => res.json())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
put(path, data) {
|
put(path, data) {
|
||||||
return fetch(this.make_url(path), {
|
return this.req('PUT', path, data);
|
||||||
credentials: 'include',
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
'X-API-Key': this.key,
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
})
|
|
||||||
.then(res => res.json())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue