(action, callback, options, get_params)
| 472 | } |
| 473 | |
| 474 | function call_api(action, callback, options, get_params) { |
| 475 | if (typeof callback !== "function") { |
| 476 | callback = function () { |
| 477 | }; |
| 478 | } |
| 479 | |
| 480 | const USE_PROMISES = !options.disable_promises; |
| 481 | |
| 482 | const deferred = utils.deferredPromise(); |
| 483 | if (options == null) { |
| 484 | options = {}; |
| 485 | } |
| 486 | |
| 487 | let [params, unsigned_params, file] = get_params.call(); |
| 488 | params = utils.process_request_params(params, options); |
| 489 | params = extend(params, unsigned_params); |
| 490 | let api_url = utils.api_url(action, options); |
| 491 | let boundary = utils.random_public_id(); |
| 492 | let errorRaised = false; |
| 493 | let handle_response = function (res) { |
| 494 | // let buffer; |
| 495 | if (errorRaised) { |
| 496 | |
| 497 | // Already reported |
| 498 | } else if (res.error) { |
| 499 | errorRaised = true; |
| 500 | |
| 501 | if (USE_PROMISES) { |
| 502 | deferred.reject(res); |
| 503 | } |
| 504 | callback(res); |
| 505 | } else if (includes([200, 400, 401, 404, 420, 500], res.statusCode)) { |
| 506 | let buffer = ""; |
| 507 | res.on("data", (d) => { |
| 508 | buffer += d; |
| 509 | return buffer; |
| 510 | }); |
| 511 | res.on("end", () => { |
| 512 | let result; |
| 513 | if (errorRaised) { |
| 514 | return; |
| 515 | } |
| 516 | result = parseResult(buffer, res); |
| 517 | if (result.error) { |
| 518 | result.error.http_code = res.statusCode; |
| 519 | if (USE_PROMISES) { |
| 520 | deferred.reject(result.error); |
| 521 | } |
| 522 | } else { |
| 523 | cacheResults(result, options); |
| 524 | if (USE_PROMISES) { |
| 525 | deferred.resolve(result); |
| 526 | } |
| 527 | } |
| 528 | callback(result); |
| 529 | }); |
| 530 | res.on("error", (error) => { |
| 531 | errorRaised = true; |
no test coverage detected