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

167 lines
4.3 KiB
Ruby
Raw Normal View History

2020-08-05 18:40:47 +02: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"
PRIVILEGES_RESOURCE_NAME = "privileges"
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
attr_reader :procedure
attr_accessor :token
attr_accessor :api_object
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
def entreprise(siren)
call_with_siret(ENTREPRISE_RESOURCE_NAME, siren)
2015-08-10 11:05:06 +02:00
end
def etablissement(siret)
call_with_siret(ETABLISSEMENT_RESOURCE_NAME, siret)
end
def exercices(siret)
call_with_siret(EXERCICES_RESOURCE_NAME, siret)
2015-12-11 10:36:16 +01:00
end
def rna(siret)
call_with_siret(RNA_RESOURCE_NAME, siret)
end
def effectifs(siren, annee, mois)
2020-04-07 14:20:22 +02:00
endpoint = [EFFECTIFS_RESOURCE_NAME, annee, mois, "entreprise"].join('/')
call_with_siret(endpoint, siren)
end
def effectifs_annuels(siren)
call_with_siret(EFFECTIFS_ANNUELS_RESOURCE_NAME, siren)
2020-04-07 14:20:22 +02:00
end
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
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
def bilans_bdf(siren)
return unless procedure.api_entreprise_role?("bilans_entreprise_bdf")
call_with_siret(BILANS_BDF_RESOURCE_NAME, siren)
end
def privileges
url = make_url(PRIVILEGES_RESOURCE_NAME)
call(url)
2020-04-30 08:18:17 +02:00
end
def current_status
status_url = "https://entreprise.api.gouv.fr/watchdoge/dashboard/current_status"
response = Typhoeus.get(status_url, timeout: 1)
handle_response(response)
end
private
def call_with_siret(resource_name, siret_or_siren, user_id: nil)
url = make_url(resource_name, siret_or_siren)
params = build_params(user_id)
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
response = Typhoeus.get(url,
headers: { Authorization: "Bearer #{token}" },
params: params,
timeout: TIMEOUT)
handle_response(response)
end
def handle_response(response)
if response.success?
JSON.parse(response.body, symbolize_names: true)
elsif response.code&.between?(401, 499)
2020-12-03 16:52:58 +01:00
raise Error::ResourceNotFound.new(response)
elsif response.code == 400
2020-12-03 16:52:58 +01:00
raise Error::BadFormatRequest.new(response)
elsif response.code == 502
2020-12-03 16:52:58 +01:00
raise Error::BadGateway.new(response)
elsif response.code == 503
2020-12-03 16:52:58 +01:00
raise Error::ServiceUnavailable.new(response)
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)
end
2015-08-10 11:05:06 +02:00
end
def make_url(resource_name, siret_or_siren = nil)
[API_ENTREPRISE_URL, resource_name, siret_or_siren].compact.join("/")
end
def build_params(user_id)
params = base_params
params[:object] = if api_object.present?
api_object
elsif procedure.present?
"procedure_id: #{procedure.id}"
end
params[:user_id] = user_id if user_id.present?
params
end
def base_params
# rubocop:disable DS/ApplicationName
{
context: "demarches-simplifiees.fr",
recipient: ENV.fetch('API_ENTREPRISE_DEFAULT_SIRET'),
non_diffusables: true
}
# rubocop:enable DS/ApplicationName
end
2018-03-16 13:07:20 +01: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
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