Skip to content

Commit

Permalink
custom data type: plugin_user_access_token added
Browse files Browse the repository at this point in the history
This patch shows how to use the plugin_user_access_token inside the custom data type updater.

see #72130
  • Loading branch information
martinrode committed Apr 19, 2024
1 parent f80198c commit 101852d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
44 changes: 41 additions & 3 deletions customDataTypeUpdater/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,39 @@ Date.prototype.AddMinutes = function ( minutes ) {
return this;
}

const http = require('http');

// Function to perform a GET request and return the body of the response
async function fetchUrl(url) {
return new Promise((resolve, reject) => {
http.get(url, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('Response status was ' + res.statusCode));
}
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
}).on('error', (err) => {
reject(err);
});
});
}

main = (payload) => {
switch (payload.action) {
case "start_update":
outputData({
"state": {
"personal": 2
},
"log": ["started logging"]
"log": [
"started logging",
"config.name.internal_name: "+config.system.config.name.internal_name
]
})
break
case "update":
Expand Down Expand Up @@ -75,21 +100,34 @@ outputErr = (err2) => {
process.exit(0);
}

let config

(() => {
run_main = () => {
run_main = async () => {
try {
let payload = JSON.parse(data)
// dv.send(payload)
// fs.writeFileSync('/tmp/post-in', data);

console.error("data has length", data.length)
console.error(payload)

config = JSON.parse(await fetchUrl(info.api_url+"/api/v1/config?access_token="+info.plugin_user_access_token));
// fs.writeFileSync('/tmp/config-load', JSON.stringify(config, "", " "))

main(payload)
} catch (error) {
console.error("caught error", error)
outputErr(error)
}
}

// dv.send(JSON.parse(process.argv[2]))
// info contains api_url, plugin_user, plugin_user_access_token
let info = JSON.parse(process.argv[2])

// fs.writeFileSync('/tmp/post-info', JSON.stringify(info, "", " "));

// dv.send(JSON.parse(process.argv))

let data = ""
process.stdin.setEncoding('utf8');
Expand Down
3 changes: 3 additions & 0 deletions manifest.master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ custom_types:
value:
type: bool
update:
# pass in a root session when calling the plugin updater
plugin_user:
reference: system:root
exec:
service: "node"
commands:
Expand Down

0 comments on commit 101852d

Please sign in to comment.