/**
* Function to get the clients of a device from the Meraki Dashboard API.
*
* @param {Object} args - Arguments for the request.
* @param {string} args.serial - The serial number of the device.
* @param {number} [args.timespan=86400] - The timespan for which the information will be fetched, in seconds (default is 1 day).
* @returns {Promise<Object>} - The result of the device clients request.
*/
const executeFunction = async ({ serial, timespan = 86400 }) => {
const baseUrl = 'https://api.meraki.com/api/v1';
const token = process.env.CISCO_MERAKI_S_PUBLIC_WORKSPACE_API_KEY;
try {
// Construct the URL with path variable
const url = new URL(`${baseUrl}/devices/${serial}/clients`);
if (timespan) {
url.searchParams.append('timespan', timespan.toString());
}
// Set up headers for the request
const headers = {
'Accept': '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 errorData = await response.json();
throw new Error(errorData);
}
// Parse and return the response data
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching device clients:', error);
return { error: 'An error occurred while fetching device clients.' };
}
};
/**
* Tool configuration for getting device clients from the Meraki Dashboard API.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'getDeviceClients',
description: 'List the clients of a device.',
parameters: {
type: 'object',
properties: {
serial: {
type: 'string',
description: 'The serial number of the device.'
},
timespan: {
type: 'integer',
description: 'The timespan for which the information will be fetched, in seconds.'
}
},
required: ['serial']
}
}
}
};
export { apiTool };