test.aspose.barcode.cloud
Version:
Aspose.BarCode Cloud SDK for Node.js
150 lines (146 loc) • 5.77 kB
JavaScript
;
/*
* MIT License
* Copyright (c) 2018 Aspose Pty Ltd
* 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) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const localVarRequest = require("request");
class OAuth {
constructor(configuration) {
this._configuration = configuration;
}
/**
* Apply authentication settings to header and query params.
*/
applyToRequest(requestOptions) {
return __awaiter(this, void 0, void 0, function* () {
if (this._accessToken == null) {
yield this.requestToken();
}
if (requestOptions && requestOptions.headers) {
requestOptions.headers.Authorization = "Bearer " + this._accessToken;
}
return Promise.resolve();
});
}
applyUnauthorized() {
return __awaiter(this, void 0, void 0, function* () {
yield this.refreshToken();
});
}
requestToken() {
const requestOptions = {
method: "POST",
json: true,
uri: this._configuration.baseUrl + "/oauth2/token",
form: {
grant_type: "client_credentials",
client_id: this._configuration.appSID,
client_secret: this._configuration.appKey,
},
};
return new Promise((resolve, reject) => {
var self = this;
localVarRequest(requestOptions, (error, response, body) => {
if (error) {
reject(error);
}
else {
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
self._accessToken = response.body.access_token;
self._refreshToken = response.body.refresh_token;
resolve();
}
else {
reject();
}
}
});
});
}
refreshToken() {
const requestOptions = {
method: "POST",
json: true,
uri: this._configuration.baseUrl + "/oauth2/token",
form: {
grant_type: "refresh_token",
refresh_token: this._refreshToken,
},
};
return new Promise((resolve, reject) => {
var self = this;
localVarRequest(requestOptions, (error, response, body) => {
if (error) {
reject(error);
}
else {
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
self._accessToken = response.body.access_token;
self._refreshToken = response.body.refresh_token;
resolve();
}
else {
reject();
}
}
});
});
}
}
exports.OAuth = OAuth;
var ApiVersion;
(function (ApiVersion) {
ApiVersion[ApiVersion["v1"] = 0] = "v1";
ApiVersion[ApiVersion["v2"] = 1] = "v2";
ApiVersion[ApiVersion["v3"] = 2] = "v3";
})(ApiVersion = exports.ApiVersion || (exports.ApiVersion = {}));
class Configuration {
constructor(appSID, appKey, version) {
/**
* Gets or sets the API version.
*/
this.version = ApiVersion.v1;
this.appSID = appSID;
this.appKey = appKey;
this.baseUrl = "https://api.aspose.cloud";
if (version) {
this.version = version;
}
else {
this.version = ApiVersion.v1;
}
this.authentication = new OAuth(this);
}
/**
* Returns api base url
*/
getApiBaseUrl() {
return this.baseUrl + "/" + ApiVersion[this.version];
}
}
exports.Configuration = Configuration;
//# sourceMappingURL=configuration.js.map