react-logo/frontend/src/services/api-client.service.js

54 lines
935 B
JavaScript
Raw Normal View History

2020-02-25 10:14:21 +01:00
export class APIClient {
constructor(baseURL = 'http://localhost:8001/api/v1') {
this.baseURL = baseURL;
this.login = this.login.bind(this);
this.logout = this.logout.bind(this);
this.me = this.me.bind(this);
this.listUsers = this.listUsers.bind(this);
this.listRequests = this.listRequests.bind(this);
}
login(username, password) {
return this._callAPI('/login', { username, password }, 'POST')
}
logout() {
return this._callAPI('/logout')
}
me() {
return this._callAPI('/me')
}
listUsers() {
return this._callAPI('/users')
}
listRequests() {
}
createRequest(request) {
}
updateRequestStatus(reqID, newStatus) {
}
_callAPI(path, body, method='GET') {
2020-02-25 10:15:59 +01:00
return fetch(this.baseURL + path, {
2020-02-25 10:14:21 +01:00
method,
headers: {
'Content-Type': 'application/json',
},
mode: 'cors',
2020-02-25 10:15:59 +01:00
credentials: 'include',
2020-02-25 10:14:21 +01:00
body: JSON.stringify(body),
})
.then(res => res.json())
}
}