@gridscale/gsclient-js
Version: 
gridscale API Wrapper
180 lines • 7.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const api_1 = require("./api");
const Server_1 = require("./Objects/Server");
const Storage_1 = require("./Objects/Storage");
const Network_1 = require("./Objects/Network");
const IP_1 = require("./Objects/IP");
const ISOImage_1 = require("./Objects/ISOImage");
const SSHKey_1 = require("./Objects/SSHKey");
const Template_1 = require("./Objects/Template");
const Location_1 = require("./Objects/Location");
const ObjectStorage_1 = require("./Objects/ObjectStorage");
const Label_1 = require("./Objects/Label");
const Loadbalancer_1 = require("./Objects/Loadbalancer");
const Events_1 = require("./Objects/Events");
const Firewall_1 = require("./Objects/Firewall");
const PAAS_1 = require("./Objects/PAAS");
const Deleted_1 = require("./Objects/Deleted");
const PaasServiceTemplate_1 = require("./Objects/PaasServiceTemplate");
const PaasService_1 = require("./Objects/PaasService");
const PaasSecurityZone_1 = require("./Objects/PaasSecurityZone");
const PaasServiceMetrics_1 = require("./Objects/PaasServiceMetrics");
const MarketplaceApplication_1 = require("./Objects/MarketplaceApplication");
const MarketplaceApplication_2 = require("./Objects/ServiceMarketplace/MarketplaceApplication");
const Certificate_1 = require("./Objects/Certificate");
const BackupLocation_1 = require("./Objects/BackupLocation");
const MarketplacePlan_1 = require("./Objects/ServiceMarketplace/MarketplacePlan");
const MarketplacePlanSettings_1 = require("./Objects/ServiceMarketplace/MarketplacePlanSettings");
const MarketplaceApplicationInstance_1 = require("./Objects/ServiceMarketplace/MarketplaceApplicationInstance");
const MarketplaceVersion_1 = require("./Objects/ServiceMarketplace/MarketplaceVersion");
const GPU_1 = require("./Objects/GPU");
const _version_1 = require("./.version");
const PaasDeprecatedClusters_1 = require("./Objects/PaasDeprecatedClusters");
const ContractStorageBackup_1 = require("./Objects/ContractStorageBackup");
/**
 * generate Client Class for all Connections
 * test
 */
