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 'rake-progressbar', require: false
gem 'react-rails'
gem 'rest-client'
gem 'rgeo-geojson'
gem 'sanitize-url'
gem 'sassc-rails' # Use SCSS for stylesheets

View file

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

View file

@ -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

View file

@ -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?

View file

@ -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?

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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' }

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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