I'm using UiPath Orchestrator. This runs as expected. But I now additionally want to reduce the authentication to a single call (instead of always do an auth when requesting an odata
). So my idea was to write the object to a file and on the odata
request read that object and re-use it.
The following orchestrator
object comes from the constructor of new Orchestrator
. This object is ready to be used and has the following structure (via console.log(orchestrator)
):
In my tool I need the object functions of odata
. So this works:
console.log(orchestrator['v2']['odata']);
I now want to save that object as file to be able to re-use it, so I did:
fs.writeFileSync('./data.json', orchestrator, 'utf-8')
But sadly I get the error:
Converting circular structure to JSON
That is intended as the node package is using a circulare structure. So my idea was to use the circular-json
package to fix that issue:
const {parse, stringify} = require('circular-json');
...
var savetofile = stringify(orchestrator);
...
var readfromfile = parse(savetofile);
...
console.log(readfromfile['v2']['odata']);
But sadly than readfromfile['v2']['odata']
is not available anymore. The reason is that stringify(orchestrator)
is already minifying too heavy:
So how I achieve that I am able to read the Orchestrator object from the file and being able to use the functions again? Or is it more useful to use a memory tool in my case?
The issue was not located in the Orchestrator object itself. So there is no need to do a single authentication.
My problem was that I put the res.send
outside of the callback. So it never waited for the actual finish of the REST api call.
This was the base code where it just took the static result of the first request, it never updated the results:
app.get('/jobs', function (req, res) {
...
var orchestrator = require('./authenticate');
var results = {};
var apiQuery= {};
orchestrator.get('/odata/Jobs', apiQuery, function (err, data) {
for (row in data) {
results[i] =
{
'id' : row.id,
...
};
}
});
return res.send({results});
});
The solution is to moving the res.send({results});
into the orchestrator.get
, then it properly overwrites the results as it waits correctly for the callback:
app.get('/jobs', function (req, res) {
...
var orchestrator = require('./authenticate');
var results = {};
var apiQuery= {};
orchestrator.get('/odata/Jobs', apiQuery, function (err, data) {
for (row in data) {
results[i] =
{
'id' : row.id,
...
};
}
return res.send({results});
});
});