class GridscaleClient {
    /**
     * Init Client with Default Values
     *
     *
     * @param _token Security Token
     * @param _userId UUID of User
     * @param _options
     * @param _isolated (if true, use isolated api which can be used alongside other instances. Default behavior is shared settings/tokens between the client instances)
     */
    constructor(_token, _userId, _options = {}, _isolated = false) {
        this.VERSION = _version_1.VERSION;
        if (_isolated) {
            this.myapi = new api_1.APIClass();
        }
        // Store Security Tokens
        this.api.storeToken(_token, _userId);
        // Store advanced Options
        this.api.setOptions(_options);
        // Call Subtypes
        this.Server = new Server_1.Server(this.api);
        this.Storage = new Storage_1.Storage(this.api);
        this.Network = new Network_1.Network(this.api);
        this.IP = new IP_1.IP(this.api);
        this.ISOImage = new ISOImage_1.ISOImage(this.api);
        this.SSHKey = new SSHKey_1.SSHKey(this.api);
        this.Template = new Template_1.Template(this.api);
        this.Location = new Location_1.Location(this.api);
        this.ObjectStorage = new ObjectStorage_1.ObjectStorage(this.api);
        this.Label = new Label_1.Label(this.api);
        this.Loadbalancer = new Loadbalancer_1.Loadbalancer(this.api);
        this.Events = new Events_1.Events(this.api);
        this.Firewall = new Firewall_1.Firewall(this.api);
        this.PAAS = new PAAS_1.PAAS(this.api);
        this.PaasDeprecatedClusters = new PaasDeprecatedClusters_1.PaasDeprecatedClusters(this.api);
        this.PaasServiceTemplate = new PaasServiceTemplate_1.PaasServiceTemplate(this.api);
        this.PaasService = new PaasService_1.PaasService(this.api);
        this.PaasSecurityZone = new PaasSecurityZone_1.PaasSecurityZone(this.api);
        this.Deleted = new Deleted_1.Deleted(this.api);
        this.MarketplaceApplication = new MarketplaceApplication_1.MarketplaceApplication(this.api);
        this.ServiceMarketplaceApplication = new MarketplaceApplication_2.MarketplaceApplication(this.api);
        this.ServiceMarketplaceApplicationInstance = new MarketplaceApplicationInstance_1.MarketplaceApplicationInstance(this.api);
        this.ServiceMarketplacePlan = new MarketplacePlan_1.MarketplacePlan(this.api);
        this.ServiceMarketplacePlanSettings = new MarketplacePlanSettings_1.MarketplacePlanSettings(this.api);
        this.ServiceMarketplaceVersion = new MarketplaceVersion_1.MarketplaceVersion(this.api);
        this.Certificate = new Certificate_1.Certificate(this.api);
        this.BackupLocation = new BackupLocation_1.BackupLocation(this.api);
        this.GPU = new GPU_1.GPU(this.api);
        this.ContractStorageBackup = new ContractStorageBackup_1.ContractStorageBackup(this.api);
        this.watchRequest = this.api.watchRequest.bind(this.api);
    }
    /**
     * Set the identifier of the client (used in X-Api-Client Header)
     * @param _client
     */
    setApiClient(_client) {
        this.api.storeClient(_client);
    }
    /**
     * Set a new Token and User-UUID
     * @param _token
     * @param _userId
     */
    setToken(_token, _userUUID) {
        this.api.storeToken(_token, _userUUID);
    }
    /**
     * Set the HTTP endpoint of the API
     * @param _endpoint
     */
    setEndpoint(_endpoint) {
        this.api.setOptions({ endpoint: _endpoint });
    }
    /**
     * Inject a custom fetch method, otherwise the API will decide if to use the browser's fetch method or a polyfill
     * @param _fetch
     */
    setFetch(_fetch) {
        this.api.setOptions({ fetch: fetch });
    }
    /**
     * set addiotional headers
     */
    setAdditionalHeaders(additionalHeaders) {
        this.api.setOptions({ additionalHeaders });
    }
    /**
     * Add an additional logger callback, called whenever an error is happening
     * @param _callback
     */
    addLogger(_callback) {
        this.api.addLogger(_callback);
    }
    /**
     * Calls the Validate Token Endpoint of the API
     * @returns HTTP Promise
     */
    validateToken() {
        return this.api.get('/validate_token');
    }
    /**
     * Get the paas service metrics API which is a special one as the service-uuid is required early in the URL
     * @param _serviceUUID
     */
    PaasServiceMetrics(_serviceUUID) {
        return new PaasServiceMetrics_1.PaasServiceMetrics(this.api, _serviceUUID);
    }
    /**
     * Stringifies all non string-values of a HTTP Response (e.g. headers)
     * @param object
     * @deprecated
     */
    stringifyResponseRequest(object) {
        // tslint:disable-next-line: no-any
        const tmp = {};
        for (let _key in object) {
            if (object.hasOwnProperty(_key)) {
                const _val = object[_key];
                if (_val instanceof Headers) {
                    tmp[_key] = {};
                    _val.forEach((_h, _k) => {
                        tmp[_key][_k] = _h;
                    });
                }
                else if (_val instanceof Request) {
                    tmp[_key] = this.stringifyResponseRequest(_val);
                }
                else if (['string', 'number', 'object', 'boolean'].indexOf(typeof (_val)) >= 0) {
                    tmp[_key] = _val;
                }
            }
        }
        ;
        return tmp;
    }
    get api() {
        return this.myapi || api_1.api;
    }
}
exports.Client = GridscaleClient;
//# sourceMappingURL=client.js.map