Skip to content

Commit 9d20ff1

Browse files
authored
fix(insee): correction checkApiInsee 1110 (#965)
## Ticket(s) lié(s) Complément 1110 ## Description On change la méthode de check API
2 parents b714521 + 593c1b8 commit 9d20ff1

2 files changed

Lines changed: 15 additions & 21 deletions

File tree

packages/backend/src/controllers/siret/checkApiInsee.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const logger = require("../../utils/logger");
2-
const { getToken } = require("../../services/Insee");
2+
const { getInformations } = require("../../services/Insee");
33
const AppError = require("../../utils/error");
44

55
const log = logger(module.filename);
66

77
module.exports = async function checkApiInsee(_req, res, next) {
88
log.i("IN");
99
try {
10-
const data = await getToken();
10+
const data = await getInformations();
1111
if (data.etatService !== "UP") {
1212
throw new AppError("Le service INSEE n'est pas UP", {
1313
details: data,

packages/backend/src/services/Insee.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,40 @@ const log = logger(module.filename);
88
const NB_ELEMENTS_TO_GET = 1000;
99

1010
module.exports.checkTokenApiEntreprise = async () => {
11+
log.i("IN");
1112
try {
1213
const params = new URLSearchParams({ token: config.apiEntreprise.token });
1314
const url = `${config.apiEntreprise.url}/privileges?${params}`;
1415
const { data } = await axios.get(url);
16+
log.i("DONE");
1517
return data;
1618
} catch (error) {
19+
log.w("DONE with error");
1720
throw new Error(`API Entreprise error: ${error.message}`);
1821
}
1922
};
2023

21-
const getToken = async () => {
24+
const getInformations = async () => {
2225
log.i("IN");
2326
try {
2427
const { apiInsee } = config;
25-
const cle = `${apiInsee.CLIENT_ID}:${apiInsee.CLIENT_SECRET}`;
26-
const authHeader = `Basic ${Buffer.from(cle).toString("base64")}`;
27-
const token = await axios.post(
28-
`${apiInsee.URL}/token`,
29-
{},
30-
{
31-
headers: {
32-
Authorization: authHeader,
33-
"Content-type": "application/x-www-form-urlencoded",
34-
},
35-
params: {
36-
grant_type: "client_credentials",
37-
},
38-
},
28+
const { data } = await axios.get(
29+
`${apiInsee.URL}${apiInsee.URI}/informations`,
30+
{ headers: { "X-INSEE-Api-Key-Integration": `${apiInsee.TOKEN}` } },
3931
);
40-
return token.data.access_token;
32+
log.i("DONE");
33+
return data;
4134
} catch (error) {
42-
throw new Error(`API Entreprise error: ${error.message}`);
35+
log.w("DONE with error");
36+
throw new Error(`API Insee error: ${error.message}`);
4337
}
4438
};
4539

46-
module.exports.getToken = getToken;
40+
module.exports.getInformations = getInformations;
4741

4842
module.exports.getEtablissement = async (siret) => {
4943
const { apiInsee } = config;
50-
log.w("getEtablissement", { siret });
44+
log.i("getEtablissement", { siret });
5145
const dateDuJour = dayjs().format("YYYY-MM-DD");
5246
const { data } = await axios.get(
5347
`${apiInsee.URL}${apiInsee.URI}/siret/${siret}?date=${dateDuJour}`,

0 commit comments

Comments
 (0)