projects-get-load-test-scripts.js•2.73 kB
import { getAuthToken } from './auth-get-token.js';
/**
* Function to get scripts for a load test in a project from LoadRunner Cloud.
*
* @param {Object} args - Arguments for the load test scripts request.
* @param {string} args.projectId - The ID of the project.
* @param {string} args.loadTestId - The ID of the load test.
* @returns {Promise<Object>} - The list of scripts or an error message.
*/
const executeFunction = async ({ projectId, loadTestId }) => {
const baseUrl = process.env.LRC_BASE_URL;
const tenantId = process.env.LRC_TENANT_ID;
const token = await getAuthToken();
try {
// Construct the URL with query parameters
const url = new URL(`${baseUrl}/projects/${projectId}/load-tests/${loadTestId}/scripts`);
url.searchParams.append('TENANTID', tenantId);
// Set up headers for the request
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
// Perform the fetch request
const response = await fetch(url.toString(), {
method: 'GET',
headers
});
// Check if the response was successful
if (!response.ok) {
const text = await response.text();
try {
const errorData = JSON.parse(text);
throw new Error(JSON.stringify(errorData));
} catch (jsonErr) {
// Not JSON, log the raw text
console.error('Non-JSON error response:', text);
throw new Error(text);
}
}
// Parse and return the response data
const text = await response.text();
try {
const data = JSON.parse(text);
return data;
} catch (jsonErr) {
// Not JSON, log the raw text
console.error('Non-JSON success response:', text);
return { error: 'Received non-JSON response from API', raw: text };
}
} catch (error) {
console.error('Error retrieving load test scripts:', error);
return { error: 'An error occurred while retrieving load test scripts.' };
}
};
/**
* Tool configuration for retrieving scripts for a load test in a project from LoadRunner Cloud.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'projects_getLoadTestScripts',
description: 'Retrieve scripts for a load test in a project from LoadRunner Cloud.',
parameters: {
type: 'object',
properties: {
projectId: {
type: 'string',
description: 'The ID of the project.'
},
loadTestId: {
type: 'string',
description: 'The ID of the load test.'
}
},
required: ['projectId', 'loadTestId']
}
}
}
};
export { apiTool };