UNPKG

ilorest

Version:
144 lines (119 loc) 8.14 kB
'use strict'; /* * (c) Copyright 2018 Hewlett Packard Enterprise Development LP * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and limitations * under the License. */ Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x18, _x19, _x20) { var _again = true; _function: while (_again) { var object = _x18, property = _x19, receiver = _x20; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x18 = parent; _x19 = property; _x20 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var _base = require('./base'); /** * HTTP client class * @extends baseClient */ var httpClient = (function (_baseClient) { _inherits(httpClient, _baseClient); /** * This class instance is initiated by helper function {@link restClient} or {@link redfishClient} */ function httpClient(baseUrl) { var userName = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; var password = arguments.length <= 2 || arguments[2] === undefined ? '' : arguments[2]; var biosPassword = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3]; var sessionKey = arguments.length <= 4 || arguments[4] === undefined ? null : arguments[4]; var defaultPrefix = arguments.length <= 5 || arguments[5] === undefined ? '/rest/v1' : arguments[5]; var timeout = arguments.length <= 6 || arguments[6] === undefined ? 60 : arguments[6]; var concurrent = arguments.length <= 7 || arguments[7] === undefined ? 5 : arguments[7]; var cacheOptions = arguments.length <= 8 || arguments[8] === undefined ? null : arguments[8]; var _this = this; var isRedfish = arguments.length <= 9 || arguments[9] === undefined ? false : arguments[9]; var proxy = arguments.length <= 10 || arguments[10] === undefined ? null : arguments[10]; _classCallCheck(this, httpClient); _get(Object.getPrototypeOf(httpClient.prototype), 'constructor', this).call(this, baseUrl, userName, password, biosPassword, sessionKey, defaultPrefix, timeout, concurrent, cacheOptions, proxy); this.isRedfish = isRedfish; _base._p.get(this).rootPromise = this.getRootObject(); _base._p.get(this).rootPromise.then(function (res) { _this.root = res.body; _this.loginUrl = _this.isRedfish ? _this.root.Links.Sessions['@odata.id'] : _this.root.links.Sessions.href; })['catch'](function () { _this.root = null; _this.loginUrl = null; }); } _createClass(httpClient, [{ key: '__getReqHeaders', value: function __getReqHeaders(headers, providerHeader) { headers = _get(Object.getPrototypeOf(httpClient.prototype), '__getReqHeaders', this).call(this, headers, providerHeader); if (this.isRedfish) { headers['OData-Version'] = '4.0'; } return headers; } }, { key: '__formOptions', value: function __formOptions(path) { var method = arguments.length <= 1 || arguments[1] === undefined ? 'GET' : arguments[1]; var args = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; var body = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3]; var headers = arguments.length <= 4 || arguments[4] === undefined ? null : arguments[4]; var timeout = arguments.length <= 5 || arguments[5] === undefined ? null : arguments[5]; var optionalPassword = arguments.length <= 6 || arguments[6] === undefined ? null : arguments[6]; var providerHeader = arguments.length <= 7 || arguments[7] === undefined ? null : arguments[7]; if (!this.isRedfish && path && path.indexOf(this.defaultPrefix) >= 0 && path.charAt(path.length - 1) === '/') { path = path.slice(0, path.length - 1); } else if (this.isRedfish && path && path.indexOf(this.defaultPrefix) >= 0 && path.charAt(path.length - 1) !== '/') { path = path + '/'; } return _get(Object.getPrototypeOf(httpClient.prototype), '__formOptions', this).call(this, path, method, args, body, headers, timeout, optionalPassword, providerHeader); } }, { key: 'logout', value: function logout() { var _this2 = this; var __sessionLocation = this.getSessionLocation(); if (!__sessionLocation) { return this.get(this.loginUrl)['catch'](function () { throw Error('Session location not found'); }).then(function (res) { var info = res.body.Oem; if (_this2.isRedfish) { for (var key in info) { if (info[key].Links && info[key].Links.MySession['@odata.id']) { __sessionLocation = info[key].Links.MySession['@odata.id']; break; } } } else { for (var key in info) { if (info[key].links && info[key].links.MySession.href) { __sessionLocation = info[key].links.MySession.href; break; } } } if (__sessionLocation) { _base._p.get(_this2).sessionLocation = __sessionLocation; return _get(Object.getPrototypeOf(httpClient.prototype), 'logout', _this2).call(_this2); } throw Error('Session location not found'); }); } else { return _get(Object.getPrototypeOf(httpClient.prototype), 'logout', this).call(this); } } }]); return httpClient; })(_base.baseClient); exports.httpClient = httpClient;