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:
mfo 2023-10-19 15:19:23 +02:00 committed by Martin
parent a4ef3cdf23
commit 0922e0987a
4 changed files with 38 additions and 48 deletions

View file

@ -4,4 +4,12 @@ class Procedure::ChorusFormComponent < ApplicationComponent
def initialize(procedure:)
@procedure = procedure
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

View file

@ -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|
- map_attribute_to_autocomplete_endpoint.map do |chorus_configuration_attribute, datasource_endpoint|
- label_class_name = "#{chorus_configuration_attribute}-label"

View file

@ -2,48 +2,32 @@ class DataSources::ChorusController < ApplicationController
before_action :authenticate_administrateur!
def search_domaine_fonct
@result = APIBretagneService.new.search_domaine_fonct(code_or_label: params[:q])
result_json = @result.map do |item|
{
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
value: "#{item[:label]} - #{item[:code_programme]}",
data: item
}
end
render json: result_json
result_json = APIBretagneService.new.search_domaine_fonct(code_or_label: params[:q])
render json: format_result(result_json:,
label_formatter: ChorusConfiguration.method(:format_domaine_fonctionnel_label))
end
def search_centre_couts
@result = APIBretagneService.new.search_centre_couts(code_or_label: params[:q])
result_json = @result.map do |item|
{
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
value: "#{item[:label]} - #{item[:code_programme]}",
data: item
}
end
render json: result_json
result_json = APIBretagneService.new.search_centre_couts(code_or_label: params[:q])
render json: format_result(result_json:,
label_formatter: ChorusConfiguration.method(:format_centre_de_coup_label))
end
def search_ref_programmation
@result = APIBretagneService.new.search_ref_programmation(code_or_label: params[:q])
result_json = @result.map do |item|
result_json = APIBretagneService.new.search_ref_programmation(code_or_label: params[:q])
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]}",
data: item
}
end
render json: result_json
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

View file

@ -13,33 +13,33 @@ class APIBretagneService
}
def search_domaine_fonct(code_or_label: "")
url = build_url(ENDPOINTS.fetch('domaine-fonct'))
return [] if code_or_label.size < 3
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
request(endpoint: ENDPOINTS.fetch('domaine-fonct'), code_or_label:)
end
def search_centre_couts(code_or_label: "")
url = build_url(ENDPOINTS.fetch('centre-couts'))
return [] if code_or_label.size < 3
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
request(endpoint: ENDPOINTS.fetch('centre-couts'), code_or_label:)
end
def search_ref_programmation(code_or_label: "")
url = build_url(ENDPOINTS.fetch('ref-programmation'))
return [] if code_or_label.size < 3
fetch_page(url:, params: { query: code_or_label, page_number: 1 })[:items] || []
request(endpoint: ENDPOINTS.fetch('ref-programmation'), code_or_label:)
end
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:)
case result
in Failure(code:, reason:) if code.in?(401..403)
if retry_count > 0
if remaining_retry_count > 0
login
fetch_page(url:, params:, retry_count: 0)
fetch_page(url:, params:, remaining_retry_count: 0)
else
fail "APIBretagneService, #{reason} #{code}"
end
@ -72,7 +72,7 @@ class APIBretagneService
result = API::Client.new.call(url: build_url(ENDPOINTS.fetch("login")),
json: {
email: ENV['API_DATABRETAGE_USERNAME'],
password: ENV['API_DATABRETAGE_PASSWORD']
password: ENV['API_DATABRETAGE_PASSWORD']
},
method: :post)
case result