/**
* Function to list the clients that have used a specified network in the given timespan.
*
* @param {Object} args - Arguments for the client listing.
* @param {string} args.networkId - The ID of the network to list clients for.
* @param {number} [args.timespan=86400] - The timespan for which the information will be fetched, in seconds (default is 1 day).
* @param {number} [args.perPage=10] - The number of entries per page returned (default is 10).
* @param {string} [args.startingAfter] - A token used by the server to indicate the start of the page.
* @param {string} [args.endingBefore] - A token used by the server to indicate the end of the page.
* @param {string} [args.statuses] - Filters clients based on status ('Online' or 'Offline').
* @param {string} [args.ip] - Filters clients based on a partial or full match for the IP address.
* @param {string} [args.mac] - Filters clients based on a partial or full match for the MAC address.
* @returns {Promise<Array>} - The list of clients for the specified network.
*/
const executeFunction = async ({ networkId, timespan = 86400, perPage = 10, startingAfter, endingBefore, statuses, ip, mac }) => {
const baseUrl = 'https://api.meraki.com/api/v1';
const token = process.env.CISCO_MERAKI_S_PUBLIC_WORKSPACE_API_KEY;
try {
// Construct the URL with query parameters
const url = new URL(`${baseUrl}/networks/${networkId}/clients`);
if (timespan) url.searchParams.append('timespan', timespan.toString());
if (perPage) url.searchParams.append('perPage', perPage.toString());
if (startingAfter) url.searchParams.append('startingAfter', startingAfter);
if (endingBefore) url.searchParams.append('endingBefore', endingBefore);
if (statuses) url.searchParams.append('statuses', statuses);
if (ip) url.searchParams.append('ip', ip);
if (mac) url.searchParams.append('mac', mac);
// 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 network clients:', error);
return { error: 'An error occurred while fetching network clients.' };
}
};
/**
* Tool configuration for listing clients on a specified network.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'getNetworkClients',
description: 'List the clients that have used this network in the timespan.',
parameters: {
type: 'object',
properties: {
networkId: {
type: 'string',
description: 'The ID of the network to list clients for.'
},
timespan: {
type: 'integer',
description: 'The timespan for which the information will be fetched, in seconds.'
},
perPage: {
type: 'integer',
description: 'The number of entries per page returned.'
},
startingAfter: {
type: 'string',
description: 'A token used by the server to indicate the start of the page.'
},
endingBefore: {
type: 'string',
description: 'A token used by the server to indicate the end of the page.'
},
statuses: {
type: 'string',
description: 'Filters clients based on status.'
},
ip: {
type: 'string',
description: 'Filters clients based on a partial or full match for the IP address.'
},
mac: {
type: 'string',
description: 'Filters clients based on a partial or full match for the MAC address.'
}
},
required: ['networkId']
}
}
}
};
export { apiTool };