"use strict";
/**
 *  HTTP Client wrapper
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
var node_fetch_1 = require("node-fetch");
/**
 * Wrapper class to make HTTP requests
 */
var HttpClient = /** @class */ (function () {
    function HttpClient() {
    }
    /**
     * Make a POST request
     *
     * @param url     - endpoint
     * @param payload - POST data
     * @param options - HTTP options
     *
     * @return the response promise
     */
    HttpClient.prototype.post = function (url, payload, options) {
        if (payload === void 0) { payload = {}; }
        if (options === void 0) { options = {}; }
        this.validateUrl(url);
        var default_options = {
            method: 'POST',
            body: JSON.stringify(payload),
            headers: { 'Content-Type': 'application/json' },
        };
        return node_fetch_1.default(url, __assign(__assign({}, default_options), options));
    };
    /**
     * Check if a URL is valid
     *
     * @param url - the URL to test
     *
     * @return whether or not the URL is valid
     */
    HttpClient.prototype.isValidUrl = function (url) {
        try {
            new URL(url);
            return true;
        }
        catch (_) {
            return false;
        }
    };
    /**
     * Validate a URL
     *
     * @param url - URL to test
     *
     * @throws if the URL is invalid
     */
    HttpClient.prototype.validateUrl = function (url) {
        if (!this.isValidUrl(url)) {
            throw new Error("The provided URL is invalid: " + url);
        }
    };
    return HttpClient;
}());
exports.HttpClient = HttpClient;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiSHR0cENsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIkh0dHBDbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBbUJHOzs7Ozs7Ozs7Ozs7OztBQUVILHlDQUEyQztBQUUzQzs7R0FFRztBQUNIO0lBQUE7SUFzREEsQ0FBQztJQXJEQzs7Ozs7Ozs7T0FRRztJQUNJLHlCQUFJLEdBQVgsVUFDRSxHQUFXLEVBQ1gsT0FBMEIsRUFDMUIsT0FBMEI7UUFEMUIsd0JBQUEsRUFBQSxZQUEwQjtRQUMxQix3QkFBQSxFQUFBLFlBQTBCO1FBRTFCLElBQUksQ0FBQyxXQUFXLENBQUMsR0FBRyxDQUFDLENBQUM7UUFFdEIsSUFBTSxlQUFlLEdBQUc7WUFDdEIsTUFBTSxFQUFFLE1BQU07WUFDZCxJQUFJLEVBQUUsSUFBSSxDQUFDLFNBQVMsQ0FBQyxPQUFPLENBQUM7WUFDN0IsT0FBTyxFQUFFLEVBQUMsY0FBYyxFQUFFLGtCQUFrQixFQUFDO1NBQzlDLENBQUM7UUFFRixPQUFPLG9CQUFLLENBQUMsR0FBRyx3QkFBTSxlQUFlLEdBQUssT0FBTyxFQUFFLENBQUM7SUFDdEQsQ0FBQztJQUVEOzs7Ozs7T0FNRztJQUNJLCtCQUFVLEdBQWpCLFVBQWtCLEdBQVc7UUFDM0IsSUFBSTtZQUNGLElBQUksR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFDO1lBQ2IsT0FBTyxJQUFJLENBQUM7U0FDYjtRQUFDLE9BQU8sQ0FBQyxFQUFFO1lBQ1YsT0FBTyxLQUFLLENBQUM7U0FDZDtJQUNILENBQUM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