import CodeSnippet from '../CodeSnippet.js';
import 'prismjs/components/prism-python.min.js';
export default class RequestsSnippet extends CodeSnippet {
    lang = 'python';
    computeCommand(url, method, headers, payload) {
        if (!url || !method) {
            return '';
        }
        let result = 'import requests\n\n';
        const hasHeaders = !!headers;
        const hasPayload = !!payload;
        result += `url = '${url}'\n`;
        if (hasHeaders) {
            result += this._getHeaders(headers);
            if (!hasPayload) {
                result += '\n';
            }
        }
        if (hasPayload) {
            result += this._getPayload(payload);
        }
        result += this._getConnection(method, hasPayload, hasHeaders);
        result += this._getFooter();
        return result;
    }
    _getHeaders(headers) {
        let result = 'headers = {';
        const parts = [];
        for (const [name, value] of headers) {
            parts.push(`'${name}': '${value}'`);
        }
        result += parts.join(',');
        result += '}\n';
        return result;
    }
    _getPayload(payload) {
        let result = 'body = """';
        result += payload;
        result += '"""\n\n';
        return result;
    }
    /**
     * Computes value of connection definition
     * @param url HTTP request url
     * @param method HTTP request method
     * @param hasPayload True if the request contains payload message
     * @param hasHeaders True if the request contains headers
     */
    _getConnection(method, hasPayload, hasHeaders) {
        const lowerMethod = String(method).toLowerCase();
        let result = `req = requests.${lowerMethod}(url`;
        if (hasHeaders) {
            result += ', headers=headers';
        }
        if (hasPayload) {
            result += ', data=body';
        }
        result += ')\n\n';
        return result;
    }
    /**
     * @return Returns ending of the code definition
     */
    _getFooter() {
        let result = 'print(req.status_code)\n';
        result += 'print(req.headers)\n';
        result += 'print(req.text)';
        return result;
    }
}
//# sourceMappingURL=RequestsSnippet.js.map