UNPKG

@sap/subaccount-destination-service-provider

Version:

Provide service consumption of SAP subaccount services

260 lines 14.8 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.AbapServiceRetriever = void 0; const lodash_1 = require("lodash"); const vkbeautify = require("vkbeautify"); const xml2jsonParser = require("xml2js"); const Adapters = require("../utils/serviceProviderAdapter"); const service_provider_apis_1 = require("@sap/service-provider-apis"); const systemUtils_1 = require("../../utils/systemUtils"); const abapBaseRetriever_1 = require("./abapBaseRetriever"); const retrieverUtils_1 = require("../utils/retrieverUtils"); const META_DATA_SUFFIX = "/$metadata"; class AbapServiceRetriever extends abapBaseRetriever_1.AbapBaseRetriever { constructor() { super(); } retrieveServices(destinationName, destinationUrl, credentials, filter) { return __awaiter(this, void 0, void 0, function* () { this.logger.debug(`Inside retrieveServices`); const serviceRetrieverUrls = this.getServiceRetrieveUrl(filter === null || filter === void 0 ? void 0 : filter.get("protocol"), destinationName); let error; const services = []; let errorCount = 0; for (const serviceRetrieverUrl of serviceRetrieverUrls) { try { yield this.executeSendServiceRequest(destinationName, serviceRetrieverUrl, services, credentials); } catch (e) { error = e; this.logger.warn(`${serviceRetrieverUrl} failed with error: ${error.message}`); errorCount++; } } if (error && errorCount === serviceRetrieverUrls.length) { (0, retrieverUtils_1.handleProviderError)(error, "retrieveServices fails with", this.logger); } this.logger.debug(`Exiting retrieveServices with ${services.length} services`); return services; }); } retrieveMetadata(destinationName, serviceUrl, encoding, credentials) { return __awaiter(this, void 0, void 0, function* () { 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 serviceUrlObj = new URL(`https://example.org/${serviceUrl}`); const serviceRetrieverUrl = `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${serviceUrlObj.pathname}${META_DATA_SUFFIX}${serviceUrlObj.search}${serviceUrlObj.hash}`; try { const basCredentialsHeader = yield this.getMetadataHeaders(credentials); const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceRetrieverUrl, this.proxy, false, basCredentialsHeader); return { data: vkbeautify.xml(responseBody), encoding: service_provider_apis_1.EncodingMode.XML, }; } catch (e) { (0, retrieverUtils_1.handleProviderError)(e, service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_METADATA, this.logger); } }); } retrieveLiveData(destinationName, serviceUrl, entityName, encoding, filter, credentials, headerParameters) { return __awaiter(this, void 0, void 0, function* () { filter = (0, service_provider_apis_1.addFormatParamToFilter)(filter, encoding); if (!serviceUrl.startsWith("/")) { serviceUrl = "/" + serviceUrl; } const serviceUrlObj = new URL(`https://example.org${serviceUrl}`); const requestParams = (0, service_provider_apis_1.getRequestParamString)(filter); const pathname = serviceUrl ? serviceUrlObj.pathname : ""; const serviceRetrieverUrl = `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${pathname}/${entityName}${requestParams}${serviceUrlObj.hash}`; try { const basCredentialsHeader = yield this.getBasCredentialsHeader(credentials); const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceRetrieverUrl, this.proxy, encoding === service_provider_apis_1.EncodingMode.JSON, basCredentialsHeader); return (0, systemUtils_1.buildServiceCommonResponse)(encoding, responseBody); } catch (e) { (0, retrieverUtils_1.handleProviderError)(e, service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_ENTITY_DATA(entityName), this.logger); } }); } retrieveAnnotations(destinationName, serviceId, credentials) { return __awaiter(this, void 0, void 0, function* () { const serviceRetrieverUrl = `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.SubaccountAbapCatalogServiceUrl.getV2AnnotationUrl(serviceId)}`; try { const basCredentialsHeader = yield this.getBasCredentialsHeader(credentials); const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceRetrieverUrl, this.proxy, false, basCredentialsHeader); const parsedAnnotations = yield xml2jsonParser.parseStringPromise(responseBody, { explicitArray: false, mergeAttrs: true, }); let annotations = Adapters.AnnotationResponseToAnnotations(parsedAnnotations); annotations = yield this.retrieveAnnotationsData(destinationName, annotations, basCredentialsHeader); return annotations; } catch (e) { (0, retrieverUtils_1.handleProviderError)(e, service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_ANNOTATIONS, this.logger); } }); } retrieveDestinations(filter) { return __awaiter(this, void 0, void 0, function* () { try { const responseBody = yield this.executeRetrieveDestinationsRequest(); const systems = []; responseBody.forEach((destinationConfiguration) => { if (this.filterDestination(destinationConfiguration, filter)) { systems.push(Adapters.DestinationToProviderSystem(destinationConfiguration, this)); } }); return systems; } catch (e) { (0, retrieverUtils_1.handleProviderError)(e, service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_DESTINATIONS, this.logger); } }); } executeSendServiceRequest(destinationName, destinationUrl, services, credentials) { return __awaiter(this, void 0, void 0, function* () { this.logger.debug(`Inside executeSendServiceRequest`); try { yield this.sendServiceRequest(destinationUrl, credentials, services); } catch (e) { this.logger.debug(`executeSendServiceRequest: failed to retrieve data using ${destinationUrl} url, error: ${e}`); const serviceRetrieveOldUrl = this.getServiceRetrieveOldUrl(destinationUrl, destinationName); this.logger.debug(`Trying with old API with URL ${serviceRetrieveOldUrl}`); yield this.sendServiceRequest(serviceRetrieveOldUrl, credentials, services); } return services; }); } getMetadataHeaders(credentials) { return __awaiter(this, void 0, void 0, function* () { const headers = yield this.getBasCredentialsHeader(credentials); headers.Accept = systemUtils_1.APPLICATION_XML_HEADER; return headers; }); } getServiceRetrieveOldUrl(serviceRetrieverUrl, destinationName) { if (serviceRetrieverUrl.includes(systemUtils_1.V4_RECOMMENDED_SERVICES_PATH)) { return `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.SubaccountAbapCatalogServiceUrl.v4Old}`; } return `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.SubaccountAbapCatalogServiceUrl.v2Old}`; } getServiceRetrieveUrl(protocol, destinationName) { const serviceRetriveUrls = []; if (protocol) { protocol.forEach((protocolValue) => { if (protocolValue.toLowerCase() == "odatav2") { serviceRetriveUrls.push(`${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.SubaccountAbapCatalogServiceUrl.v2New}`); } if (protocolValue.toLowerCase() == "odatav4") { serviceRetriveUrls.push(`${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.SubaccountAbapCatalogServiceUrl.v4New}`); } }); } else { serviceRetriveUrls.push(`${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.SubaccountAbapCatalogServiceUrl.v2New}`); } return serviceRetriveUrls; } sendServiceRequest(serviceUrl, credentials, services) { return __awaiter(this, void 0, void 0, function* () { let responseBody; try { const basCredentialsHeader = yield this.getBasCredentialsHeader(credentials); responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, serviceUrl, this.proxy, true, basCredentialsHeader); } catch (e) { (0, retrieverUtils_1.handleProviderError)(e, service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_SERVICES, this.logger); } yield this.handleServicesResponse(responseBody, serviceUrl, credentials, services); }); } handleServicesResponse(responseBody, serviceUrl, credentials, services) { return __awaiter(this, void 0, void 0, function* () { let responseServices; const protocol = AbapServiceRetriever.getServiceProtocolFromUrl(serviceUrl); switch (protocol) { case "odatav2": responseServices = (0, lodash_1.get)(responseBody, ["d", "results"]); break; case "odatav4": responseServices = (0, lodash_1.get)(responseBody, ["value"]); break; default: throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.INTERNAL_ERROR, service_provider_apis_1.messages.SYS_ERROR_FAIL_PARSE_RESPONSE); } if (!responseServices) { throw new service_provider_apis_1.ServiceProviderError(service_provider_apis_1.ServiceProviderErrorCode.RETRIEVE_DATA, service_provider_apis_1.messages.SYS_ERROR_FAIL_PARSE_RESPONSE); } responseServices.forEach((responseService) => { Adapters.ServiceResponseToService(responseService, protocol).forEach((service) => { services.push(service); }); }); if (protocol == "odatav4" && responseBody["@odata.nextLink"]) { const nextUrl = responseBody["@odata.nextLink"]; const skipToken = nextUrl.substring(nextUrl.lastIndexOf("$skiptoken"), nextUrl.length); const newServiceUrl = serviceUrl.lastIndexOf("&$skiptoken") < 0 ? serviceUrl : serviceUrl.substring(0, serviceUrl.lastIndexOf("&$skiptoken")); yield this.sendServiceRequest(`${newServiceUrl}&${skipToken}`, credentials, services); } }); } static getServiceProtocolFromUrl(serviceUrl) { if (serviceUrl.includes(systemUtils_1.ODATA_V2_CATALOG_SERVICES)) { return "odatav2"; } if (serviceUrl.includes(systemUtils_1.ODATA_V4_CATALOG_SERVICES)) { return "odatav4"; } return ""; } retrieveAnnotationsData(destinationName, annotations, basCredentialsHeader) { return __awaiter(this, void 0, void 0, function* () { for (const annotation of annotations) { if (annotation) { const requestPath = `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}${systemUtils_1.ODATA_V2_CATALOG_SERVICES}/${annotation.src}`; try { const responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, requestPath, this.proxy, false, basCredentialsHeader); annotation.data = vkbeautify.xml(responseBody); } catch (e) { this.logger.warn(`Could not retrieve Annotations value from: ${requestPath}`); annotation.data = ""; } } } return annotations; }); } retrieveServiceUiType(destinationName, service, credentials) { return __awaiter(this, void 0, void 0, function* () { const requestPath = `${abapBaseRetriever_1.DESTINATION_PATH}/${destinationName}/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/ServiceTypeForHUBServices('${service.id}')`; const basCredentialsHeader = yield this.getBasCredentialsHeader(credentials); let responseBody; try { responseBody = yield service_provider_apis_1.Connectivity.sendRequest(this.h2oUrl, requestPath, this.proxy, false, basCredentialsHeader); } catch (e) { (0, retrieverUtils_1.handleProviderError)(e, service_provider_apis_1.messages.ERROR_FAILED_TO_GET_SERVICE_TYPE, this.logger); } return responseBody.d.ServiceType; }); } } exports.AbapServiceRetriever = AbapServiceRetriever; //# sourceMappingURL=abapServiceRetriever.js.map