diff --git a/Gemfile b/Gemfile index 73142e9b7..032f94b43 100644 --- a/Gemfile +++ b/Gemfile @@ -63,7 +63,6 @@ gem 'rails' gem 'rails-i18n' # Locales par défaut gem 'rake-progressbar', require: false gem 'react-rails' -gem 'rest-client' gem 'rgeo-geojson' gem 'sanitize-url' gem 'sassc-rails' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index 62899d419..a13bb0995 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -793,7 +793,6 @@ DEPENDENCIES rails-i18n rake-progressbar react-rails - rest-client rgeo-geojson rspec-rails (~> 4.0.0.beta) rspec_junit_formatter diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 059c4554f..3fdd88b7b 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -62,7 +62,7 @@ class Champs::CarteController < ApplicationController @champ.save end - rescue RestClient::ResourceNotFound + rescue ApiCarto::API::ResourceNotFound flash.alert = 'Les données cartographiques sont temporairement indisponibles. Réessayez dans un instant.' response.status = 503 end diff --git a/app/controllers/champs/siret_controller.rb b/app/controllers/champs/siret_controller.rb index f78a35fb6..0eabd821e 100644 --- a/app/controllers/champs/siret_controller.rb +++ b/app/controllers/champs/siret_controller.rb @@ -16,7 +16,7 @@ class Champs::SiretController < ApplicationController begin etablissement = find_etablissement_with_siret - rescue RestClient::RequestFailed + rescue ApiEntreprise::API::RequestFailed return siret_error(:network_error) end if etablissement.blank? diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index e66aaf44b..2e70cd862 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -106,7 +106,7 @@ module Users sanitized_siret = siret_model.siret begin etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(sanitized_siret, @dossier.procedure.id) - rescue RestClient::RequestFailed + rescue ApiEntreprise::API::RequestFailed return render_siret_error(t('errors.messages.siret_network_error')) end if etablissement_attributes.blank? diff --git a/app/lib/api_carto/api.rb b/app/lib/api_carto/api.rb index eadb813f8..865a8b192 100644 --- a/app/lib/api_carto/api.rb +++ b/app/lib/api_carto/api.rb @@ -1,4 +1,7 @@ class ApiCarto::API + class ResourceNotFound < StandardError + end + def self.search_qp(geojson) url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/") call(url, geojson) @@ -19,7 +22,7 @@ class ApiCarto::API else message = response.code == 0 ? response.return_message : response.code.to_s Rails.logger.error "[ApiCarto] Error on #{url}: #{message}" - raise RestClient::ResourceNotFound + raise ResourceNotFound end end end diff --git a/app/lib/api_entreprise/adapter.rb b/app/lib/api_entreprise/adapter.rb index 8c4b21de4..4e26e8a1f 100644 --- a/app/lib/api_entreprise/adapter.rb +++ b/app/lib/api_entreprise/adapter.rb @@ -9,7 +9,7 @@ class ApiEntreprise::Adapter def data_source begin @data_source ||= get_resource - rescue RestClient::ResourceNotFound + rescue ApiEntreprise::API::ResourceNotFound @data_source = nil end end diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 818d1e833..69304366a 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -6,6 +6,12 @@ class ApiEntreprise::API TIMEOUT = 15 + class ResourceNotFound < StandardError + end + + class RequestFailed < StandardError + end + def self.entreprise(siren, procedure_id) call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id) end @@ -35,9 +41,9 @@ class ApiEntreprise::API if response.success? JSON.parse(response.body, symbolize_names: true) elsif response.code&.between?(401, 499) - raise RestClient::ResourceNotFound + raise ResourceNotFound else - raise RestClient::RequestFailed + raise RequestFailed end end diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index c537a4e50..2c4201e7f 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -51,20 +51,29 @@ class Pipedrive::API api_token: token }) - response = RestClient.get(url, params: params) - JSON.parse(response.body)['data'] + response = Typhoeus.get(url, params: params) + + if response.success? + JSON.parse(response.body)['data'] + end end def self.put(url, params) - url = "#{url}?api_token=#{token}" - - RestClient.put(url, params.to_json, { content_type: :json }) + Typhoeus.put( + url, + params: { api_token: token }, + body: params.to_json, + headers: { 'content-type' => 'application/json' } + ) end def self.post(url, params) - url = "#{url}?api_token=#{token}" - - RestClient.post(url, params.to_json, { content_type: :json }) + Typhoeus.post( + url, + params: { api_token: token }, + body: params.to_json, + headers: { 'content-type' => 'application/json' } + ) end def self.token diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index feb687544..32fa8ada7 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -4,7 +4,7 @@ class ApiEntrepriseService # 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 + # Raises a ApiEntreprise::API::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 @@ -14,13 +14,13 @@ class ApiEntrepriseService begin association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params etablissement_params.merge!(association_params) - rescue RestClient::RequestFailed + rescue ApiEntreprise::API::RequestFailed end begin exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params etablissement_params.merge!(exercices_params) - rescue RestClient::RequestFailed + rescue ApiEntreprise::API::RequestFailed end etablissement_params.merge(entreprise_params) diff --git a/spec/controllers/champs/carte_controller_spec.rb b/spec/controllers/champs/carte_controller_spec.rb index e420323f6..dbba13ebb 100644 --- a/spec/controllers/champs/carte_controller_spec.rb +++ b/spec/controllers/champs/carte_controller_spec.rb @@ -75,7 +75,7 @@ describe Champs::CarteController, type: :controller do allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter) .to receive(:results) - .and_raise(RestClient::ResourceNotFound) + .and_raise(ApiCarto::API::ResourceNotFound) post :show, params: params, format: 'js' end diff --git a/spec/controllers/champs/siret_controller_spec.rb b/spec/controllers/champs/siret_controller_spec.rb index e374ce4a1..cd7163803 100644 --- a/spec/controllers/champs/siret_controller_spec.rb +++ b/spec/controllers/champs/siret_controller_spec.rb @@ -59,7 +59,7 @@ describe Champs::SiretController, type: :controller do let(:siret) { '82161143100015' } before do - allow(controller).to receive(:find_etablissement_with_siret).and_raise(RestClient::RequestFailed) + allow(controller).to receive(:find_etablissement_with_siret).and_raise(ApiEntreprise::API::RequestFailed) end subject! { get :show, params: params, format: 'js' } diff --git a/spec/lib/api_carto/api_spec.rb b/spec/lib/api_carto/api_spec.rb index 2e52a58b1..bb03c40e5 100644 --- a/spec/lib/api_carto/api_spec.rb +++ b/spec/lib/api_carto/api_spec.rb @@ -15,8 +15,8 @@ describe ApiCarto::API do let(:status) { 404 } let(:body) { '' } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiCarto::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) end end @@ -25,8 +25,8 @@ describe ApiCarto::API do let(:status) { 500 } let(:body) { 'toto' } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiCarto::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) end end @@ -63,8 +63,8 @@ describe ApiCarto::API do let(:status) { 404 } let(:body) { '' } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiCarto::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) end end diff --git a/spec/lib/api_carto/cadastre_adapter_spec.rb b/spec/lib/api_carto/cadastre_adapter_spec.rb index aaba3cae7..63d7f83c9 100644 --- a/spec/lib/api_carto/cadastre_adapter_spec.rb +++ b/spec/lib/api_carto/cadastre_adapter_spec.rb @@ -60,6 +60,6 @@ describe ApiCarto::CadastreAdapter do let(:status) { 404 } let(:body) { '' } - it { expect { subject }.to raise_error(RestClient::ResourceNotFound) } + it { expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) } end end diff --git a/spec/lib/api_carto/quartiers_prioritaires_adapter_spec.rb b/spec/lib/api_carto/quartiers_prioritaires_adapter_spec.rb index 32f5eb60b..70593327d 100644 --- a/spec/lib/api_carto/quartiers_prioritaires_adapter_spec.rb +++ b/spec/lib/api_carto/quartiers_prioritaires_adapter_spec.rb @@ -33,6 +33,6 @@ describe ApiCarto::QuartiersPrioritairesAdapter do let(:status) { 404 } let(:body) { '' } - it { expect { subject }.to raise_error(RestClient::ResourceNotFound) } + it { expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) } end end diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index abd7c07c2..e4453ad99 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -16,8 +16,8 @@ describe ApiEntreprise::API do 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) + it 'raises ApiEntreprise::API::RequestFailed' do + expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed) end end @@ -26,8 +26,8 @@ describe ApiEntreprise::API do let(:status) { 404 } 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) + it 'raises ApiEntreprise::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound) end end @@ -36,8 +36,8 @@ describe ApiEntreprise::API do let(:status) { 403 } let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_private.json') } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiEntreprise::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound) end end @@ -64,8 +64,8 @@ describe ApiEntreprise::API do let(:status) { 404 } let(:body) { '' } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiEntreprise::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound) end end @@ -93,8 +93,8 @@ describe ApiEntreprise::API do let(:status) { 404 } let(:body) { '' } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiEntreprise::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound) end end @@ -105,7 +105,7 @@ describe ApiEntreprise::API do let(:status) { 200 } let(:body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') } - it 'raises RestClient::Unauthorized' do + it 'success' do expect(subject).to eq(JSON.parse(body, symbolize_names: true)) end end @@ -124,8 +124,8 @@ describe ApiEntreprise::API do let(:status) { 404 } let(:body) { '' } - it 'raises RestClient::ResourceNotFound' do - expect { subject }.to raise_error(RestClient::ResourceNotFound) + it 'raises ApiEntreprise::API::ResourceNotFound' do + expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound) end end diff --git a/spec/lib/api_entreprise/entreprise_adapter_spec.rb b/spec/lib/api_entreprise/entreprise_adapter_spec.rb index 51f120d2d..56c78d8aa 100644 --- a/spec/lib/api_entreprise/entreprise_adapter_spec.rb +++ b/spec/lib/api_entreprise/entreprise_adapter_spec.rb @@ -84,7 +84,7 @@ describe ApiEntreprise::EntrepriseAdapter do let(:status) { 502 } it 'raises an exception' do - expect { subject }.to raise_error(RestClient::RequestFailed) + expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed) end end end diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index 0fefcb99e..192f16400 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -66,8 +66,8 @@ describe ApiEntrepriseService do let(:entreprises_status) { 400 } let(:entreprises_body) { '' } - it 'should raise RestClient::RequestFailed' do - expect { result }.to raise_error(RestClient::RequestFailed) + it 'should raise ApiEntreprise::API::RequestFailed' do + expect { result }.to raise_error(ApiEntreprise::API::RequestFailed) end end @@ -75,8 +75,8 @@ describe ApiEntrepriseService do let(:etablissements_status) { 400 } let(:etablissements_body) { '' } - it 'should raise RestClient::RequestFailed' do - expect { result }.to raise_error(RestClient::RequestFailed) + it 'should raise ApiEntreprise::API::RequestFailed' do + expect { result }.to raise_error(ApiEntreprise::API::RequestFailed) end end