From 2e2f4706c51a9521a4d06f2b1930e95a1831875d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 5 Apr 2024 12:26:32 +0200 Subject: [PATCH] deduplicate and add error management --- app/services/api_entreprise_service.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index 5b0136c1d..188f6d23d 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -60,21 +60,25 @@ class APIEntrepriseService # See: https://entreprise.api.gouv.fr/developpeurs#surveillance-etat-fournisseurs def api_insee_up? - response = Typhoeus.get("https://entreprise.api.gouv.fr/ping/insee/sirene", timeout: 1) - if response.success? - JSON.parse(response.body).fetch('status') == 'ok' - else - false - end + api_up?("https://entreprise.api.gouv.fr/ping/insee/sirene") end def api_djepva_up? - response = Typhoeus.get("https://entreprise.api.gouv.fr/ping/djepva/api-association", timeout: 1) + api_up?("https://entreprise.api.gouv.fr/ping/djepva/api-association") + end + + private + + def api_up?(url) + response = Typhoeus.get(url, timeout: 1) if response.success? JSON.parse(response.body).fetch('status') == 'ok' else false end + rescue => e + Sentry.capture_exception(e) + false end end end