import {Logger} from "../logging/logger.js";

export class Requester {
    static async doGetRequest(url){
        return fetch(url)
            .then(async response => {
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status} ${response.statusText} : ${await response.text()}`);
                }
                return response.json();
            })
            .then(data => {
                return data;
            })
            .catch(async error => {
                await Logger.error(`Failed to fetch`, error);
                throw error;
            });
    }

    static async doPostRequest(url, headers, body){
        return await fetch(url,{
            method: 'POST',
            headers: headers,
            body: body
        })
            .then(async response => {
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status} ${response.statusText} : ${await response.text()}`);
                }
                return response.json()
            })
            .then(data => {
                return data;
            })
            .catch(error => {
                Logger.error(`Unable to fetch`, error);
                throw error;
            });
    }
}