jsonplaceholder_health_test
Verify API connectivity and measure response times for specified JSONPlaceholder endpoints like posts, users, and comments to ensure system health and performance.
Instructions
Test API connectivity and response times using JSONPlaceholder
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endpoints | No | Endpoints to test: posts, users, comments, albums, photos, todos |
Implementation Reference
- The main handler function that performs API health checks on specified JSONPlaceholder endpoints, measures response times using the JSONPlaceholderAPIClient, aggregates results, and returns a comprehensive test summary.execute: async (args: any) => { try { const endpoints = args.endpoints || ['posts', 'users', 'comments']; const testResults: any = { timestamp: new Date().toISOString(), endpoints_tested: endpoints, results: {}, summary: { total_tests: endpoints.length, successful: 0, failed: 0, average_response_time: 0 } }; let totalResponseTime = 0; for (const endpoint of endpoints) { const startTime = Date.now(); try { let data; switch (endpoint) { case 'posts': data = await client.getPosts(3); break; case 'users': data = await client.getUsers(3); break; case 'comments': data = await client.getComments(undefined, 3); break; case 'albums': data = await client.getAlbums(undefined, 3); break; case 'photos': data = await client.getPhotos(undefined, 3); break; case 'todos': data = await client.getTodos(undefined, 3); break; default: throw new Error(`Unknown endpoint: ${endpoint}`); } const responseTime = Date.now() - startTime; totalResponseTime += responseTime; testResults.results[endpoint] = { status: 'success', response_time: responseTime, data_count: Array.isArray(data) ? data.length : 1 }; testResults.summary.successful++; } catch (error) { const responseTime = Date.now() - startTime; testResults.results[endpoint] = { status: 'failed', response_time: responseTime, error: error instanceof Error ? error.message : 'Unknown error' }; testResults.summary.failed++; } } testResults.summary.average_response_time = Math.round(totalResponseTime / endpoints.length); return { success: true, data: { source: 'JSONPlaceholder API', health_test: testResults, timestamp: Date.now(), apiUsed: true } }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to run health test' }; } }
- src/tools/testing/jsonplaceholder-tools.ts:372-472 (registration)Registers the 'jsonplaceholder_health_test' tool with the ToolRegistry, defining its metadata, input schema, and execute handler.registry.registerTool({ name: 'jsonplaceholder_health_test', description: 'Test API connectivity and response times using JSONPlaceholder', category: 'testing', source: 'jsonplaceholder.typicode.com', inputSchema: { type: 'object', properties: { endpoints: { type: 'array', items: { type: 'string' }, description: 'Endpoints to test: posts, users, comments, albums, photos, todos', default: ['posts', 'users', 'comments'] } }, required: [] }, execute: async (args: any) => { try { const endpoints = args.endpoints || ['posts', 'users', 'comments']; const testResults: any = { timestamp: new Date().toISOString(), endpoints_tested: endpoints, results: {}, summary: { total_tests: endpoints.length, successful: 0, failed: 0, average_response_time: 0 } }; let totalResponseTime = 0; for (const endpoint of endpoints) { const startTime = Date.now(); try { let data; switch (endpoint) { case 'posts': data = await client.getPosts(3); break; case 'users': data = await client.getUsers(3); break; case 'comments': data = await client.getComments(undefined, 3); break; case 'albums': data = await client.getAlbums(undefined, 3); break; case 'photos': data = await client.getPhotos(undefined, 3); break; case 'todos': data = await client.getTodos(undefined, 3); break; default: throw new Error(`Unknown endpoint: ${endpoint}`); } const responseTime = Date.now() - startTime; totalResponseTime += responseTime; testResults.results[endpoint] = { status: 'success', response_time: responseTime, data_count: Array.isArray(data) ? data.length : 1 }; testResults.summary.successful++; } catch (error) { const responseTime = Date.now() - startTime; testResults.results[endpoint] = { status: 'failed', response_time: responseTime, error: error instanceof Error ? error.message : 'Unknown error' }; testResults.summary.failed++; } } testResults.summary.average_response_time = Math.round(totalResponseTime / endpoints.length); return { success: true, data: { source: 'JSONPlaceholder API', health_test: testResults, timestamp: Date.now(), apiUsed: true } }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Failed to run health test' }; } } });
- src/utils/input-validator.ts:281-283 (schema)Zod-based input schema for validating the 'endpoints' parameter of the jsonplaceholder_health_test tool.'jsonplaceholder_health_test': z.object({ endpoints: z.array(z.enum(['posts', 'users', 'comments', 'albums', 'photos', 'todos'])).optional() }),
- Helper class JSONPlaceholderAPIClient that provides methods for fetching data from various JSONPlaceholder endpoints, used by the health test handler to perform actual API calls.class JSONPlaceholderAPIClient { private baseURL = 'https://jsonplaceholder.typicode.com'; async makeRequest(endpoint: string, params: Record<string, any> = {}) { try { const response = await axios.get(`${this.baseURL}${endpoint}`, { params, timeout: 10000 }); return response.data; } catch (error) {throw error; } } async getPosts(limit?: number) { const posts = await this.makeRequest('/posts'); return limit ? posts.slice(0, limit) : posts; } async getUsers(limit?: number) { const users = await this.makeRequest('/users'); return limit ? users.slice(0, limit) : users; } async getComments(postId?: number, limit?: number) { const endpoint = postId ? `/posts/${postId}/comments` : '/comments'; const comments = await this.makeRequest(endpoint); return limit ? comments.slice(0, limit) : comments; } async getAlbums(userId?: number, limit?: number) { const endpoint = userId ? `/users/${userId}/albums` : '/albums'; const albums = await this.makeRequest(endpoint); return limit ? albums.slice(0, limit) : albums; } async getPhotos(albumId?: number, limit?: number) { const endpoint = albumId ? `/albums/${albumId}/photos` : '/photos'; const photos = await this.makeRequest(endpoint); return limit ? photos.slice(0, limit) : photos; } async getTodos(userId?: number, limit?: number) { const endpoint = userId ? `/users/${userId}/todos` : '/todos'; const todos = await this.makeRequest(endpoint); return limit ? todos.slice(0, limit) : todos; } }