var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Buffer } from "buffer";
import * as logger from "../logger";
import { decodeValue } from "../gtx/serialization";
import { Method } from "./enums";
function handleRequest(method, path, endpoint, postObject) {
    return __awaiter(this, void 0, void 0, function* () {
        if (method == Method.GET) {
            return yield get(path, endpoint);
        }
        else {
            return yield post(path, endpoint, postObject);
        }
    });
}
export default handleRequest;
/**
 * Sends request to get data from a given API endpoint.
 * @param path API endpoint of Rell backend
 * @param endpoint
 */
function get(path, endpoint) {
    return __awaiter(this, void 0, void 0, function* () {
        logger.debug("GET URL " + new URL(path, endpoint).href);
        try {
            const response = yield fetch(new URL(path, endpoint).href);
            const rspBody = yield response.json();
            if (response.status == 404) {
                return { error: null, statusCode: response.status, rspBody };
            }
            return { error: null, statusCode: response.status, rspBody };
        }
        catch (error) {
            logger.error(error.message);
            return { error, statusCode: null, rspBody: null };
        }
    });
}
/**
 * Sends request to post data to a given API endpoint.
 * @param path API endpoint of Rell backend
 * @param endpoint
 * @param requestBody request body
 */
function post(path, endpoint, requestBody) {
    return __awaiter(this, void 0, void 0, function* () {
        logger.debug("POST URL " + new URL(path, endpoint).href);
        logger.debug("POST body " + JSON.stringify(requestBody));
        if (Buffer.isBuffer(requestBody)) {
            try {
                const requestOptions = {
                    method: "post",
                    body: requestBody,
                    headers: {
                        Accept: "application/octet-stream",
                        "Content-Type": "application/octet-stream",
                    },
                };
                const response = yield fetch(new URL(path, endpoint).href, requestOptions);
                return {
                    error: null,
                    statusCode: response.status,
                    rspBody: decodeValue(Buffer.from(yield response.arrayBuffer())),
                };
            }
            catch (error) {
                return { error, statusCode: null, rspBody: null };
            }
        }
        else {
            try {
                const response = yield fetch(new URL(path, endpoint).href, {
                    method: "post",
                    body: JSON.stringify(requestBody),
                });
                return {
                    error: null,
                    statusCode: response.status,
                    rspBody: yield response.json(),
                };
            }
            catch (error) {
                return { error, statusCode: null, rspBody: null };
            }
        }
    });
}
//# sourceMappingURL=httpUtil.js.map