Skip to content

Commit 912c32f

Browse files
committed
fix
1 parent 99b5d75 commit 912c32f

File tree

1 file changed

+21
-78
lines changed

1 file changed

+21
-78
lines changed

src/jobs/sirene/api.js

Lines changed: 21 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -26,89 +26,32 @@ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
2626
* @returns {Promise} Page data, next cursor and total count
2727
*/
2828
const fetchPage = async (endpoint, params, cursor) => {
29-
try {
30-
// Log request details
31-
console.log('=== SIRENE API REQUEST DETAILS ===');
32-
console.log('Endpoint:', endpoint);
33-
console.log('Parameters:', JSON.stringify(params, null, 2));
34-
console.log('Cursor:', cursor);
35-
36-
// Log API configuration
37-
console.log('=== API CONFIGURATION ===');
38-
console.log('API URL:', apiUrl);
39-
// Log first and last 4 chars of API key if exists
40-
if (apiKey) {
41-
console.log('API Key Length:', apiKey?.length);
42-
console.log('API Key Preview:', `${apiKey.substring(0, 4)}...${apiKey.substring(apiKey.length - 4)}`);
43-
}
44-
45-
const urlParams = new URLSearchParams({
46-
...params,
47-
curseur: cursor,
48-
nombre: SIZE_LIMIT
49-
});
50-
51-
const url = `${apiUrl}/${endpoint}?${urlParams}`;
52-
console.log('=== REQUEST URL ===');
53-
console.log('Full URL:', url);
29+
const urlParams = new URLSearchParams({
30+
...params,
31+
curseur: cursor,
32+
nombre: SIZE_LIMIT
33+
});
5434

55-
console.log('=== REQUEST HEADERS ===');
56-
console.log('Headers:', JSON.stringify(API_KEY_HEADER, null, 2));
35+
const url = `${apiUrl}/${endpoint}?${urlParams}`;
5736

58-
// Make the request
59-
console.log('=== MAKING REQUEST ===');
60-
const response = await fetch(url, { headers: API_KEY_HEADER });
61-
62-
// Log response details
63-
console.log('=== RESPONSE DETAILS ===');
64-
console.log('Status:', response.status);
65-
console.log('Status Text:', response.statusText);
66-
console.log('Response Headers:', JSON.stringify(Object.fromEntries([...response.headers]), null, 2));
67-
68-
await sleep(RATE_LIMIT_DELAY);
69-
70-
if (response.status === 404) {
71-
console.log('404 Not Found - Returning empty result');
72-
return { data: [], nextCursor: null, total: 0 };
73-
}
37+
const response = await fetch(url, { headers: API_KEY_HEADER });
38+
await sleep(RATE_LIMIT_DELAY);
7439

75-
if (response.status === 401) {
76-
console.error('401 Unauthorized - Authentication failed');
77-
throw new Error('Authentication failed - Please check API credentials');
78-
}
79-
80-
if (response.status === 200) {
81-
const json = await response.json();
82-
console.log('=== RESPONSE BODY PREVIEW ===');
83-
console.log('Header:', JSON.stringify(json.header, null, 2));
84-
console.log(`${API_CONFIG[endpoint]} Count:`, json?.[API_CONFIG[endpoint]]?.length);
85-
86-
const data = json?.[API_CONFIG[endpoint]] ?? [];
87-
return {
88-
data,
89-
nextCursor: json.header.curseurSuivant,
90-
total: json.header.total
91-
};
92-
}
93-
94-
// If we get here, it's an unexpected status
95-
console.error('=== UNEXPECTED RESPONSE ===');
96-
console.error('Response:', JSON.stringify(response, null, 2));
97-
const responseBody = await response.json().catch(async () => {
98-
console.error('Failed to parse JSON response, trying text');
99-
return await response.text();
100-
});
101-
console.error('Response body:', responseBody);
102-
103-
throw new Error(`Invalid API response status ${response.status}`);
40+
if (response.status === 404) {
41+
console.log('404 Not Found - Returning empty result');
42+
return { data: [], nextCursor: null, total: 0 };
43+
}
10444

105-
} catch (error) {
106-
console.error('=== ERROR IN FETCH PAGE ===');
107-
console.error('Error Type:', error.constructor.name);
108-
console.error('Error Message:', error.message);
109-
console.error('Error Stack:', error.stack);
110-
throw error;
45+
if (response.status === 200) {
46+
const json = await response.json();
47+
const data = json?.[API_CONFIG[endpoint]] ?? [];
48+
return {
49+
data,
50+
nextCursor: json.header.curseurSuivant,
51+
total: json.header.total
52+
};
11153
}
54+
throw new Error(`Invalid API response status ${response.status}`);
11255
};
11356

11457

0 commit comments

Comments
 (0)