Bug 32939: Introduce count
Not used yet, will replace myFetchTotal Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com> Signed-off-by: Nick Clemens <nick@bywatersolutions.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
929f5a2199
commit
2c34c60951
1 changed files with 22 additions and 4 deletions
|
@ -8,13 +8,18 @@ class HttpClient {
|
|||
};
|
||||
}
|
||||
|
||||
async _fetchJSON(endpoint, headers = {}, options = {}) {
|
||||
async _fetchJSON(
|
||||
endpoint,
|
||||
headers = {},
|
||||
options = {},
|
||||
return_response = false
|
||||
) {
|
||||
let res;
|
||||
await fetch(this._baseURL + endpoint, {
|
||||
...options,
|
||||
headers: { ...this._headers, ...headers },
|
||||
})
|
||||
.then(this.checkError)
|
||||
.then((response) => this.checkError(response, return_response))
|
||||
.then(
|
||||
(result) => {
|
||||
res = result;
|
||||
|
@ -60,6 +65,21 @@ class HttpClient {
|
|||
});
|
||||
}
|
||||
|
||||
count(params = {}) {
|
||||
let res;
|
||||
this._fetchJSON(params.endpoint, params.headers, 1).then(
|
||||
(response) => {
|
||||
if (response) {
|
||||
res = response.headers.get("X-Total-Count");
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
setError(error.toString());
|
||||
}
|
||||
);
|
||||
return res;
|
||||
}
|
||||
|
||||
checkError(response, return_response = 0) {
|
||||
if (response.status >= 200 && response.status <= 299) {
|
||||
return return_response ? response : response.json();
|
||||
|
@ -69,8 +89,6 @@ class HttpClient {
|
|||
throw Error("%s (%s)".format(response.statusText, response.status));
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Implement count method
|
||||
}
|
||||
|
||||
export default HttpClient;
|
||||
|
|
Loading…
Reference in a new issue