Skip to content

Commit f234429

Browse files
committed
fix: link requests with divaservices-utils
1 parent 135d324 commit f234429

File tree

10 files changed

+45
-103
lines changed

10 files changed

+45
-103
lines changed

src/config.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
// require('dotenv').config(); // this loads the defined variables from .env
2-
3-
export const {
4-
HOST = "/",
5-
WEBSERVICES_XML_FILEPATH = "api/services.xml",
6-
SERVICES_API = "",
7-
COLLECTIONS_API_ENDPOINT = "api/collections",
8-
WORKFLOWS_API_ENDPOINT = "workflows",
9-
WORKFLOWS_EXECUTION_ENDPOINT = "exec",
10-
BASE_URL = "http://diufvm17.unifr.ch:8080/exist/projects/diae"
11-
} = process.env;
12-
13-
export const WORKFLOWS_EXECUTION = `${BASE_URL}/${WORKFLOWS_EXECUTION_ENDPOINT}`;
14-
export const COLLECTIONS_API = `${BASE_URL}/${COLLECTIONS_API_ENDPOINT}`;
15-
export const WORKFLOWS_API = `${BASE_URL}/${WORKFLOWS_API_ENDPOINT}`;
1+
export const { WEBSERVICES_XML_FILEPATH = "api/services.xml" } = process.env;

src/js/api/requests.js

+19-56
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,26 @@
1-
import fetch from "node-fetch";
2-
import { app } from "../app";
3-
import { Decorators } from "divaservices-utils";
4-
import {
5-
SERVICES_API,
6-
WEBSERVICES_XML_FILEPATH,
7-
COLLECTIONS_API,
8-
WORKFLOWS_API
9-
} from "../../config";
1+
import { Decorators, API } from "divaservices-utils";
2+
import { WEBSERVICES_XML_FILEPATH } from "../../config";
3+
import { webservices } from "../constants/globals";
104

