demarches-normaliennes/app/lib/api_entreprise/api.rb

114 lines
3.5 KiB
Ruby
Raw Normal View History

2018-03-15 16:56:21 +01:00
class ApiEntreprise::API
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"
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"
2018-03-27 17:59:51 +02:00
TIMEOUT = 15
2018-03-27 17:16:56 +02:00
2020-03-05 13:50:38 +01:00
class ResourceNotFound < StandardError
end
class RequestFailed < StandardError
end
class BadFormatRequest < StandardError
end
def self.entreprise(siren, procedure_id)
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
2015-08-10 11:05:06 +02:00
end
def self.etablissement(siret, procedure_id)
call(ETABLISSEMENT_RESOURCE_NAME, siret, procedure_id)
2015-08-10 11:05:06 +02:00
end
def self.exercices(siret, procedure_id)
call(EXERCICES_RESOURCE_NAME, siret, procedure_id)
end
def self.rna(siret, procedure_id)
call(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('/')
call(endpoint, siren, procedure_id)
end
def self.effectifs_annuels(siren, procedure_id)
call(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)
call(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)
call(ATTESTATION_FISCALE_RESOURCE_NAME, siren, procedure_id, user_id) if procedure.api_entreprise_role?("attestations_fiscales")
end
2020-04-30 08:18:17 +02:00
def self.bilans_bdf(siren, procedure_id)
procedure = Procedure.find(procedure_id)
call(BILANS_BDF_RESOURCE_NAME, siren, procedure_id) if procedure.api_entreprise_role?("bilans_entreprise_bdf")
end
private
2020-04-29 17:34:54 +02:00
def self.call(resource_name, siret_or_siren, procedure_id, user_id = nil)
return if ApiEntrepriseToken.new(token_for_procedure(procedure_id)).expired?
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
response = Typhoeus.get(url,
params: params,
2018-03-27 17:16:56 +02:00
timeout: TIMEOUT)
2018-03-15 14:23:57 +01:00
if response.success?
JSON.parse(response.body, symbolize_names: true)
elsif response.code&.between?(401, 499)
2020-05-20 15:41:22 +02:00
raise ResourceNotFound, "url: #{url}"
elsif response.code == 400
2020-05-20 15:41:22 +02:00
raise BadFormatRequest, "url: #{url}"
2019-04-30 16:36:40 +02:00
else
raise RequestFailed, "HTTP Error Code: #{response.code} for #{url}\nheaders: #{response.headers}\nbody: #{response.body}"
end
2015-08-10 11:05:06 +02:00
end
def self.url(resource_name, siret_or_siren)
base_url = [API_ENTREPRISE_URL, resource_name, siret_or_siren].join("/")
2019-07-04 12:36:17 +02:00
if Flipper.enabled?(:insee_api_v3)
base_url += "?with_insee_v3=true"
end
base_url
end
2020-04-29 17:34:54 +02:00
def self.params(siret_or_siren, procedure_id, user_id)
# rubocop:disable DS/ApplicationName
2020-04-29 17:34:54 +02:00
params = {
context: "demarches-simplifiees.fr",
recipient: siret_or_siren,
2018-03-15 14:14:50 +01:00
object: "procedure_id: #{procedure_id}",
non_diffusables: true,
token: token_for_procedure(procedure_id)
}
# rubocop:enable DS/ApplicationName
2020-04-29 17:34:54 +02:00
params[:user_id] = user_id if user_id.present?
params
end
2018-03-16 13:07:20 +01: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
2015-08-10 11:05:06 +02:00
end