2020-08-05 18:40:47 +02:00
|
|
|
class APIEntreprise::API
|
2023-05-25 11:15:26 +02:00
|
|
|
ENTREPRISE_RESOURCE_NAME = "v3/insee/sirene/unites_legales/%{id}"
|
2023-05-30 16:27:20 +02:00
|
|
|
ETABLISSEMENT_RESOURCE_NAME = "v3/insee/sirene/etablissements/%{id}"
|
2023-05-25 14:46:34 +02:00
|
|
|
EXTRAIT_KBIS_NAME = "v3/infogreffe/rcs/unites_legales/%{id}/extrait_kbis"
|
2023-05-25 15:29:16 +02:00
|
|
|
TVA_NAME = "v3/european_commission/unites_legales/%{id}/numero_tva"
|
2023-05-29 16:45:21 +02:00
|
|
|
EXERCICES_RESOURCE_NAME = "v3/dgfip/etablissements/%{id}/chiffres_affaires"
|
2023-05-30 11:32:59 +02:00
|
|
|
RNA_RESOURCE_NAME = "v4/djepva/api-association/associations/open_data/%{id}"
|
2023-10-17 13:23:17 +02:00
|
|
|
EFFECTIFS_RESOURCE_NAME = "v3/gip_mds/etablissements/%{id}/effectifs_mensuels"
|
2023-10-17 13:26:21 +02:00
|
|
|
EFFECTIFS_ANNUELS_RESOURCE_NAME = "v3/gip_mds/unites_legales/%{id}/effectifs_annuels"
|
2023-05-22 14:56:40 +02:00
|
|
|
ATTESTATION_SOCIALE_RESOURCE_NAME = "v4/urssaf/unites_legales/%{id}/attestation_vigilance"
|
2023-05-28 18:03:45 +02:00
|
|
|
ATTESTATION_FISCALE_RESOURCE_NAME = "v4/dgfip/unites_legales/%{id}/attestation_fiscale"
|
2023-05-28 13:10:18 +02:00
|
|
|
BILANS_BDF_RESOURCE_NAME = "v3/banque_de_france/unites_legales/%{id}/bilans"
|
2023-11-14 17:22:18 +01:00
|
|
|
PRIVILEGES_RESOURCE_NAME = "privileges"
|
2018-03-15 14:02:29 +01:00
|
|
|
|
2020-12-03 10:26:05 +01:00
|
|
|
TIMEOUT = 20
|
2021-02-10 09:59:58 +01:00
|
|
|
DEFAULT_API_ENTREPRISE_DELAY = 0.0
|
2018-03-27 17:16:56 +02:00
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
attr_reader :procedure
|
|
|
|
attr_accessor :token
|
2022-07-25 16:27:26 +02:00
|
|
|
attr_accessor :api_object
|
2022-07-20 14:52:17 +02:00
|
|
|
|
|
|
|
def initialize(procedure_id = nil)
|
|
|
|
return if procedure_id.blank?
|
|
|
|
|
|
|
|
@procedure = Procedure.find(procedure_id)
|
|
|
|
@token = @procedure.api_entreprise_token
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def entreprise(siren)
|
|
|
|
call_with_siret(ENTREPRISE_RESOURCE_NAME, siren)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def etablissement(siret)
|
|
|
|
call_with_siret(ETABLISSEMENT_RESOURCE_NAME, siret)
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
|
2023-05-25 14:46:34 +02:00
|
|
|
def extrait_kbis(siren)
|
|
|
|
call_with_siret(EXTRAIT_KBIS_NAME, siren)
|
|
|
|
end
|
|
|
|
|
2023-05-25 15:29:16 +02:00
|
|
|
def tva(siren)
|
|
|
|
call_with_siret(TVA_NAME, siren)
|
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def exercices(siret)
|
|
|
|
call_with_siret(EXERCICES_RESOURCE_NAME, siret)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def rna(siret)
|
|
|
|
call_with_siret(RNA_RESOURCE_NAME, siret)
|
|
|
|
end
|
|
|
|
|
2023-10-17 13:23:17 +02:00
|
|
|
def effectifs(siret, annee, mois)
|
|
|
|
endpoint = [EFFECTIFS_RESOURCE_NAME, mois, "annee", annee].join('/')
|
|
|
|
call_with_siret(endpoint, siret)
|
2020-04-22 17:53:45 +02:00
|
|
|
end
|
|
|
|
|
2023-10-17 13:26:21 +02:00
|
|
|
def effectifs_annuels(siren, annee)
|
|
|
|
endpoint = [EFFECTIFS_ANNUELS_RESOURCE_NAME, annee].join('/')
|
|
|
|
call_with_siret(endpoint, siren)
|
2020-04-07 14:20:22 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def attestation_sociale(siren)
|
|
|
|
return unless procedure.api_entreprise_role?("attestations_sociales")
|
|
|
|
|
|
|
|
call_with_siret(ATTESTATION_SOCIALE_RESOURCE_NAME, siren)
|
2020-04-27 18:09:00 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def attestation_fiscale(siren, user_id)
|
|
|
|
return unless procedure.api_entreprise_role?("attestations_fiscales")
|
|
|
|
|
|
|
|
call_with_siret(ATTESTATION_FISCALE_RESOURCE_NAME, siren, user_id: user_id)
|
2020-04-29 17:34:54 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def bilans_bdf(siren)
|
|
|
|
return unless procedure.api_entreprise_role?("bilans_entreprise_bdf")
|
|
|
|
|
|
|
|
call_with_siret(BILANS_BDF_RESOURCE_NAME, siren)
|
2020-10-19 11:33:36 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def privileges
|
|
|
|
url = make_url(PRIVILEGES_RESOURCE_NAME)
|
|
|
|
call(url)
|
2020-04-30 08:18:17 +02:00
|
|
|
end
|
|
|
|
|
2022-09-15 17:49:57 +02:00
|
|
|
def current_status
|
2023-04-27 17:17:38 +02:00
|
|
|
status_url = "https://status.entreprise.api.gouv.fr/summary.json"
|
2022-09-15 17:49:57 +02:00
|
|
|
response = Typhoeus.get(status_url, timeout: 1)
|
|
|
|
|
|
|
|
handle_response(response)
|
|
|
|
end
|
|
|
|
|
2018-12-07 10:27:40 +01:00
|
|
|
private
|
|
|
|
|
2023-07-26 19:27:08 +02:00
|
|
|
def recipient_for(siret_or_siren)
|
|
|
|
service_siret = @procedure&.service && @procedure.service.siret.presence
|
|
|
|
return service_siret if service_siret && !service_siret.starts_with?(siret_or_siren)
|
|
|
|
ENV.fetch('API_ENTREPRISE_DEFAULT_SIRET')
|
2023-07-18 18:13:18 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def call_with_siret(resource_name, siret_or_siren, user_id: nil)
|
|
|
|
url = make_url(resource_name, siret_or_siren)
|
|
|
|
|
2023-07-26 19:27:08 +02:00
|
|
|
params = build_params(user_id, siret_or_siren)
|
2022-07-20 14:52:17 +02:00
|
|
|
|
|
|
|
call(url, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(url, params = nil)
|
|
|
|
verify_token!
|
2021-02-10 09:59:58 +01:00
|
|
|
|
|
|
|
# this is a poor man throttling
|
|
|
|
# the idea is to queue api entreprise job on 1 worker
|
|
|
|
# and add a delay between each call
|
|
|
|
# example: API_ENTREPRISE_DELAY=1 => 60 rpm max
|
|
|
|
if api_entreprise_delay != 0.0
|
|
|
|
sleep api_entreprise_delay
|
|
|
|
end
|
|
|
|
|
2020-10-19 11:33:36 +02:00
|
|
|
response = Typhoeus.get(url,
|
2020-12-10 15:28:39 +01:00
|
|
|
headers: { Authorization: "Bearer #{token}" },
|
2022-07-20 14:52:17 +02:00
|
|
|
params: params,
|
2020-10-19 11:33:36 +02:00
|
|
|
timeout: TIMEOUT)
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
handle_response(response)
|
2020-10-19 11:33:36 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def handle_response(response)
|
2018-10-04 16:22:19 +02:00
|
|
|
if response.success?
|
2018-03-26 16:38:03 +02:00
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
2019-06-12 11:39:49 +02:00
|
|
|
elsif response.code&.between?(401, 499)
|
2020-12-03 16:52:58 +01:00
|
|
|
raise Error::ResourceNotFound.new(response)
|
2020-05-19 10:43:49 +02:00
|
|
|
elsif response.code == 400
|
2020-12-03 16:52:58 +01:00
|
|
|
raise Error::BadFormatRequest.new(response)
|
2020-12-02 17:17:00 +01:00
|
|
|
elsif response.code == 502
|
2020-12-03 16:52:58 +01:00
|
|
|
raise Error::BadGateway.new(response)
|
2020-10-27 21:13:06 +01:00
|
|
|
elsif response.code == 503
|
2020-12-03 16:52:58 +01:00
|
|
|
raise Error::ServiceUnavailable.new(response)
|
2020-12-03 10:39:51 +01:00
|
|
|
elsif response.timed_out?
|
2020-12-03 16:52:58 +01:00
|
|
|
raise Error::TimedOut.new(response)
|
2019-04-30 16:36:40 +02:00
|
|
|
else
|
2020-12-03 16:52:58 +01:00
|
|
|
raise Error::RequestFailed.new(response)
|
2018-03-26 16:38:03 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def make_url(resource_name, siret_or_siren = nil)
|
2023-05-19 13:59:53 +02:00
|
|
|
[API_ENTREPRISE_URL, format(resource_name, id: siret_or_siren)].compact.join("/")
|
2018-03-15 14:17:57 +01:00
|
|
|
end
|
|
|
|
|
2023-07-26 19:27:08 +02:00
|
|
|
def build_params(user_id, siret_or_siren)
|
|
|
|
params = base_params(siret_or_siren)
|
2022-07-20 14:52:17 +02:00
|
|
|
|
2022-07-25 16:27:26 +02:00
|
|
|
params[:object] = if api_object.present?
|
|
|
|
api_object
|
|
|
|
elsif procedure.present?
|
|
|
|
"procedure_id: #{procedure.id}"
|
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
params[:user_id] = user_id if user_id.present?
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2023-07-26 19:27:08 +02:00
|
|
|
def base_params(siret_or_siren)
|
2022-07-20 14:52:17 +02:00
|
|
|
{
|
2024-03-26 12:08:35 +01:00
|
|
|
context: APPLICATION_NAME,
|
2023-07-26 19:27:08 +02:00
|
|
|
recipient: recipient_for(siret_or_siren),
|
2020-12-10 15:28:39 +01:00
|
|
|
non_diffusables: true
|
2018-03-15 12:02:45 +01:00
|
|
|
}
|
|
|
|
end
|
2018-03-16 13:07:20 +01:00
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def api_entreprise_delay
|
|
|
|
ENV.fetch("API_ENTREPRISE_DELAY", DEFAULT_API_ENTREPRISE_DELAY).to_f
|
2018-03-16 13:07:20 +01:00
|
|
|
end
|
2021-02-10 09:59:58 +01:00
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
def verify_token!
|
|
|
|
return unless APIEntrepriseToken.new(token).expired?
|
|
|
|
|
|
|
|
raise APIEntrepriseToken::TokenError, I18n.t("api_entreprise.errors.token_expired")
|
2021-02-10 09:59:58 +01:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|