UNPKG

@sap/subaccount-destination-service-provider

Version:

Provide service consumption of SAP subaccount services

163 lines 9.18 kB
"use strict"; 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.C4cRetrieve = void 0; const vkbeautify = require("vkbeautify"); const lodash_1 = require("lodash"); const bas_sdk_1 = require("@sap/bas-sdk"); const service_provider_apis_1 = require("@sap/service-provider-apis"); const systemUtils_1 = require("../../utils/systemUtils"); const c4cDestinationProviderSystem_1 = require("../providers/c4cDestinationProviderSystem"); const c4cService_1 = require("../type/c4cService"); const utils_1 = require("../utils/utils"); const messages_1 = require("../../i18n/messages"); const DESTINATIONS = `/destinations/`; const C4C_GET_SERVICE_SUFFIX_URL = "/sap/c4c/odata/v1/odataservicecatalog/ODataServiceCollection"; const DEPRECATED_SERVICES_NAME = ["c4codata"]; const SUPER_SERVICE = "c4codataapi"; const SUPER_SERVICES_SUFFIX_URL = `/sap/c4c/odata/v1/odata_apihub_services/GetIndividualServices?VirtualServiceName='c4codataapi'`; const METADATA_FILTER = "?$filter="; class C4cRetrieve { constructor() { this.h2oUrl = (0, lodash_1.get)(process, "env.H2O_URL"); this.proxy = (0, lodash_1.get)(process, "env.http_proxy"); } retrieveAnnotations(systemName, serviceId, credentials) { return __awaiter(this, void 0, void 0, function* () { throw new Error(messages_1.messages.METHOD_NOT_IMPLEMENTED()); }); } retrieveDestinations(filter) { return __awaiter(this, void 0, void 0, function* () { try { const c4cFilter = (0, utils_1.getC4cDestinationFilter)(filter); const c4cDestinations = yield bas_sdk_1.destinations.getDestinations(c4cFilter); const providerSystems = c4cDestinations.map((destination) => this.DestinationListInfoToC4cProviderSystem(destination)); return providerSystems; } catch (e) { const error = e; throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, error); } }); } retrieveServices(systemName, systemUrl, credentials, filter) { return __awaiter(this, void 0, void 0, function* () { const url = `${DESTINATIONS}${systemName}${C4C_GET_SERVICE_SUFFIX_URL}`; try { const basHeader = (0, systemUtils_1.createBasHeader)(credentials); const rawServices = yield this.getRawServices(url, basHeader); let superService; const services = []; rawServices.forEach((rawService) => { if (DEPRECATED_SERVICES_NAME.includes(rawService.ServiceName)) { return; } else if (SUPER_SERVICE === rawService.ServiceName) { superService = new c4cService_1.C4cService(rawService); return; } services.push(new c4cService_1.C4cService(rawService)); }); if (!superService) { return services; } const superServicesUrl = `${DESTINATIONS}${systemName}${SUPER_SERVICES_SUFFIX_URL}`; const additionalService = yield this.superServiceToServices(superService, superServicesUrl, basHeader); return [...services, ...additionalService]; } catch (e) { e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_SERVICES}: ${e.message}`; throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e); } }); } superServiceToServices(superService, url, headers) { return __awaiter(this, void 0, void 0, function* () { let rawSuperServiceList; try { rawSuperServiceList = yield this.getRawServices(url, headers); } catch (e) { return [superService]; } return rawSuperServiceList.map((rawSuperService) => { const rawService = { ServiceName: (0, utils_1.addSpaceBeforeCapitalLetter)(rawSuperService.LeadingEntityTypeName), Namespace: superService.namespace, MetadataURL: superService.url, metadataFilter: `${METADATA_FILTER}${rawSuperService.LeadingEntityTypeName}`, }; return new c4cService_1.C4cService(rawService); }); }); } retrieveMetadata(systemName, serviceUrl, encoding, credentials) { return __awaiter(this, void 0, void 0, function* () { try { if (encoding !== service_provider_apis_1.EncodingMode.XML) { const e = new Error(`${service_provider_apis_1.messages.SYS_ERROR_FAIL_PARSE_RESPONSE}`); throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e); } const basHeader = this.getMetadataHeaders(credentials); const metadataUrl = `${DESTINATIONS}${systemName}${serviceUrl}`; const connectivityResponse = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, metadataUrl, this.proxy, false, basHeader); return { data: vkbeautify.xml(connectivityResponse), encoding: service_provider_apis_1.EncodingMode.XML, }; } catch (e) { e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_METADATA}: ${e.message}`; throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e); } }); } retrieveLiveData(systemName, serviceUrl, entityName, encoding, filter, credentials, headerParameters) { return __awaiter(this, void 0, void 0, function* () { const requestParams = (0, service_provider_apis_1.getRequestParamString)(filter); const liveDataUrl = `${DESTINATIONS}${systemName}${serviceUrl}/${entityName}${requestParams}`; try { const basHeader = (0, systemUtils_1.createBasHeader)(credentials); const connectivityResponse = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, liveDataUrl, this.proxy, true, basHeader); return { data: JSON.stringify(connectivityResponse), encoding: service_provider_apis_1.EncodingMode.JSON, }; } catch (e) { e.message = `${service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_ENTITY_DATA(entityName)}: ${e.message}`; const serviceProviderError = new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, e); serviceProviderError.stack = `${serviceProviderError.stack}\nCaused By:\n${e.stack}`; throw serviceProviderError; } }); } getRawServices(url, headers) { return __awaiter(this, void 0, void 0, function* () { const connectivityResponse = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, url, this.proxy, true, headers); if (connectivityResponse && connectivityResponse.d && connectivityResponse.d.results) { return connectivityResponse.d.results; } return []; }); } DestinationListInfoToC4cProviderSystem(destination) { return new c4cDestinationProviderSystem_1.C4cDestinationProviderSystem(destination.name, destination.host, service_provider_apis_1.DataType.CATALOG_C4C, (0, systemUtils_1.getProxyType)(destination.proxyType), destination.credentials.authentication, this, destination === null || destination === void 0 ? void 0 : destination.basProperties.productName, destination === null || destination === void 0 ? void 0 : destination.description); } getMetadataHeaders(credentials) { const headers = (0, systemUtils_1.createBasHeader)(credentials); headers.Accept = systemUtils_1.APPLICATION_XML_HEADER; return headers; } } exports.C4cRetrieve = C4cRetrieve; //# sourceMappingURL=c4cRetrieve.js.map