api_entreprise: raise on network error
This commit is contained in:
parent
7c1c61248e
commit
ad4a89f0f6
7 changed files with 50 additions and 9 deletions
|
@ -7,9 +7,11 @@ class ApiEntreprise::Adapter
|
||||||
end
|
end
|
||||||
|
|
||||||
def data_source
|
def data_source
|
||||||
@data_source ||= get_resource
|
begin
|
||||||
rescue
|
@data_source ||= get_resource
|
||||||
@data_source = nil
|
rescue RestClient::ResourceNotFound
|
||||||
|
@data_source = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_params
|
def to_params
|
||||||
|
|
|
@ -34,8 +34,10 @@ class ApiEntreprise::API
|
||||||
|
|
||||||
if response.success?
|
if response.success?
|
||||||
JSON.parse(response.body, symbolize_names: true)
|
JSON.parse(response.body, symbolize_names: true)
|
||||||
else
|
elsif response.code == 404 || response.code == 422
|
||||||
raise RestClient::ResourceNotFound
|
raise RestClient::ResourceNotFound
|
||||||
|
else
|
||||||
|
raise RestClient::RequestFailed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
class ApiEntrepriseService
|
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)
|
def self.get_etablissement_params_for_siret(siret, procedure_id)
|
||||||
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
|
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
|
||||||
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params
|
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params
|
||||||
|
|
7
spec/fixtures/files/api_entreprise/entreprises_unavailable.json
vendored
Normal file
7
spec/fixtures/files/api_entreprise/entreprises_unavailable.json
vendored
Normal 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
|
||||||
|
}
|
|
@ -2,21 +2,35 @@ require 'spec_helper'
|
||||||
|
|
||||||
describe ApiEntreprise::API do
|
describe ApiEntreprise::API do
|
||||||
let(:procedure_id) { 12 }
|
let(:procedure_id) { 12 }
|
||||||
|
|
||||||
describe '.entreprise' do
|
describe '.entreprise' do
|
||||||
subject { described_class.entreprise(siren, procedure_id) }
|
subject { described_class.entreprise(siren, procedure_id) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
|
||||||
.to_return(status: status, body: body)
|
.to_return(status: status, body: body)
|
||||||
end
|
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
|
context 'when siren does not exist' do
|
||||||
let(:siren) { '111111111' }
|
let(:siren) { '111111111' }
|
||||||
let(:status) { 404 }
|
let(:status) { 404 }
|
||||||
let(:body) { '' }
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') }
|
||||||
|
|
||||||
it 'raises RestClient::ResourceNotFound' do
|
it 'raises RestClient::ResourceNotFound' do
|
||||||
expect { subject }.to raise_error(RestClient::ResourceNotFound)
|
expect { subject }.to raise_error(RestClient::ResourceNotFound)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when siret exist' do
|
context 'when siret exist' do
|
||||||
let(:siren) { '418166096' }
|
let(:siren) { '418166096' }
|
||||||
let(:status) { 200 }
|
let(:status) { 200 }
|
||||||
|
|
|
@ -11,7 +11,7 @@ describe ApiEntreprise::EntrepriseAdapter do
|
||||||
.to_return(body: body, status: status)
|
.to_return(body: body, status: status)
|
||||||
end
|
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(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
|
||||||
let(:status) { 200 }
|
let(:status) { 200 }
|
||||||
|
|
||||||
|
@ -70,12 +70,21 @@ describe ApiEntreprise::EntrepriseAdapter do
|
||||||
end
|
end
|
||||||
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(: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
|
it '#to_params class est une Hash ?' do
|
||||||
expect(subject).to eq({})
|
expect(subject).to eq({})
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -17,7 +17,7 @@ describe ApiEntreprise::RNAAdapter do
|
||||||
context 'when siret is not valid' do
|
context 'when siret is not valid' do
|
||||||
let(:siret) { '234567' }
|
let(:siret) { '234567' }
|
||||||
let(:body) { '' }
|
let(:body) { '' }
|
||||||
let(:status) { '404' }
|
let(:status) { 404 }
|
||||||
|
|
||||||
it { is_expected.to eq({}) }
|
it { is_expected.to eq({}) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue