This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit 1924b7b596b8d6d273fadcc7829f2fee019d5339
Author: Marcus Christie <[email protected]>
AuthorDate: Fri Aug 27 17:10:01 2021 -0400

    AIRAVATA-3497 JS utility code for loading an input or output file
---
 .../js/utils/ExperimentUtils.js                    | 80 +++++++++++++++++++++-
 1 file changed, 78 insertions(+), 2 deletions(-)

diff --git 
a/django_airavata/apps/api/static/django_airavata_api/js/utils/ExperimentUtils.js
 
b/django_airavata/apps/api/static/django_airavata_api/js/utils/ExperimentUtils.js
index be91f0a..c8a7954 100644
--- 
a/django_airavata/apps/api/static/django_airavata_api/js/utils/ExperimentUtils.js
+++ 
b/django_airavata/apps/api/static/django_airavata_api/js/utils/ExperimentUtils.js
@@ -22,7 +22,9 @@ const createExperiment = async function ({
       applicationName
     );
   } else {
-    throw new Error("Either applicationInterfaceId or applicationId or 
applicationName is required");
+    throw new Error(
+      "Either applicationInterfaceId or applicationId or applicationName is 
required"
+    );
   }
   const applicationModuleId = applicationInterface.applicationModuleId;
   let computeResourceId = null;
@@ -170,8 +172,82 @@ const loadWorkspacePreferences = async function () {
   return await services.WorkspacePreferencesService.get();
 };
 
-export { createExperiment };
+const loadExperiment = async function (experimentId) {
+  return await services.ExperimentService.retrieve({ lookup: experimentId });
+};
+
+const readDataProduct = async function (
+  dataProductURI,
+  { bodyType = "text" } = {}
+) {
+  return await fetch(
+    `/sdk/download?data-product-uri=${encodeURIComponent(dataProductURI)}`,
+    {
+      credentials: "same-origin",
+    }
+  ).then((r) => {
+    if (r.status === 404) {
+      return null;
+    }
+    if (!r.ok) {
+      throw new Error(r.statusText);
+    }
+    return r[bodyType]();
+  });
+};
+
+const readExperimentDataObject = async function (
+  experimentId,
+  name,
+  dataType,
+  { bodyType = "text" } = {}
+) {
+  if (dataType !== "input" && dataType !== "output") {
+    throw new Error("dataType should be one of 'input' or 'output'");
+  }
+  const experiment = await loadExperiment(experimentId);
+  const dataObjectsField =
+    dataType === "input" ? "experimentInputs" : "experimentOutputs";
+  const dataObject = experiment[dataObjectsField].find(
+    (dataObj) => dataObj.name === name
+  );
+  if (dataObject.value && dataObject.type.isFileValueType) {
+    const downloads = dataObject.value
+      .split(",")
+      .map((dp) => readDataProduct(dp, { bodyType }));
+    if (downloads.length === 1) {
+      return await downloads[0];
+    } else {
+      return await Promise.all(downloads);
+    }
+  }
+  return null;
+};
+
+const readInputFile = async function (
+  experimentId,
+  inputName,
+  { bodyType = "text" } = {}
+) {
+  return await readExperimentDataObject(experimentId, inputName, "input", {
+    bodyType,
+  });
+};
+
+const readOutputFile = async function (
+  experimentId,
+  outputName,
+  { bodyType = "text" } = {}
+) {
+  return await readExperimentDataObject(experimentId, outputName, "output", {
+    bodyType,
+  });
+};
+
+export { createExperiment, readInputFile, readOutputFile };
 
 export default {
   createExperiment,
+  readInputFile,
+  readOutputFile,
 };

Reply via email to