in django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js [183:309]
service(serviceName) {
if (!serviceName) {
throw new TypeError("Invalid Service Name");
} else if (!(serviceName in this.serviceConfigurations)) {
throw new Error("Service :" + serviceName + " could not be found");
}
let serviceConfiguration = this.serviceConfigurations[serviceName];
let serviceObj = {};
let functionNames = Object.keys(serviceConfiguration);
for (let functionName of functionNames) {
let config = serviceConfiguration[functionName];
switch (config.requestType.toLowerCase()) {
case postKey:
case getKey:
case putKey:
case delKey:
break;
default:
throw new TypeError(
"Invalid request type: " +
config.requestType +
" for the function: " +
functionName +
" in the service: " +
serviceName
);
}
let pathParamsMapping = parsePathParams(config.url);
let queryParamsMapping = parseQueryMapping(config.queryParams);
serviceObj[functionName] = function (
params = {},
{ ignoreErrors, showSpinner, cache } = {
ignoreErrors: false,
showSpinner: true,
cache: false,
}
) {
let url = config.url;
let paramKeys = Object.keys(params);
let queryParams = {};
let bodyParams = {};
let initialData = undefined;
for (let paramKey of paramKeys) {
if (paramKey in pathParamsMapping) {
if (pathParamsMapping[paramKey] !== null) {
url = url.replace(
"<" + pathParamsMapping[paramKey] + ":" + paramKey + ">",
config.encodePathParams
? encodeURIComponent(params[paramKey])
: params[paramKey]
);
} else {
url = url.replace(
"<" + paramKey + ">",
config.encodePathParams
? encodeURIComponent(params[paramKey])
: params[paramKey]
);
}
} else if (paramKey in queryParamsMapping) {
if (queryParamsMapping[paramKey] === null) {
queryParams[paramKey] = params[paramKey];
} else {
queryParams[queryParamsMapping[paramKey]] = params[paramKey];
}
} else if (
(config.requestType == postKey || config.requestType == putKey) &&
config.bodyParams instanceof Array &&
paramKey in config.bodyParams
) {
bodyParams[paramKey] = params[paramKey];
} else if (
(config.requestType == postKey || config.requestType == putKey) &&
config.bodyParams !== null &&
config.bodyParams.name == paramKey
) {
bodyParams = params[paramKey];
} else if (
config.initialDataParam &&
paramKey === config.initialDataParam
) {
initialData = params[paramKey];
}
}
let paginationHandler = (data) => {
if (config.pagination === true && "next" in data) {
return new PaginationIterator(data, config.modelClass);
} else if (data instanceof Array) {
return data.map((item) => resultHandler(item));
} else {
return resultHandler(data);
}
};
let resultHandler = (data) => {
if (Array.isArray(data)) {
return data.map((item) => resultHandler(item));
}
return config.modelClass ? new config.modelClass(data) : data;
};
switch (config.requestType.toLowerCase()) {
case postKey:
return FetchUtils.post(url, bodyParams, queryParams, {
ignoreErrors,
showSpinner,
}).then(resultHandler);
case getKey:
if (initialData) {
return Promise.resolve(paginationHandler(initialData));
} else {
return FetchUtils.get(url, queryParams, {
ignoreErrors,
showSpinner,
cache,
}).then(paginationHandler);
}
case putKey:
return FetchUtils.put(url, bodyParams, {
ignoreErrors,
showSpinner,
}).then(resultHandler);
case delKey:
return FetchUtils.delete(url, { ignoreErrors, showSpinner });
}
};
}
return serviceObj;
}