From d64e87f1894aad4dcc8050f45aedab356eacb242 Mon Sep 17 00:00:00 2001 From: Magnus-SmariSma <20734986-Magnus-SmariSma@users.noreply.replit.com> Date: Thu, 20 Mar 2025 22:42:04 +0000 Subject: [PATCH] Implement a new endpoint to check CITES API connectivity using a dedicated API call instead of token validation. Replit-Commit-Author: Agent Replit-Commit-Session-Id: e931b5ab-041b-42e7-baf1-50017869cef6 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/e19c6a51-7e4c-4bb8-a6a6-46dc00f0ec99/2b0086f8-9e7d-4ae1-adc4-d84a54cd21ff.jpg --- client/src/lib/api.ts | 9 ++----- server/routes.ts | 55 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/client/src/lib/api.ts b/client/src/lib/api.ts index 4e91fdf..d28791d 100644 --- a/client/src/lib/api.ts +++ b/client/src/lib/api.ts @@ -177,13 +177,8 @@ export const apiClient = { async checkCitesApiStatus(): Promise { try { - // We'll use the token endpoint to check if the CITES API is accessible - const token = await this.getToken(); - return { - success: true, - connected: !!token, - message: token ? "CITES API is connected" : "CITES API token not found" - }; + const response = await axios.get("/api/cites/status"); + return response.data; } catch (error: any) { return { success: false, diff --git a/server/routes.ts b/server/routes.ts index 16021e8..5b4a2aa 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -213,6 +213,56 @@ export async function registerRoutes(app: Express): Promise { } }); + // CITES API Status check endpoint + app.get("/api/cites/status", async (req: Request, res: Response) => { + try { + const activeToken = await storage.getActiveToken(); + + if (!activeToken || !activeToken.token) { + return res.status(401).json({ + success: false, + connected: false, + message: "CITES API token is not configured" + }); + } + + try { + // Make a request to the CITES API to verify the token + const response = await axios.get(`${CITES_BASE_URL}/taxon_concepts/search`, { + params: { + page: 1, + per_page: 1 + }, + headers: { + 'X-Authentication-Token': activeToken.token + } + }); + + res.json({ + success: true, + connected: true, + message: "CITES API is connected and responding" + }); + } catch (error) { + if (axios.isAxiosError(error) && error.response) { + return res.status(error.response.status).json({ + success: false, + connected: false, + message: "Failed to connect to CITES API or invalid token", + status: error.response.status + }); + } + throw error; + } + } catch (error) { + res.status(500).json({ + success: false, + connected: false, + message: "Error checking CITES API status" + }); + } + }); + // IUCN API Status check endpoint app.get("/api/iucn/status", async (req: Request, res: Response) => { try { @@ -220,13 +270,14 @@ export async function registerRoutes(app: Express): Promise { if (!apiKey) { return res.status(401).json({ success: false, + connected: false, message: "IUCN API key is not configured" }); } - // Use a region endpoint which is simpler and less prone to errors + // Use the version endpoint which is the simplest endpoint try { - const response = await axios.get(`${IUCN_BASE_URL}/region/list`, { + const response = await axios.get(`${IUCN_BASE_URL}/version`, { params: { token: apiKey } });