|
@@ -7,31 +7,22 @@ const router = express.Router();
|
|
|
const IRL_SERIES = '001515333';
|
|
const IRL_SERIES = '001515333';
|
|
|
|
|
|
|
|
async function fetchIrlFromInsee(year, quarter) {
|
|
async function fetchIrlFromInsee(year, quarter) {
|
|
|
- const token = process.env.INSEE_API_TOKEN;
|
|
|
|
|
- if (!token) return null;
|
|
|
|
|
-
|
|
|
|
|
const period = `${year}-Q${quarter}`;
|
|
const period = `${year}-Q${quarter}`;
|
|
|
const url = `https://api.insee.fr/series/BDM/V1/data/SERIES_BDM/${IRL_SERIES}?startPeriod=${period}&endPeriod=${period}`;
|
|
const url = `https://api.insee.fr/series/BDM/V1/data/SERIES_BDM/${IRL_SERIES}?startPeriod=${period}&endPeriod=${period}`;
|
|
|
|
|
|
|
|
const resp = await fetch(url, {
|
|
const resp = await fetch(url, {
|
|
|
- headers: {
|
|
|
|
|
- 'Authorization': `Bearer ${token}`,
|
|
|
|
|
- 'Accept': 'application/json',
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ headers: { 'Accept': 'application/xml' },
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- if (!resp.ok) throw new Error(`INSEE API ${resp.status}`);
|
|
|
|
|
|
|
+ if (!resp.ok) throw new Error(`INSEE API HTTP ${resp.status}`);
|
|
|
|
|
|
|
|
- const data = await resp.json();
|
|
|
|
|
|
|
+ const xml = await resp.text();
|
|
|
|
|
|
|
|
- // SDMX-JSON parsing
|
|
|
|
|
- const seriesKey = Object.keys(data.dataSets[0].series)[0];
|
|
|
|
|
- const observations = data.dataSets[0].series[seriesKey].observations;
|
|
|
|
|
- const obsKey = Object.keys(observations)[0];
|
|
|
|
|
- const value = observations[obsKey][0];
|
|
|
|
|
|
|
+ // SDMX-XML : extraire <generic:ObsValue value="142.06"/>
|
|
|
|
|
+ const match = xml.match(/ObsValue[^/]*value="([\d.]+)"/);
|
|
|
|
|
+ if (!match) throw new Error('Valeur IRL introuvable dans la réponse XML');
|
|
|
|
|
|
|
|
- if (value === undefined || value === null) throw new Error('Valeur IRL introuvable pour cette période');
|
|
|
|
|
- return parseFloat(value);
|
|
|
|
|
|
|
+ return parseFloat(match[1]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GET /api/irl?year=2024&quarter=2
|
|
// GET /api/irl?year=2024&quarter=2
|
|
@@ -39,16 +30,12 @@ router.get('/', authMiddleware, async (req, res) => {
|
|
|
const { year, quarter } = req.query;
|
|
const { year, quarter } = req.query;
|
|
|
if (!year || !quarter) return res.status(400).json({ error: 'year et quarter requis' });
|
|
if (!year || !quarter) return res.status(400).json({ error: 'year et quarter requis' });
|
|
|
|
|
|
|
|
- const configured = !!process.env.INSEE_API_TOKEN;
|
|
|
|
|
-
|
|
|
|
|
- if (!configured) return res.json({ value: null, configured: false });
|
|
|
|
|
-
|
|
|
|
|
try {
|
|
try {
|
|
|
const value = await fetchIrlFromInsee(parseInt(year), parseInt(quarter));
|
|
const value = await fetchIrlFromInsee(parseInt(year), parseInt(quarter));
|
|
|
- res.json({ value, configured: true, year: parseInt(year), quarter: parseInt(quarter) });
|
|
|
|
|
|
|
+ res.json({ value, year: parseInt(year), quarter: parseInt(quarter) });
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
console.error('IRL fetch error:', e.message);
|
|
console.error('IRL fetch error:', e.message);
|
|
|
- res.json({ value: null, configured: true, error: e.message });
|
|
|
|
|
|
|
+ res.status(502).json({ value: null, error: e.message });
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|