11-
export const getServicesAPI = async () => {
12-
let xml;
5+
export const getServices = async () => {
136
if (process.env.NODE_ENV === "production") {
14-
const xmlApi = await fetch(SERVICES_API);
15-
xml = await xmlApi.text();
7+
return await API.getServices();
168
} else {
179
const filepath = WEBSERVICES_XML_FILEPATH;
18-
xml = (await import(`!!raw-loader!../../${filepath}`)).default;
10+
const xml = (await import(`!!raw-loader!../../${filepath}`)).default;
11+
return await Decorators.webservicesDecorator(xml);
1912
}
20-
return xml;
2113
};
2214

23-
export const sendWorkflowSteps = async (xml, installation = false) => {
24-
// const xhr = new XMLHttpRequest();
25-
// xhr.open(
26-
// "POST",
27-
// "http://diufvm17.unifr.ch:8080/exist/projects/diae/api/workflow/save?id=" +
28-
// app.workflowId,
29-
// true
30-
// );
31-
// xhr.setRequestHeader("Content-Type", "text/xml");
32-
// xhr.setRequestHeader("username", USERNAME);
33-
// xhr.setRequestHeader("password", PASSWORD);
34-
// xhr.send(xml);
35-
36-
const install = installation ? "install=true&" : "";
37-
38-
return await fetch(`${WORKFLOWS_API}/save?${install}id=${app.workflowId}`, {
39-
method: "POST",
40-
body: xml,
41-
headers: {
42-
"Content-Type": "text/xml"
43-
}
44-
});
45-
};
46-
47-
export const openWorkflowFromId = async id => {
48-
let xml;
15+
export const getWorkflowById = async (id, asXml = false) => {
4916
if (process.env.NODE_ENV === "production") {
50-
const workflow = await fetch(`${WORKFLOWS_API}?id=${id}`, {
51-
headers: {
52-
"Content-Type": "text/xml"
53-
}
54-
});
55-
xml = await workflow.text();
17+
if (asXml) {
18+
return await API.getWorkflowByIdJSON(id);
19+
} else {
20+
return await API.getWorkflowById(id);
21+
}
5622
} else {
57-
xml = `<Workflow>
23+
const xml = `<Workflow>
5824
<Id>116</Id>
5925
<Information>
6026
<Name>new rofklow</Name>
@@ -94,19 +60,17 @@ export const openWorkflowFromId = async id => {
9460
</Step>
9561
</Steps>
9662
</Workflow>`;
63+
return asXml ? xml : await Decorators.workflowDecorator(xml, webservices);
9764
}
98-
return xml;
9965
};
10066

101-
export const getCollectionsAPI = async () => {
102-
let xml;
67+
export const getCollections = async () => {
10368
if (process.env.NODE_ENV === "production") {
104-
const xmlApi = await fetch(COLLECTIONS_API);
105-
xml = await xmlApi.text();
69+
return await API.getCollections();
10670
} else {
10771
// const filepath = "collections.xml";
10872
// xml = (await import(`raw-loader!./${filepath}`)).default;
109-
xml = `<Collections>
73+
const xml = `<Collections>
11074
<Collection>
11175
<Id>134</Id>
11276
<Url>http://134.21.72.190:8080/collections/qwertz</Url>
@@ -335,7 +299,6 @@ export const getCollectionsAPI = async () => {
335299
</File>
336300
</Files>
337301
</Collection></Collections>`;
302+
return await Decorators.collectionsDecorator(xml);
338303
}
339-
340-
return await Decorators.collectionsDecorator(xml);
341304
};

src/js/app.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { mapActions, mapState } from "vuex";
44
import plugins from "./plugins";
55
import components from "./layout/components";
66
import store from "./store/store";
7-
import { Constants } from "divaservices-utils";
7+
import { Constants, DivaServices } from "divaservices-utils";
88
const { Types } = Constants;
99
import { initWebservices } from "./constants/globals";
1010
import { initPaperEvents } from "./events/paperEvents";
@@ -269,8 +269,7 @@ export let app;
269269

270270
if (process.env.NODE_ENV === "production") {
271271
// check id, if there is no id, go back to workflows
272-
const url_string = window.location.href.split("id=")[1];
273-
const id = parseInt(url_string);
272+
const id = DivaServices.getUrlParameters().id;
274273
if (!isNaN(id)) {
275274
this.workflowId = id;
276275
readWorkflow(id);

src/js/constants/globals.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import { Decorators } from "divaservices-utils";
2-
import { getServicesAPI } from "../api/requests";
1+
import { getServices } from "../api/requests";
32
import { DATATEST_TYPE } from "./constants";
43

54
export let webservices;
65
export let dataInputs;
76

8-
const _initWebservices = async () => {
9-
const xml = await getServicesAPI();
10-
webservices = await Decorators.webservicesDecorator(xml);
11-
};
12-
137
const _initDataInputs = async () => {
148
dataInputs = [
159
...new Set( // remove duplicates
@@ -32,7 +26,7 @@ const _initDataInputs = async () => {
3226

3327
// init webservices from xml file
3428
export const initWebservices = async () => {
35-
await _initWebservices();
29+
webservices = await getServices();
3630
await _initDataInputs();
3731
};
3832

src/js/layout/components/Collections.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from "vue";
22
import * as $ from "jquery";
33
import draggable from "vuedraggable";
4-
import { getCollectionsAPI } from "../../api/requests";
4+
import { getCollections } from "../../api/requests";
55
import { app } from "../../app";
66
import { updateImgPreview } from "../../elements/addDataElement";
77
import { DRAGGABLE_GROUP_NAME } from "../../constants/constants";
@@ -88,7 +88,7 @@ const Collections = Vue.component("Collections", {
8888
}
8989
},
9090
async mounted() {
91-
this.collections = await getCollectionsAPI();
91+
this.collections = await getCollections();
9292
},
9393
template: `
9494
<div id="collections" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

src/js/layout/components/toolsbar.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getElementByBoxId } from "../utils";
1313
import { saveWorkflow } from "../../workflows/saveWorkflow";
1414
import { fireAlert } from "../../utils/alerts";
1515
import { MESSAGE_SAVE_SUCCESS } from "../../constants/messages";
16-
import { WORKFLOWS_API, WORKFLOWS_EXECUTION_ENDPOINT } from "../../../config";
16+
import { API } from "divaservices-utils";
1717

1818
const Toolsbar = Vue.component("Toolsbar", {
1919
props: ["selectedElements", "paper", "scale"],
@@ -163,7 +163,9 @@ const Toolsbar = Vue.component("Toolsbar", {
163163
"save and install": {
164164
action: async () => {
165165
await saveWorkflow(app.graph.toJSON(), true);
166-
top.window.location.href = `${WORKFLOWS_API}/${app.workflowId}/${WORKFLOWS_EXECUTION_ENDPOINT}`;
166+
top.window.location.href = API.getExecutionViewUrl(
167+
app.workflowId
168+
);
167169
},
168170
icon: "fas fa-download",
169171
enabledCondition: true

src/js/layout/inputs.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const $ = jQuery;
44
global.$ = global.jQuery = $;
55

66
import "select2js";
7-
import { BASE_URL } from "../../config";
87
import {
98
TOOLTIP_BREAK_LINE,
109
PARAM_COL,
@@ -29,7 +28,7 @@ import { objectToString } from "./utils";
2928
import { layoutSettingsApp } from "../layoutSettings";
3029
import { app } from "../app";
3130
import { elementOnChangePosition } from "../events/paperEvents";
32-
import { Validation, Constants } from "divaservices-utils";
31+
import { Validation, Constants, API } from "divaservices-utils";
3332
const { Types } = Constants;
3433

3534
export const setSelectValueInElement = (boxId, parameters) => {
@@ -424,10 +423,7 @@ export const setParametersInForeignObject = (element, defaultParams = {}) => {
424423
"data-placement": "right"
425424
})
426425
.on("click", function() {
427-
const win = window.open(
428-
`${BASE_URL}services/${serviceId}/view`,
429-
"_blank"
430-
);
426+
const win = window.open(API.getServiceViewUrl(serviceId), "_blank");
431427
win.focus();
432428
})
433429
.appendTo(foreignObject.find(`.${TITLE_ROW_CLASS}`));

src/js/workflows/readWorkflow.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {
1111
} from "../elements/addElement";
1212
import { app } from "../app";
1313
import { isParamInput, generateUniqueId } from "../layout/utils";
14-
import { openWorkflowFromId } from "../api/requests";
14+
import { getWorkflowById } from "../api/requests";
1515
import { buildDataElement } from "../elements/addDataElement";
16+
import { API } from "divaservices-utils";
1617

1718
export const readWorkflow = async id => {
18-
const xml = await openWorkflowFromId(id);
19-
19+
const xml = await getWorkflowById(id, true);
2020
const elements = [];
2121
const links = [];
2222
const linksTmp = [];
@@ -79,11 +79,11 @@ export const readWorkflow = async id => {
7979
// add input box
8080
const ref = generateUniqueId();
8181
const mimetype = mime.lookup(Path[0]);
82-
const [collectionName, filename] = Path[0].split("/");
82+
const identifier = Path[0];
8383
const dataEl = buildDataElement(mimetype, [
8484
{
85-
identifier: Path[0],
86-
url: `http://134.21.72.190:8080/files/${collectionName}/original/${filename}`,
85+
identifier,
86+
url: API.buildFileUrlFromIdentifier(identifier),
8787
options: {
8888
"mime-type": mimetype
8989
}

src/js/workflows/saveWorkflow.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import xml2js from "xml2js";
66
import { getWebserviceByName } from "../constants/globals";
77
import { app } from "../app";
88
import { CATEGORY_DATATEST, CATEGORY_SERVICE } from "../constants/constants";
9-
import { Validation, XMLBuilders, DivaServices } from "divaservices-utils";
10-
import { sendWorkflowSteps } from "../api/requests";
9+
import { Validation, XMLBuilders, DivaServices, API } from "divaservices-utils";
1110
import { getOrderedElements } from "../elements/orderElement";
1211

1312
// we use the actual graph nodes to get the workflow
@@ -47,7 +46,9 @@ export const saveWorkflow = async (jsonGraph, installation = false) => {
4746
Name: paramName,
4847
Value
4948
});
50-
_inputs.parameters[paramName] = JSON.parse(Value);
49+
_inputs.parameters[paramName] = DivaServices.parseParameterValue(
50+
Value
51+
);
5152
}
5253
const validity = Validation.checkValue(Value, paramType, values);
5354
if (!validity) {
@@ -182,7 +183,7 @@ export const saveWorkflow = async (jsonGraph, installation = false) => {
182183
// var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
183184
// saveAs(blob, "../../tmp/hello.xml");
184185

185-
await sendWorkflowSteps(xml, installation);
186+
await API.saveWorkflow(xml, app.workflowId, installation);
186187

187188
return request;
188189
};

yarn.lock

+2-1
Original file line numberDiff line numberDiff line change
@@ -2747,9 +2747,10 @@ diffie-hellman@^5.0.0:
27472747

27482748
"divaservices-utils@https://github.com/pyphilia/divaservices-utils":
27492749
version "1.0.0"
2750-
resolved "https://github.com/pyphilia/divaservices-utils#3292d674445897c3911ab88fe2b7309940910d16"
2750+
resolved "https://github.com/pyphilia/divaservices-utils#3414747eb12fa0befbc24a6545c5d3d3f317c1cb"
27512751
dependencies:
27522752
eslint "^6.6.0"
2753+
node-fetch "^2.6.0"
27532754
prettier "^1.19.1"
27542755
xml2js "^0.4.22"
27552756

0 commit comments

Comments
 (0)