UNPKG

@atomist/automation-client

Version:
81 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const os = require("os"); const __1 = require("../.."); const retry_1 = require("../../util/retry"); const spawned_1 = require("../../util/spawned"); const httpClient_1 = require("./httpClient"); /** * Curl based HttpClient implementation. */ class CurlHttpClient { exchange(url, options = {}) { const optionsToUse = Object.assign({}, httpClient_1.DefaultHttpClientOptions, options); // Prepare headers const headers = _.map(optionsToUse.headers, (v, k) => `${k}: ${v}`); // Prepare the provided raw curl options const rawOptions = _.map(optionsToUse.options, (v, k) => { if (k.length === 1) { return [`-${k}`, v]; } else { return [`--${k}`, v]; } }); const request = () => { let log = ""; const passthroughLog = { write(str) { __1.logger.debug(str); log += str; }, }; return spawned_1.spawnAndWatch({ command: "curl", args: [ "-X", optionsToUse.method, url, "-s", "--write-out", "HTTPSTATUS:%{http_code}", ..._.flatten(headers.map(h => (["-H", h]))), ..._.flatten(rawOptions), ...(options.body ? ["-d", JSON.stringify(options.body)] : []), ], }, {}, passthroughLog, { logCommand: false, errorFinder: spawned_1.SuccessIsReturn0ErrorFinder, }) .then(result => { const parts = log.split("HTTPSTATUS:"); let body = parts[0]; try { body = JSON.parse(body); } catch (err) { // ignore } return { status: +parts[1], body, }; }); }; return retry_1.doWithRetry(request, `Requesting '${url}'`, optionsToUse.retry); } } exports.CurlHttpClient = CurlHttpClient; /** * HttpClientFactory that creates HttpClient instances backed by curl. */ class CurlHttpClientFactory { create(url) { if (os.platform() === "win32") { throw new Error("CurlHttpClient is not available on Windows."); } return new CurlHttpClient(); } } exports.CurlHttpClientFactory = CurlHttpClientFactory; //# sourceMappingURL=curlHttpClient.js.map