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:
Jonathan Druart 2023-02-15 10:43:46 +01:00 committed by Tomas Cohen Arazi
parent 929f5a2199
commit 2c34c60951
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F

View file

@ -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;