Begin using async/await.

This commit is contained in:
kialam
2018-11-12 13:24:17 -05:00
parent 06470a0e65
commit f520be71d6
5 changed files with 28 additions and 44 deletions

View File

@@ -1,12 +1,6 @@
import axios from 'axios';
const API_ROOT = '/api/';
const API_LOGIN = `${API_ROOT}login/`;
const API_LOGOUT = `${API_ROOT}logout/`;
const API_V2 = `${API_ROOT}v2/`;
const API_CONFIG = `${API_V2}config/`;
const API_PROJECTS = `${API_V2}projects/`;
const API_ORGANIZATIONS = `${API_V2}organizations/`;
import * as constant from './endpoints';
const CSRF_COOKIE_NAME = 'csrftoken';
const CSRF_HEADER_NAME = 'X-CSRFToken';
@@ -38,7 +32,7 @@ class APIClient {
return authenticated;
}
login (username, password, redirect = API_CONFIG) {
async login (username, password, redirect = constant.API_CONFIG) {
const un = encodeURIComponent(username);
const pw = encodeURIComponent(password);
const next = encodeURIComponent(redirect);
@@ -46,29 +40,15 @@ class APIClient {
const data = `username=${un}&password=${pw}&next=${next}`;
const headers = { 'Content-Type': LOGIN_CONTENT_TYPE };
return this.http.get(API_LOGIN, { headers })
.then(() => this.http.post(API_LOGIN, data, { headers }));
try {
await this.http.get(constant.API_LOGIN, { headers });
await this.http.post(constant.API_LOGIN, data, { headers });
} catch (err) {
alert(`There was a problem logging in: ${err}`);
}
}
logout () {
return this.http.get(API_LOGOUT);
}
getConfig () {
return this.http.get(API_CONFIG);
}
getProjects () {
return this.http.get(API_PROJECTS);
}
getOrganizations () {
return this.http.get(API_ORGANIZATIONS);
}
getRoot () {
return this.http.get(API_ROOT);
}
BaseGet = (endpoint) => this.http.get(endpoint);
}
export default new APIClient();