Skip to content

Commit

Permalink
review(maj): strip la valeur recherché sur les api chorus avant de le…
Browse files Browse the repository at this point in the history
… soumettre à l'API, et quelques maj de style

Co-authored-by: Colin Darie <colin@darie.eu>
  • Loading branch information
2 people authored and Martin committed Oct 20, 2023
1 parent a4ef3cd commit 2e831c1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
8 changes: 8 additions & 0 deletions app/components/procedure/chorus_form_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
50 changes: 17 additions & 33 deletions app/controllers/data_sources/chorus_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions app/services/api_bretagne_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:, code_or_label:)
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
Expand Down Expand Up @@ -72,7 +72,7 @@ def login
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
Expand Down

0 comments on commit 2e831c1

Please sign in to comment.