Fixes the calls to agent with JWT

main
Yasen Pramatarov 2024-10-02 14:04:45 +03:00
parent 8bb2e8c838
commit 4e827e62f0
2 changed files with 9 additions and 8 deletions

View File

@ -21,13 +21,12 @@
$jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']);
?>
<?php if (isset($_SESSION["{$agent['id']}_cache"])) { ?>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="load recently cached data" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>')">load cache</button>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get fresh data from agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="load recently cached data" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>')">load cache</button>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get fresh data from agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
<?php } else { ?>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get data from the agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>')">fetch data</button>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get data from the agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>')">fetch data</button>
<button class="btn btn-light" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="no cache to refresh">force refresh</button>
<?php } ?>
</p>
<p>Result:</p>
<pre id="result<?= htmlspecialchars($agent['id']) ?>">click a button to fetch data from the agent.</pre>
<?php } ?>

View File

@ -16,8 +16,8 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
var xhr = new XMLHttpRequest();
const agentUrl = url + endpoint;
// FIXME for debugging purpose
console.log("Requesting URL:", agentUrl);
// DEBUG show the requested URL for debugging purpose
//console.log("Requesting URL:", agentUrl);
// Handle invalid URL error
try {
@ -29,9 +29,8 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
}
// send the token
xhr.setRequestHeader("Authorization", "Bearer " + jwtToken);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Authorization", "Bearer " + jwtToken);
// Set a timeout in milliseconds (10 seconds = 10000 ms)
xhr.timeout = 10000;
@ -46,6 +45,9 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
try {
// Parse and display the result
let result = JSON.parse(xhr.responseText);
// DEBUG display the result
//console.log(xhr.responseText);
if (result.error) {
resultElement.innerHTML = "Error: " + result.error;
} else {