review(maj): strip la valeur recherché sur les api chorus avant de le soumettre à l'API, et quelques maj de style
Co-authored-by: Colin Darie <colin@darie.eu>
This commit is contained in:
parent
a4ef3cdf23
commit
0922e0987a
4 changed files with 38 additions and 48 deletions
|
@ -4,4 +4,12 @@ class Procedure::ChorusFormComponent < ApplicationComponent
|
||||||
def initialize(procedure:)
|
def initialize(procedure:)
|
||||||
@procedure = procedure
|
@procedure = procedure
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def map_attribute_to_autocomplete_endpoint
|
||||||
|
{
|
||||||
|
centre_de_coup: data_sources_search_centre_couts_path,
|
||||||
|
domaine_fonctionnel: data_sources_search_domaine_fonct_path,
|
||||||
|
referentiel_de_programmation: data_sources_search_ref_programmation_path
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
- map_attribute_to_autocomplete_endpoint = { centre_de_coup: data_sources_search_centre_couts_path, domaine_fonctionnel: data_sources_search_domaine_fonct_path, referentiel_de_programmation: data_sources_search_ref_programmation_path }
|
|
||||||
|
|
||||||
= form_for([procedure, procedure.chorus_configuration],url: admin_procedure_chorus_path(procedure), method: :put) do |f|
|
= form_for([procedure, procedure.chorus_configuration],url: admin_procedure_chorus_path(procedure), method: :put) do |f|
|
||||||
- map_attribute_to_autocomplete_endpoint.map do |chorus_configuration_attribute, datasource_endpoint|
|
- map_attribute_to_autocomplete_endpoint.map do |chorus_configuration_attribute, datasource_endpoint|
|
||||||
- label_class_name = "#{chorus_configuration_attribute}-label"
|
- label_class_name = "#{chorus_configuration_attribute}-label"
|
||||||
|
|
|
@ -2,48 +2,32 @@ class DataSources::ChorusController < ApplicationController
|
||||||
before_action :authenticate_administrateur!
|
before_action :authenticate_administrateur!
|
||||||
|
|
||||||
def search_domaine_fonct
|
def search_domaine_fonct
|
||||||
@result = APIBretagneService.new.search_domaine_fonct(code_or_label: params[:q])
|
result_json = APIBretagneService.new.search_domaine_fonct(code_or_label: params[:q])
|
||||||
result_json = @result.map do |item|
|
render json: format_result(result_json:,
|
||||||
{
|
label_formatter: ChorusConfiguration.method(:format_domaine_fonctionnel_label))
|
||||||
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
|
|
||||||
value: "#{item[:label]} - #{item[:code_programme]}",
|
|
||||||
data: item
|
|
||||||
}
|
|
||||||
end
|
|
||||||
render json: result_json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_centre_couts
|
def search_centre_couts
|
||||||
@result = APIBretagneService.new.search_centre_couts(code_or_label: params[:q])
|
result_json = APIBretagneService.new.search_centre_couts(code_or_label: params[:q])
|
||||||
result_json = @result.map do |item|
|
render json: format_result(result_json:,
|
||||||
{
|
label_formatter: ChorusConfiguration.method(:format_centre_de_coup_label))
|
||||||
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
|
|
||||||
value: "#{item[:label]} - #{item[:code_programme]}",
|
|
||||||
data: item
|
|
||||||
}
|
|
||||||
end
|
|
||||||
render json: result_json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_ref_programmation
|
def search_ref_programmation
|
||||||
@result = APIBretagneService.new.search_ref_programmation(code_or_label: params[:q])
|
result_json = APIBretagneService.new.search_ref_programmation(code_or_label: params[:q])
|
||||||
result_json = @result.map do |item|
|
render json: format_result(result_json:,
|
||||||
|
label_formatter: ChorusConfiguration.method(:format_ref_programmation_label))
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def format_result(result_json:, label_formatter:)
|
||||||
|
result_json.map do |item|
|
||||||
{
|
{
|
||||||
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
|
label: label_formatter.call(item),
|
||||||
value: "#{item[:label]} - #{item[:code_programme]}",
|
value: "#{item[:label]} - #{item[:code_programme]}",
|
||||||
data: item
|
data: item
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
render json: result_json
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# def search
|
|
||||||
# if params[:q].present? && params[:q].length > 3
|
|
||||||
# response = Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 })
|
|
||||||
# result = JSON.parse(response.body, symbolize_names: true)
|
|
||||||
# render json: result[:features].map { { label: _1[:properties][:label], value: _1[:properties][:label] } }
|
|
||||||
# else
|
|
||||||
# render json: []
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,33 +13,33 @@ class APIBretagneService
|
||||||
}
|
}
|
||||||
|
|
||||||
def search_domaine_fonct(code_or_label: "")
|
def search_domaine_fonct(code_or_label: "")
|
||||||
url = build_url(ENDPOINTS.fetch('domaine-fonct'))
|
request(endpoint: ENDPOINTS.fetch('domaine-fonct'), code_or_label:)
|
||||||
return [] if code_or_label.size < 3
|
|
||||||
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_centre_couts(code_or_label: "")
|
def search_centre_couts(code_or_label: "")
|
||||||
url = build_url(ENDPOINTS.fetch('centre-couts'))
|
request(endpoint: ENDPOINTS.fetch('centre-couts'), code_or_label:)
|
||||||
return [] if code_or_label.size < 3
|
|
||||||
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_ref_programmation(code_or_label: "")
|
def search_ref_programmation(code_or_label: "")
|
||||||
url = build_url(ENDPOINTS.fetch('ref-programmation'))
|
request(endpoint: ENDPOINTS.fetch('ref-programmation'), code_or_label:)
|
||||||
return [] if code_or_label.size < 3
|
|
||||||
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def fetch_page(url:, params:, retry_count: 1)
|
def request(endpoint:, code_or_label:)
|
||||||
|
return [] if (code_or_label || "").strip.size < 3
|
||||||
|
url = build_url(endpoint)
|
||||||
|
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_page(url:, params:, remaining_retry_count: 1)
|
||||||
result = call(url:, params:)
|
result = call(url:, params:)
|
||||||
|
|
||||||
case result
|
case result
|
||||||
in Failure(code:, reason:) if code.in?(401..403)
|
in Failure(code:, reason:) if code.in?(401..403)
|
||||||
if retry_count > 0
|
if remaining_retry_count > 0
|
||||||
login
|
login
|
||||||
fetch_page(url:, params:, retry_count: 0)
|
fetch_page(url:, params:, remaining_retry_count: 0)
|
||||||
else
|
else
|
||||||
fail "APIBretagneService, #{reason} #{code}"
|
fail "APIBretagneService, #{reason} #{code}"
|
||||||
end
|
end
|
||||||
|
@ -72,7 +72,7 @@ class APIBretagneService
|
||||||
result = API::Client.new.call(url: build_url(ENDPOINTS.fetch("login")),
|
result = API::Client.new.call(url: build_url(ENDPOINTS.fetch("login")),
|
||||||
json: {
|
json: {
|
||||||
email: ENV['API_DATABRETAGE_USERNAME'],
|
email: ENV['API_DATABRETAGE_USERNAME'],
|
||||||
password: ENV['API_DATABRETAGE_PASSWORD']
|
password: ENV['API_DATABRETAGE_PASSWORD']
|
||||||
},
|
},
|
||||||
method: :post)
|
method: :post)
|
||||||
case result
|
case result
|
||||||
|
|
Loading…
Reference in a new issue