Merge pull request #4866 from tchak/remove-restclient

Remove restclient
This commit is contained in:
Paul Chavard 2020-03-17 11:24:57 +01:00 committed by GitHub
commit b45b51a554
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 64 additions and 48 deletions

View file

@ -63,7 +63,6 @@ gem 'rails'
gem 'rails-i18n' # Locales par défaut gem 'rails-i18n' # Locales par défaut
gem 'rake-progressbar', require: false gem 'rake-progressbar', require: false
gem 'react-rails' gem 'react-rails'
gem 'rest-client'
gem 'rgeo-geojson' gem 'rgeo-geojson'
gem 'sanitize-url' gem 'sanitize-url'
gem 'sassc-rails' # Use SCSS for stylesheets gem 'sassc-rails' # Use SCSS for stylesheets

View file

@ -793,7 +793,6 @@ DEPENDENCIES
rails-i18n rails-i18n
rake-progressbar rake-progressbar
react-rails react-rails
rest-client
rgeo-geojson rgeo-geojson
rspec-rails (~> 4.0.0.beta) rspec-rails (~> 4.0.0.beta)
rspec_junit_formatter rspec_junit_formatter

View file

@ -62,7 +62,7 @@ class Champs::CarteController < ApplicationController
@champ.save @champ.save
end end
rescue RestClient::ResourceNotFound rescue ApiCarto::API::ResourceNotFound
flash.alert = 'Les données cartographiques sont temporairement indisponibles. Réessayez dans un instant.' flash.alert = 'Les données cartographiques sont temporairement indisponibles. Réessayez dans un instant.'
response.status = 503 response.status = 503
end end

View file

@ -16,7 +16,7 @@ class Champs::SiretController < ApplicationController
begin begin
etablissement = find_etablissement_with_siret etablissement = find_etablissement_with_siret
rescue RestClient::RequestFailed rescue ApiEntreprise::API::RequestFailed
return siret_error(:network_error) return siret_error(:network_error)
end end
if etablissement.blank? if etablissement.blank?

View file

@ -106,7 +106,7 @@ module Users
sanitized_siret = siret_model.siret sanitized_siret = siret_model.siret
begin begin
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(sanitized_siret, @dossier.procedure.id) 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')) return render_siret_error(t('errors.messages.siret_network_error'))
end end
if etablissement_attributes.blank? if etablissement_attributes.blank?

View file

@ -1,4 +1,7 @@
class ApiCarto::API class ApiCarto::API
class ResourceNotFound < StandardError
end
def self.search_qp(geojson) def self.search_qp(geojson)
url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/") url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/")
call(url, geojson) call(url, geojson)
@ -19,7 +22,7 @@ class ApiCarto::API
else else
message = response.code == 0 ? response.return_message : response.code.to_s message = response.code == 0 ? response.return_message : response.code.to_s
Rails.logger.error "[ApiCarto] Error on #{url}: #{message}" Rails.logger.error "[ApiCarto] Error on #{url}: #{message}"
raise RestClient::ResourceNotFound raise ResourceNotFound
end end
end end
end end

View file

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

View file

@ -6,6 +6,12 @@ class ApiEntreprise::API
TIMEOUT = 15 TIMEOUT = 15
class ResourceNotFound < StandardError
end
class RequestFailed < StandardError
end
def self.entreprise(siren, procedure_id) def self.entreprise(siren, procedure_id)
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id) call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
end end
@ -35,9 +41,9 @@ class ApiEntreprise::API
if response.success? if response.success?
JSON.parse(response.body, symbolize_names: true) JSON.parse(response.body, symbolize_names: true)
elsif response.code&.between?(401, 499) elsif response.code&.between?(401, 499)
raise RestClient::ResourceNotFound raise ResourceNotFound
else else
raise RestClient::RequestFailed raise RequestFailed
end end
end end

View file

@ -51,20 +51,29 @@ class Pipedrive::API
api_token: token api_token: token
}) })
response = RestClient.get(url, params: params) response = Typhoeus.get(url, params: params)
JSON.parse(response.body)['data']
if response.success?
JSON.parse(response.body)['data']
end
end end
def self.put(url, params) def self.put(url, params)
url = "#{url}?api_token=#{token}" Typhoeus.put(
url,
RestClient.put(url, params.to_json, { content_type: :json }) params: { api_token: token },
body: params.to_json,
headers: { 'content-type' => 'application/json' }
)
end end
def self.post(url, params) def self.post(url, params)
url = "#{url}?api_token=#{token}" Typhoeus.post(
url,
RestClient.post(url, params.to_json, { content_type: :json }) params: { api_token: token },
body: params.to_json,
headers: { 'content-type' => 'application/json' }
)
end end
def self.token def self.token

View file

