@sap/subaccount-destination-service-provider
Version:
Provide service consumption of SAP subaccount services
300 lines • 15.9 kB
JavaScript
;
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 vkbeautify = require("vkbeautify");
const Adapters = require("../utils/serviceProviderAdapter");
const service_provider_apis_1 = require("@sap/service-provider-apis");
const systemUtils_1 = require("../../utils/systemUtils");
const baseRetriever_1 = require("../../common/baseRetriever");
const systemUtils_2 = require("../../utils/systemUtils");
const errorUtils_1 = require("../../common/utils/errorUtils");
const retrieverUtils_1 = require("../utils/retrieverUtils");
const META_DATA_SUFFIX = "/$metadata";
const SEND_SERVICE_REQUEST_TIMEOUT = 40000;
class AbapServiceRetriever extends baseRetriever_1.BaseRetriever {
constructor(connectivityProvider) {
super(connectivityProvider);
}
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, errorUtils_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 = `${serviceUrlObj.pathname}${META_DATA_SUFFIX}${serviceUrlObj.search}${serviceUrlObj.hash}`;
try {
const credentialsHeader = yield this.getMetadataHeaders(credentials);
const connProviderParams = {
urlPath: serviceRetrieverUrl,
responseType: "text",
method: "get",
headers: credentialsHeader,
};
const responseBody = yield this.connectivityProvider.sendRequestViaDestination(connProviderParams, destinationName);
return {
data: vkbeautify.xml(responseBody),
encoding: service_provider_apis_1.EncodingMode.XML,
};
}
catch (e) {
(0, errorUtils_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 = `${pathname}/${entityName}${requestParams}${serviceUrlObj.hash}`;
try {
const credentialsHeader = (0, systemUtils_2.getCredentialsHeader)(credentials);
const connProviderParams = {
urlPath: serviceRetrieverUrl,
responseType: encoding === service_provider_apis_1.EncodingMode.JSON ? "json" : "text",
method: "get",
headers: credentialsHeader,
};
const responseBody = yield this.connectivityProvider.sendRequestViaDestination(connProviderParams, destinationName);
return (0, systemUtils_1.buildServiceCommonResponse)(encoding, responseBody);
}
catch (e) {
(0, errorUtils_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 = `${systemUtils_1.SubaccountAbapCatalogServiceUrl.getV2AnnotationUrl(serviceId)}`;
try {
const credentialsHeader = (0, systemUtils_2.getCredentialsHeader)(credentials);
const connProviderParams = {
urlPath: serviceRetrieverUrl,
responseType: "json",
method: "get",
headers: credentialsHeader,
};
const responseBody = yield this.connectivityProvider.sendRequestViaDestination(connProviderParams, destinationName);
let annotations = Adapters.AnnotationResponseToAnnotations(responseBody);
annotations = yield this.retrieveAnnotationsData(destinationName, annotations, credentialsHeader);
return annotations;
}
catch (e) {
(0, errorUtils_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.connectivityProvider.getSubAccountDestinations();
const systems = [];
responseBody.forEach((destinationConfiguration) => {
if ((0, systemUtils_2.filterDestination)(destinationConfiguration, filter)) {
systems.push(Adapters.DestinationToProviderSystem(destinationConfiguration, this));
}
});
return systems;
}
catch (e) {
(0, errorUtils_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, destinationName, 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, destinationName, credentials, services);
}
return services;
});
}
getMetadataHeaders(credentials) {
return __awaiter(this, void 0, void 0, function* () {
const headers = (0, systemUtils_2.getCredentialsHeader)(credentials);
headers.Accept = systemUtils_1.APPLICATION_XML_HEADER;
return headers;
});
}
getServiceRetrieveOldUrl(serviceRetrieverUrl, destinationName) {
if (serviceRetrieverUrl.includes(systemUtils_1.V4_RECOMMENDED_SERVICES_PATH)) {
return systemUtils_1.SubaccountAbapCatalogServiceUrl.v4Old;
}
return systemUtils_1.SubaccountAbapCatalogServiceUrl.v2Old;
}
getServiceRetrieveUrl(protocol, destinationName) {
const serviceRetriveUrls = [];
if (protocol) {
protocol.forEach((protocolValue) => {
if (protocolValue.toLowerCase() == "odatav2") {
serviceRetriveUrls.push(systemUtils_1.SubaccountAbapCatalogServiceUrl.v2New);
}
if (protocolValue.toLowerCase() == "odatav4") {
serviceRetriveUrls.push(systemUtils_1.SubaccountAbapCatalogServiceUrl.v4New);
}
});
}
else {
serviceRetriveUrls.push(systemUtils_1.SubaccountAbapCatalogServiceUrl.v2New);
}
return serviceRetriveUrls;
}
sendServiceRequest(serviceUrl, destinationName, credentials, services) {
return __awaiter(this, void 0, void 0, function* () {
let responseBody;
try {
const credentialsHeader = (0, systemUtils_2.getCredentialsHeader)(credentials);
const connProviderParams = {
urlPath: serviceUrl,
responseType: "json",
method: "get",
headers: credentialsHeader,
timeout: SEND_SERVICE_REQUEST_TIMEOUT,
interceptors: {
responseInterceptor: {
onFulfilled: retrieverUtils_1.htmlResponseInterceptor,
},
},
};
responseBody = yield this.connectivityProvider.sendRequestViaDestination(connProviderParams, destinationName);
}
catch (e) {
(0, errorUtils_1.handleProviderError)(e, service_provider_apis_1.messages.SYS_ERROR_FAIL_RETRIEVE_SERVICES, this.logger);
}
yield this.handleServicesResponse(responseBody, destinationName, serviceUrl, credentials, services);
});
}
handleServicesResponse(responseBody, destinationName, serviceUrl, credentials, services) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
let responseServices;
const protocol = AbapServiceRetriever.getServiceProtocolFromUrl(serviceUrl);
switch (protocol) {
case "odatav2":
responseServices = (_a = responseBody === null || responseBody === void 0 ? void 0 : responseBody.d) === null || _a === void 0 ? void 0 : _a.results;
break;
case "odatav4":
responseServices = responseBody === null || responseBody === void 0 ? void 0 : 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}`, destinationName, 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, credentialsHeader) {
return __awaiter(this, void 0, void 0, function* () {
for (const annotation of annotations) {
if (annotation) {
const requestPath = `${systemUtils_1.ODATA_V2_CATALOG_SERVICES}/${annotation.src}`;
try {
const connProviderParams = {
urlPath: requestPath,
responseType: "text",
method: "get",
headers: credentialsHeader,
};
const responseBody = yield this.connectivityProvider.sendRequestViaDestination(connProviderParams, destinationName);
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 = `/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/ServiceTypeForHUBServices('${service.id}')`;
const credentialsHeader = (0, systemUtils_2.getCredentialsHeader)(credentials);
let responseBody;
try {
const connProviderParams = {
urlPath: requestPath,
responseType: "json",
method: "get",
headers: credentialsHeader,
};
responseBody = yield this.connectivityProvider.sendRequestViaDestination(connProviderParams, destinationName);
}
catch (e) {
(0, errorUtils_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