@asposecloud/aspose-email-cloud
Version:
Aspose.Email Cloud Node.js SDK
153 lines (149 loc) • 7.42 kB
JavaScript
/*
* MIT License
* Copyright (c) 2018-2019 Aspose Pty Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addQueryParameterToUrl = exports.invokeApiMethod = void 0;
const request = require("request");
const requestDebug = require("request-debug");
const api_error_1 = require("./api-error");
const object_serializer_1 = require("./object-serializer");
/**
* Invoke api method
* @param requestOptions request parameters
* @param confguration api configuration
* @param notApplyAuthToRequest if setted to true, auth is not applied to request
*/
function invokeApiMethod(requestOptions, confguration, notApplyAuthToRequest) {
return __awaiter(this, void 0, void 0, function* () {
return yield invokeApiMethodInternal(requestOptions, confguration, notApplyAuthToRequest);
});
}
exports.invokeApiMethod = invokeApiMethod;
/**
* Add parameter to query
* @param url url
* @param queryParameters queryParameters
* @param parameterName parameterName
* @param parameterValue parameterValue
*/
function addQueryParameterToUrl(url, queryParameters, parameterName, parameterValue) {
if (parameterValue !== undefined) {
if (url.indexOf("{" + parameterName + "}") >= 0) {
url = url.replace("{" + parameterName + "}", String(parameterValue));
}
else {
queryParameters[parameterName] = String(parameterValue);
}
}
else {
url = url.replace("/{" + parameterName + "}", "");
}
return url;
}
exports.addQueryParameterToUrl = addQueryParameterToUrl;
/**
* Invoke api method
* @param requestOptions request parameters
* @param confguration api configuration
* @param notApplyAuthToRequest if setted to true, auth is not applied to request
*/
function invokeApiMethodInternal(requestOptions, confguration, notApplyAuthToRequest) {
return __awaiter(this, void 0, void 0, function* () {
requestDebug(request, (type, data, r) => {
if (r.writeDebugToConsole) {
const toLog = {};
toLog[type] = data;
// tslint:disable-next-line:no-console
console.log(JSON.stringify(toLog, undefined, 2));
}
});
if (!requestOptions.headers) {
requestOptions.headers = {};
}
requestOptions.headers["x-aspose-client"] = "node.js sdk";
requestOptions.headers["x-aspose-client-version"] = "19.9.0";
requestOptions.timeout = 600000;
if (requestOptions.formData && requestOptions.body) {
return Promise.reject("You can't send both form data and body.");
}
if (requestOptions.formData) {
requestOptions.headers["Content-Type"] = "multipart/form-data";
}
else if (requestOptions.body && requestOptions.body instanceof Buffer && requestOptions.body.length > 0) {
requestOptions.headers["Content-Type"] = "application/octet-stream";
}
const auth = confguration.authentication;
if (!confguration.onPremise && !notApplyAuthToRequest) {
yield auth.applyToRequest(requestOptions, confguration);
}
requestOptions.pool = { maxSockets: 5 };
return new Promise((resolve, reject) => {
const r = request(requestOptions, (error, response) => __awaiter(this, void 0, void 0, function* () {
if (error) {
return reject(error);
}
else {
if (response.statusCode >= 200 && response.statusCode <= 299) {
return resolve(response);
}
else if (response.statusCode === 401 && !notApplyAuthToRequest) {
return reject(new api_error_1.ApiError("Authentication failed!", response.statusCode, null));
}
else {
try {
let modelError = null;
const bodyContent = response.body;
if (bodyContent) {
if (bodyContent instanceof Buffer) {
modelError = object_serializer_1.ObjectSerializer.deserialize(bodyContent.toString("utf8"), "ModelError");
return reject(new api_error_1.ApiError(response.body, response.statusCode, modelError));
}
else if (bodyContent.Message) {
return reject(new api_error_1.ApiError(bodyContent.Message, response.statusCode, null));
}
else {
modelError = object_serializer_1.ObjectSerializer.deserialize(bodyContent, "ModelError");
return reject(new api_error_1.ApiError(response.body, response.statusCode, bodyContent));
}
}
else {
return reject(new api_error_1.ApiError(null, response.statusCode, null));
}
}
catch (error) {
return reject(new api_error_1.ApiError(`Failed to parse Aspose.Imaging Cloud API error message: ${response.body}`, response.statusCode, null));
}
}
}
}));
r.writeDebugToConsole = confguration.debugMode;
});
});
}
//# sourceMappingURL=request-helper.js.map
;