@ -4,7 +4,7 @@ class ApiEntrepriseService
# Returns nil if the SIRET is unknown; and nested params # Returns nil if the SIRET is unknown; and nested params
# suitable for being saved into a Etablissement object otherwise. # 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.) # (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
@ -14,13 +14,13 @@ class ApiEntrepriseService
begin begin
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
etablissement_params.merge!(association_params) etablissement_params.merge!(association_params)
rescue RestClient::RequestFailed rescue ApiEntreprise::API::RequestFailed
end end
begin begin
exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params
etablissement_params.merge!(exercices_params) etablissement_params.merge!(exercices_params)
rescue RestClient::RequestFailed rescue ApiEntreprise::API::RequestFailed
end end
etablissement_params.merge(entreprise_params) etablissement_params.merge(entreprise_params)

View file

@ -75,7 +75,7 @@ describe Champs::CarteController, type: :controller do
allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter) allow_any_instance_of(ApiCarto::QuartiersPrioritairesAdapter)
.to receive(:results) .to receive(:results)
.and_raise(RestClient::ResourceNotFound) .and_raise(ApiCarto::API::ResourceNotFound)
post :show, params: params, format: 'js' post :show, params: params, format: 'js'
end end

View file

@ -59,7 +59,7 @@ describe Champs::SiretController, type: :controller do
let(:siret) { '82161143100015' } let(:siret) { '82161143100015' }
before do 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 end
subject! { get :show, params: params, format: 'js' } subject! { get :show, params: params, format: 'js' }

View file

@ -15,8 +15,8 @@ describe ApiCarto::API do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiCarto::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound)
end end
end end
@ -25,8 +25,8 @@ describe ApiCarto::API do
let(:status) { 500 } let(:status) { 500 }
let(:body) { 'toto' } let(:body) { 'toto' }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiCarto::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound)
end end
end end
@ -63,8 +63,8 @@ describe ApiCarto::API do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiCarto::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound)
end end
end end

View file

@ -60,6 +60,6 @@ describe ApiCarto::CadastreAdapter do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it { expect { subject }.to raise_error(RestClient::ResourceNotFound) } it { expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) }
end end
end end

View file

@ -33,6 +33,6 @@ describe ApiCarto::QuartiersPrioritairesAdapter do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it { expect { subject }.to raise_error(RestClient::ResourceNotFound) } it { expect { subject }.to raise_error(ApiCarto::API::ResourceNotFound) }
end end
end end

View file

@ -16,8 +16,8 @@ describe ApiEntreprise::API do
let(:status) { 502 } let(:status) { 502 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') } let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
it 'raises RestClient::RequestFailed' do it 'raises ApiEntreprise::API::RequestFailed' do
expect { subject }.to raise_error(RestClient::RequestFailed) expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
end end
end end
@ -26,8 +26,8 @@ describe ApiEntreprise::API do
let(:status) { 404 } let(:status) { 404 }
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') }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiEntreprise::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
end end
end end
@ -36,8 +36,8 @@ describe ApiEntreprise::API do
let(:status) { 403 } let(:status) { 403 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_private.json') } let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_private.json') }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiEntreprise::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
end end
end end
@ -64,8 +64,8 @@ describe ApiEntreprise::API do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiEntreprise::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
end end
end end
@ -93,8 +93,8 @@ describe ApiEntreprise::API do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiEntreprise::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
end end
end end
@ -105,7 +105,7 @@ describe ApiEntreprise::API do
let(:status) { 200 } let(:status) { 200 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') } 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)) expect(subject).to eq(JSON.parse(body, symbolize_names: true))
end end
end end
@ -124,8 +124,8 @@ describe ApiEntreprise::API do
let(:status) { 404 } let(:status) { 404 }
let(:body) { '' } let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do it 'raises ApiEntreprise::API::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound) expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
end end
end end

View file

@ -84,7 +84,7 @@ describe ApiEntreprise::EntrepriseAdapter do
let(:status) { 502 } let(:status) { 502 }
it 'raises an exception' do it 'raises an exception' do
expect { subject }.to raise_error(RestClient::RequestFailed) expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
end end
end end
end end

View file

@ -66,8 +66,8 @@ describe ApiEntrepriseService do
let(:entreprises_status) { 400 } let(:entreprises_status) { 400 }
let(:entreprises_body) { '' } let(:entreprises_body) { '' }
it 'should raise RestClient::RequestFailed' do it 'should raise ApiEntreprise::API::RequestFailed' do
expect { result }.to raise_error(RestClient::RequestFailed) expect { result }.to raise_error(ApiEntreprise::API::RequestFailed)
end end
end end
@ -75,8 +75,8 @@ describe ApiEntrepriseService do
let(:etablissements_status) { 400 } let(:etablissements_status) { 400 }
let(:etablissements_body) { '' } let(:etablissements_body) { '' }
it 'should raise RestClient::RequestFailed' do it 'should raise ApiEntreprise::API::RequestFailed' do
expect { result }.to raise_error(RestClient::RequestFailed) expect { result }.to raise_error(ApiEntreprise::API::RequestFailed)
end end
end end