Refactor: Improve IUCN API token handling by prioritizing environment variables.
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/53d606c0-59ab-45f3-8ac7-7938548fd7ae.jpg
This commit is contained in:
@ -323,7 +323,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if the API is working by hitting the version endpoint
|
// Check if the API is working by hitting the version endpoint
|
||||||
const response = await axios.get("https://apiv4.iucnredlist.org/api/v4/version", {
|
const response = await axios.get("https://apiv4.iucnredlist.org/api/v4/information/api_version", {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${iucnToken}`
|
"Authorization": `Bearer ${iucnToken}`
|
||||||
}
|
}
|
||||||
@ -558,9 +558,16 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
const nameParts = String(name).split(' ');
|
const nameParts = String(name).split(' ');
|
||||||
const [genusName, speciesName] = nameParts;
|
const [genusName, speciesName] = nameParts;
|
||||||
|
|
||||||
// Get IUCN token
|
// Try to get IUCN token from environment variable first
|
||||||
const activeToken = await storage.getActiveToken();
|
let iucnToken = process.env.IUCN_API_KEY || null;
|
||||||
if (!activeToken?.iucnToken) {
|
|
||||||
|
// If not available in env, try to get from storage
|
||||||
|
if (!iucnToken) {
|
||||||
|
const activeToken = await storage.getActiveToken();
|
||||||
|
iucnToken = activeToken?.iucnToken || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!iucnToken) {
|
||||||
return res.status(401).json({
|
return res.status(401).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: "IUCN API v4 token is not configured. Please set your token in the API Token panel."
|
message: "IUCN API v4 token is not configured. Please set your token in the API Token panel."
|
||||||
@ -571,7 +578,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
// First, we need to find the species taxon ID using scientific name lookup
|
// First, we need to find the species taxon ID using scientific name lookup
|
||||||
const taxaResponse = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", {
|
const taxaResponse = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${activeToken.iucnToken}`
|
"Authorization": `Bearer ${iucnToken}`
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
genus_name: genusName,
|
genus_name: genusName,
|
||||||
@ -592,7 +599,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
// Now retrieve the habitats using the taxon ID
|
// Now retrieve the habitats using the taxon ID
|
||||||
const habitatsResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/habitats`, {
|
const habitatsResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/habitats`, {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${activeToken.iucnToken}`
|
"Authorization": `Bearer ${iucnToken}`
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
taxonid: taxonId
|
taxonid: taxonId
|
||||||
@ -649,9 +656,16 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
const nameParts = String(name).split(' ');
|
const nameParts = String(name).split(' ');
|
||||||
const [genusName, speciesName] = nameParts;
|
const [genusName, speciesName] = nameParts;
|
||||||
|
|
||||||
// Get IUCN token
|
// Try to get IUCN token from environment variable first
|
||||||
const activeToken = await storage.getActiveToken();
|
let iucnToken = process.env.IUCN_API_KEY || null;
|
||||||
if (!activeToken?.iucnToken) {
|
|
||||||
|
// If not available in env, try to get from storage
|
||||||
|
if (!iucnToken) {
|
||||||
|
const activeToken = await storage.getActiveToken();
|
||||||
|
iucnToken = activeToken?.iucnToken || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!iucnToken) {
|
||||||
return res.status(401).json({
|
return res.status(401).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: "IUCN API v4 token is not configured. Please set your token in the API Token panel."
|
message: "IUCN API v4 token is not configured. Please set your token in the API Token panel."
|
||||||
@ -662,7 +676,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
// First, we need to find the species taxon ID using scientific name lookup
|
// First, we need to find the species taxon ID using scientific name lookup
|
||||||
const taxaResponse = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", {
|
const taxaResponse = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${activeToken.iucnToken}`
|
"Authorization": `Bearer ${iucnToken}`
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
genus_name: genusName,
|
genus_name: genusName,
|
||||||
@ -683,7 +697,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
// Now retrieve the conservation measures using the taxon ID
|
// Now retrieve the conservation measures using the taxon ID
|
||||||
const measuresResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/measures`, {
|
const measuresResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/measures`, {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${activeToken.iucnToken}`
|
"Authorization": `Bearer ${iucnToken}`
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
taxonid: taxonId
|
taxonid: taxonId
|
||||||
|
Reference in New Issue
Block a user