Skip to content

Commit

Permalink
Merge pull request #5 from felix-reck/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
felix-reck authored Oct 15, 2024
2 parents 9a2794f + 48576a4 commit d9ae33b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
11 changes: 5 additions & 6 deletions lib/body-creator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function workplaceBody(workplace,state,timestamp){
function workplaceBody(workplace,state,timestamp, format){

let ts = timestamp ?? new Date(); //either provide date or current date is being used

Expand All @@ -20,14 +20,13 @@ function workplaceBody(workplace,state,timestamp){
"operator": "EQUAL"
}
],
"columns": [],
"returnAsObject": false
"columns": []
}

return body;
return { ...body, returnAsObject: format === 'returnAsObject' };;
}

function customBody(body){
function customBody(body,format){

if (typeof body == 'string') {
try {
Expand All @@ -36,7 +35,7 @@ function customBody(body){

}
}
return body;
return { ...body, returnAsObject: format === 'returnAsObject' };
}


Expand Down
17 changes: 7 additions & 10 deletions lib/mip-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ module.exports = {
},

performRequest: async function (node, requestOptions) {
node.status({
fill: 'blue',
shape: 'dot',
text: requestOptions.auth ? 'Requesting with credentials' : 'Requesting with cookie'
node.status({
fill: 'blue',
shape: 'dot',
text: requestOptions.auth ? 'Requesting with credentials' : 'Requesting with cookie'
});
try {
const response = await axios(requestOptions);
Expand All @@ -73,16 +73,13 @@ module.exports = {
const message = error.response.data[0].message || 'Unknown error';

node.status({ fill: 'red', shape: 'dot', text: `${statusText} ${status}` });
node.error(message);

return { message, status, statusText };
} else {
// Fehler, der nicht durch den Server zurückgegeben wurde
const message = error.message;

node.status({ fill: 'red', shape: 'dot', text: message });
node.error(message);

return message;
}
}
Expand All @@ -91,9 +88,9 @@ module.exports = {
// Response Handling
handleResponse: function (response, format, servicetype, accessId) {
let data = response.data;
if (format != 'original' && servicetype == 'data') {
data = responseHandler.formatData(response.data, format == 'modified');
}
if (format != 'original' && format!= 'returnAsObject' && servicetype === 'data') {
data = responseHandler.formatData(response.data, format === 'modified');
}

this.updateSessionCookie(response.headers, accessId); // Update the session cookie

Expand Down
13 changes: 7 additions & 6 deletions lib/mip-node.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
<label for="node-input-format"><i class="fa fa-table"></i> Output</label>
<select id="node-input-format">
<option value="original" selected>Raw Output</option>
<option value="returnAsObject" selected>Raw Output as Object</option>
<option value="restructured" >Restructured Output</option>
<option value="modified" >Transformed Output</option>
<option value="modified" >Transformed (recommended)</option>
</select>
</div>

Expand All @@ -82,8 +83,7 @@ <h3>Inputs</h3>
"operator": "EQUAL"
}
],
"columns": [],
"returnAsObject": false
"columns": []
}
</pre>
<dd>For the request body to be accepted, the method must be set to <strong>POST</strong>.</dd>
Expand All @@ -106,9 +106,10 @@ <h3>Details</h3>
<h3>Configuration</h3>
<p>The output can be configured to return:</p>
<ul>
<li><strong>Raw:</strong> As received from the API.</li>
<li><strong>Restructured:</strong> As an object with key-value pairs.</li>
<li><strong>Transformed:</strong> As an object with key-value pairs, but with dots in the keys replaced by underscores.</li>
<li><strong>Raw Output:</strong> The response exactly as received from the API.</li>
<li><strong>Raw Output as Object:</strong> The API response with the addition of <code>returnAsObject=true</code>.</li>
<li><strong>Restructured Output:</strong> The API response formatted as an object with key-value pairs for easier access.</li>
<li><strong>Transformed Output:</strong> The API response as an object with key-value pairs, where dots in the keys are replaced by underscores. This is the recommended setting, as it simplifies preprocessing of the data.</li>
</ul>

<h3>References</h3>
Expand Down
16 changes: 9 additions & 7 deletions lib/mip-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);
let node = this;

node.on('input', async function (msg) {
node.on('input', async function (msg, send, done) {
node.name = config.name || 'mip-node';

// Input parameters
Expand All @@ -18,8 +18,7 @@ module.exports = function (RED) {
const servicetype = config.servicetype;

// Create request body
let requestBody = bodyCreator.customBody(msg.payload);
requestBody = format == 'original' ? requestBody : { ...requestBody, returnAsObject: false };
const requestBody = bodyCreator.customBody(msg.payload, format);

// Create options for the request using the mip-client helper
const options = mipclient.createOptions({
Expand All @@ -41,11 +40,14 @@ module.exports = function (RED) {
if (response.status === 200) {
const result = mipclient.handleResponse(response, format, servicetype, server.accessId);
msg.payload = result.data;
msg.status= result.status;
msg.statusText= result.statusText;
node.send(msg);
msg.status = result.status;
msg.statusText = result.statusText;
send(msg);
done();
} else {


done(response.message)

//do nothing!

}
Expand Down
10 changes: 6 additions & 4 deletions lib/mip-workplace.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
<label for="node-input-format"><i class="fa fa-table"></i> Output</label>
<select id="node-input-format">
<option value="original" selected>Raw Output</option>
<option value="returnAsObject" selected>Raw Output as Object</option>
<option value="restructured" >Restructured Output</option>
<option value="modified" >Transformed Output</option>
<option value="modified" >Transformed (recommended)</option>
</select>
</div>

Expand Down Expand Up @@ -68,9 +69,10 @@ <h3>Outputs</h3>
<h3>Configuration</h3>
<p>The output can be configured to return:</p>
<ul>
<li><strong>Raw:</strong> As received from the API.</li>
<li><strong>Restructured:</strong> As an object with key-value pairs.</li>
<li><strong>Transformed:</strong> As an object with key-value pairs, but with dots in the keys replaced by underscores.</li>
<li><strong>Raw Output:</strong> The response exactly as received from the API.</li>
<li><strong>Raw Output as Object:</strong> The API response with the addition of <code>returnAsObject=true</code>.</li>
<li><strong>Restructured Output:</strong> The API response formatted as an object with key-value pairs for easier access.</li>
<li><strong>Transformed Output:</strong> The API response as an object with key-value pairs, where dots in the keys are replaced by underscores. This is the recommended setting, as it simplifies preprocessing of the data.</li>
</ul>

<h3>References</h3>
Expand Down
13 changes: 7 additions & 6 deletions lib/mip-workplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);
let node = this;

node.on('input', async function (msg) {
node.on('input', async function (msg, send, done) {
node.name = config.name || 'mip-workplace';

// Input parameters
Expand All @@ -17,7 +17,7 @@ module.exports = function (RED) {
const servicetype = 'data';

// Create request body
let requestBody = bodyCreator.workplaceBody(msg.topic, msg.payload, msg.timestamp);
let requestBody = bodyCreator.workplaceBody(msg.topic, msg.payload, msg.timestamp, format);

// Create options for the request using mip-client helper
const options = mipclient.createOptions({
Expand All @@ -39,11 +39,12 @@ module.exports = function (RED) {
if (response.status === 200) {
const result = mipclient.handleResponse(response, format, servicetype, server.accessId);
msg.payload = result.data;
msg.status= result.status;
msg.statusText= result.statusText;
node.send(msg);
msg.status = result.status;
msg.statusText = result.statusText;
send(msg);
done();
} else {

done(response.message)
//do nothing!

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@felix-reck/node-red-mip",
"version": "0.1.3",
"version": "0.2.0",
"description": "A custom Node-RED node to interact with MPDV MIP HydraX API",
"repository": {
"type": "git",
Expand Down

0 comments on commit d9ae33b

Please sign in to comment.