api_entreprise: raise on network error

This commit is contained in:
Pierre de La Morinerie 2019-04-30 14:36:40 +00:00
parent 7c1c61248e
commit ad4a89f0f6
7 changed files with 50 additions and 9 deletions

View file

@ -7,9 +7,11 @@ class ApiEntreprise::Adapter
end
def data_source
@data_source ||= get_resource
rescue
@data_source = nil
begin
@data_source ||= get_resource
rescue RestClient::ResourceNotFound
@data_source = nil
end
end
def to_params

View file

@ -34,8 +34,10 @@ class ApiEntreprise::API
if response.success?
JSON.parse(response.body, symbolize_names: true)
else
elsif response.code == 404 || response.code == 422
raise RestClient::ResourceNotFound
else
raise RestClient::RequestFailed
end
end

View file

@ -1,4 +1,11 @@
class ApiEntrepriseService
# Retrieve all informations we can get about a SIRET.
#
# Returns nil if the SIRET is unknown; and nested params
# suitable for being saved into a Etablissement object otherwise.
#
# Raises a RestClient::RequestFailed exception on transcient errors
# (timeout, 5XX HTTP error code, etc.)
def self.get_etablissement_params_for_siret(siret, procedure_id)
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params

View file

@ -0,0 +1,7 @@
{
"errors": [
"Erreur interne du serveur",
"Le siret ou siren indiqué n'existe pas, n'est pas connu ou ne comporte aucune information pour cet appel"
],
"gateway_error": true
}

View file

@ -2,21 +2,35 @@ require 'spec_helper'
describe ApiEntreprise::API do
let(:procedure_id) { 12 }
describe '.entreprise' do
subject { described_class.entreprise(siren, procedure_id) }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
.to_return(status: status, body: body)
end
context 'when the service is unavailable' do
let(:siren) { '111111111' }
let(:status) { 502 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
it 'raises RestClient::RequestFailed' do
expect { subject }.to raise_error(RestClient::RequestFailed)
end
end
context 'when siren does not exist' do
let(:siren) { '111111111' }
let(:status) { 404 }
let(:body) { '' }
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') }
it 'raises RestClient::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound)
end
end
context 'when siret exist' do
let(:siren) { '418166096' }
let(:status) { 200 }

View file

@ -11,7 +11,7 @@ describe ApiEntreprise::EntrepriseAdapter do
.to_return(body: body, status: status)
end
context "when SIRET is OK" do
context "when the SIRET is valid" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
let(:status) { 200 }
@ -70,12 +70,21 @@ describe ApiEntreprise::EntrepriseAdapter do
end
end
context "when SIRET is KO" do
context "when the SIRET is unknown" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') }
let(:status) { 206 }
let(:status) { 404 }
it '#to_params class est une Hash ?' do
expect(subject).to eq({})
end
end
context "when the service is unavailable" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
let(:status) { 502 }
it 'raises an exception' do
expect { subject }.to raise_error(RestClient::RequestFailed)
end
end
end

View file

@ -17,7 +17,7 @@ describe ApiEntreprise::RNAAdapter do
context 'when siret is not valid' do
let(:siret) { '234567' }
let(:body) { '' }
let(:status) { '404' }
let(:status) { 404 }
it { is_expected.to eq({}) }
end