2020-08-05 18:40:47 +02:00
|
|
|
class APIEntreprise::API
|
2018-03-15 14:02:29 +01:00
|
|
|
ENTREPRISE_RESOURCE_NAME = "entreprises"
|
|
|
|
ETABLISSEMENT_RESOURCE_NAME = "etablissements"
|
|
|
|
EXERCICES_RESOURCE_NAME = "exercices"
|
|
|
|
RNA_RESOURCE_NAME = "associations"
|
2020-04-07 14:20:22 +02:00
|
|
|
EFFECTIFS_RESOURCE_NAME = "effectifs_mensuels_acoss_covid"
|
2020-04-22 17:53:45 +02:00
|
|
|
EFFECTIFS_ANNUELS_RESOURCE_NAME = "effectifs_annuels_acoss_covid"
|
2020-04-27 18:09:00 +02:00
|
|
|
ATTESTATION_SOCIALE_RESOURCE_NAME = "attestations_sociales_acoss"
|
2020-04-29 17:34:54 +02:00
|
|
|
ATTESTATION_FISCALE_RESOURCE_NAME = "attestations_fiscales_dgfip"
|
2020-04-30 08:18:17 +02:00
|
|
|
BILANS_BDF_RESOURCE_NAME = "bilans_entreprises_bdf"
|
2020-10-19 11:33:36 +02: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
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.entreprise(siren, procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.etablissement(siret, procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(ETABLISSEMENT_RESOURCE_NAME, siret, procedure_id)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.exercices(siret, procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(EXERCICES_RESOURCE_NAME, siret, procedure_id)
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.rna(siret, procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(RNA_RESOURCE_NAME, siret, procedure_id)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
|
2020-04-07 14:20:22 +02:00
|
|
|
def self.effectifs(siren, procedure_id, annee, mois)
|
|
|
|
endpoint = [EFFECTIFS_RESOURCE_NAME, annee, mois, "entreprise"].join('/')
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(endpoint, siren, procedure_id)
|
2020-04-22 17:53:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.effectifs_annuels(siren, procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(EFFECTIFS_ANNUELS_RESOURCE_NAME, siren, procedure_id)
|
2020-04-07 14:20:22 +02:00
|
|
|
end
|
|
|
|
|
2020-04-27 18:09:00 +02:00
|
|
|
def self.attestation_sociale(siren, procedure_id)
|
2020-04-28 10:15:08 +02:00
|
|
|
procedure = Procedure.find(procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(ATTESTATION_SOCIALE_RESOURCE_NAME, siren, procedure_id) if procedure.api_entreprise_role?("attestations_sociales")
|
2020-04-27 18:09:00 +02:00
|
|
|
end
|
|
|
|
|
2020-04-29 17:34:54 +02:00
|
|
|
def self.attestation_fiscale(siren, procedure_id, user_id)
|
|
|
|
procedure = Procedure.find(procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(ATTESTATION_FISCALE_RESOURCE_NAME, siren, procedure_id, user_id) if procedure.api_entreprise_role?("attestations_fiscales")
|
2020-04-29 17:34:54 +02:00
|
|
|
end
|
|
|
|
|
2020-04-30 08:18:17 +02:00
|
|
|
def self.bilans_bdf(siren, procedure_id)
|
|
|
|
procedure = Procedure.find(procedure_id)
|
2020-10-19 11:33:36 +02:00
|
|
|
call_with_siret(BILANS_BDF_RESOURCE_NAME, siren, procedure_id) if procedure.api_entreprise_role?("bilans_entreprise_bdf")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.privileges(token)
|
|
|
|
call_with_token(PRIVILEGES_RESOURCE_NAME, token)
|
2020-04-30 08:18:17 +02:00
|
|
|
end
|
|
|
|
|
2018-12-07 10:27:40 +01:00
|
|
|
private
|
|
|
|
|
2020-10-19 11:33:36 +02:00
|
|
|
def self.call_with_token(resource_name, token)
|
2020-12-10 15:28:39 +01:00
|
|
|
url = "#{API_ENTREPRISE_URL}/#{resource_name}"
|
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}" },
|
2020-10-19 11:33:36 +02:00
|
|
|
timeout: TIMEOUT)
|
|
|
|
|
|
|
|
if response.success?
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
else
|
2020-12-03 16:52:58 +01:00
|
|
|
raise RequestFailed.new(response)
|
2020-10-19 11:33:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call_with_siret(resource_name, siret_or_siren, procedure_id, user_id = nil)
|
2022-07-12 19:06:20 +02:00
|
|
|
if APIEntrepriseToken.new(token_for_procedure(procedure_id)).expired?
|
|
|
|
raise APIEntrepriseToken::TokenError, I18n.t("api_entreprise.errors.token_expired")
|
|
|
|
end
|
|
|
|
|
2018-03-15 14:15:32 +01:00
|
|
|
url = url(resource_name, siret_or_siren)
|
2020-04-29 17:34:54 +02:00
|
|
|
params = params(siret_or_siren, procedure_id, user_id)
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2021-02-10 09:59:58 +01:00
|
|
|
if api_entreprise_delay != 0.0
|
|
|
|
sleep api_entreprise_delay
|
|
|
|
end
|
|
|
|
|
2018-03-26 16:38:03 +02:00
|
|
|
response = Typhoeus.get(url,
|
2020-12-10 15:28:39 +01:00
|
|
|
headers: { Authorization: "Bearer #{token_for_procedure(procedure_id)}" },
|
2018-03-26 16:38:03 +02:00
|
|
|
params: params,
|
2018-03-27 17:16:56 +02:00
|
|
|
timeout: TIMEOUT)
|
2018-03-15 14:23:57 +01:00
|
|
|
|
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
|
|
|
|
|
2018-03-15 14:17:57 +01:00
|
|
|
def self.url(resource_name, siret_or_siren)
|
2021-04-06 14:37:04 +02:00
|
|
|
[API_ENTREPRISE_URL, resource_name, siret_or_siren].join("/")
|
2018-03-15 14:17:57 +01:00
|
|
|
end
|
|
|
|
|
2020-04-29 17:34:54 +02:00
|
|
|
def self.params(siret_or_siren, procedure_id, user_id)
|
2020-07-23 16:01:15 +02:00
|
|
|
# rubocop:disable DS/ApplicationName
|
2020-04-29 17:34:54 +02:00
|
|
|
params = {
|
2018-03-15 12:02:45 +01:00
|
|
|
context: "demarches-simplifiees.fr",
|
2022-04-25 15:50:33 +02:00
|
|
|
recipient: ENV.fetch('API_ENTREPRISE_DEFAULT_SIRET'),
|
2018-03-15 14:14:50 +01:00
|
|
|
object: "procedure_id: #{procedure_id}",
|
2020-12-10 15:28:39 +01:00
|
|
|
non_diffusables: true
|
2018-03-15 12:02:45 +01:00
|
|
|
}
|
2020-07-23 16:01:15 +02:00
|
|
|
# rubocop:enable DS/ApplicationName
|
2020-04-29 17:34:54 +02:00
|
|
|
params[:user_id] = user_id if user_id.present?
|
|
|
|
params
|
2018-03-15 12:02:45 +01:00
|
|
|
end
|
2018-03-16 13:07:20 +01:00
|
|
|
|
2020-04-27 15:58:04 +02:00
|
|
|
def self.token_for_procedure(procedure_id)
|
|
|
|
procedure = Procedure.find(procedure_id)
|
2020-04-29 10:23:35 +02:00
|
|
|
procedure.api_entreprise_token
|
2018-03-16 13:07:20 +01:00
|
|
|
end
|
2021-02-10 09:59:58 +01:00
|
|
|
|
|
|
|
def self.api_entreprise_delay
|
|
|
|
ENV.fetch("API_ENTREPRISE_DELAY", DEFAULT_API_ENTREPRISE_DELAY).to_f
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|