Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add zipcode island archipelago to export + cache for polynesian_cities #9

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Continuous Integration
on:
push:
branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*', 'hotfix/*']
branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*', 'feature-ouidou/*', 'hotfix/*']
pull_request:
branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*']
branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*', 'feature-ouidou/*']
merge_group:
branches: ['main', 'devpf']

Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ ENV \
API_EDUCATION_URL="https://data.education.gouv.fr/api/records/1.0"\
API_ENTREPRISE_DEFAULT_SIRET=""\
API_ENTREPRISE_KEY=""\
API_ENTREPRISE_PF_AUTH=""\
API_ENTREPRISE_PF_URL=""\
API_ISPF_AUTH_URL=""\
API_ISPF_URL=""\
API_GEO_URL="https://geo.api.gouv.fr"\
API_ISPF_PASSWORD=""\
API_ISPF_USER=""\
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def individual_params
end

def siret_params
params.require(:user).permit(:siret)
params.require(:user).permit(:siret, :dossier_id)
end

def commentaire_params
Expand Down
31 changes: 5 additions & 26 deletions app/lib/api_entreprise/pf_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def entreprise(no_tahiti)
end

def self.api_up?
(200...500).cover?(Typhoeus.get(API_ENTREPRISE_PF_URL, timeout: 2, ssl_verifypeer: false, verbose: false).code)
(200...500).cover?(Typhoeus.get(API_ISPF_URL, timeout: 2, ssl_verifypeer: false, verbose: false).code)
end

private
Expand All @@ -20,11 +20,11 @@ def call(resource_name, no_tahiti)
url = url(resource_name)
params = params(no_tahiti)

parse_response_body(Typhoeus.get(url, params: params, timeout: TIMEOUT, ssl_verifypeer: false, verbose: false))
parse_response_body(Typhoeus.get(url, headers: headers, params: params, timeout: TIMEOUT, ssl_verifypeer: false, verbose: true))
end

def url(resource_name)
base_url = [API_ENTREPRISE_PF_URL, resource_name].join("/")
base_url = [API_ISPF_URL, resource_name].join("/")

base_url
end
Expand All @@ -36,25 +36,12 @@ def params(no_tahiti)
end

def headers
{
'Authorization': "Bearer #{access_token}",
@header ||= {
'X-Gravitee-Api-Key': Rails.application.secrets.api_ispf_entreprise[:gravitee],
'Content-Type': 'application/json; charset=UTF-8'
}
end

def access_token
if !@expires_at || Time.zone.now >= @expires_at
body = parse_response_body(fetch_access_token)
if (body[:error])
Rails.logger.error "Unable to connect to I-taiete : #{body[:error_description]}"
raise APIEntreprise::API::Error::ServiceUnavailable.new(response)
end
@access_token = body[:access_token]
@expires_at = Time.zone.now + body[:expires_in].seconds - 1.minute
end
@access_token
end

def parse_response_body(response)
if response.success?
JSON.parse(response.body, symbolize_names: true)
Expand All @@ -72,12 +59,4 @@ def parse_response_body(response)
raise APIEntreprise::API::Error::RequestFailed.new(response)
end
end

def user_password
[Rails.application.secrets.api_ispf_entreprise[:user], Rails.application.secrets.api_ispf_entreprise[:pwd]].join(':')
end

def fetch_access_token
Typhoeus.post(API_ENTREPRISE_PF_AUTH, body: { grant_type: 'client_credentials' }, userpwd: user_password)
end
end
40 changes: 25 additions & 15 deletions app/lib/api_geo/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ def self.nationalites
end

def self.polynesian_cities
result = []
headers = []
File.foreach('app/lib/api_geo/polynesian_postal_codes.txt', encoding: "windows-1252").with_index do |l, i|
fields = l.split("\t")
if i == 0
headers = fields.map { |f| f.gsub(/\s+/, "_").downcase.to_sym }
else
entry = {}
[1, 6, 7].each do |f|
entry[headers[f]] = fields[f]
Rails.cache.fetch('api_polynesian_cities', expires_in: 1.week) do
result = []
headers = []
File.foreach('app/lib/api_geo/polynesian_postal_codes.txt', encoding: "windows-1252").with_index do |l, i|
fields = l.split("\t")
if i == 0
headers = fields.map { |f| f.gsub(/\s+/, "_").downcase.to_sym }
else
entry = {}
[1, 6, 7].each do |f|
entry[headers[f]] = fields[f]
end
[2, 3].each do |f|
entry[headers[f]] = fields[f].to_i
end
result << entry
end
[2, 3].each do |f|
entry[headers[f]] = fields[f].to_i
end
result << entry
end
result
end
result
end

def self.codes_postaux_de_polynesie
Expand All @@ -45,6 +47,14 @@ def self.communes_de_polynesie
big_cities + ['------------------------'] + small_cities
end

def self.commune_by_city_postal_code(value)
polynesian_cities.find { city_postal_code(_1) == value }
end

def self.commune_by_postal_code_city_label(value)
polynesian_cities.find { postal_code_city_label(_1) == value }
end

private

def self.postal_code_city_label(e)
Expand Down
8 changes: 8 additions & 0 deletions app/models/champs/code_postal_de_polynesie_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
# type_de_champ_id :integer
#
class Champs::CodePostalDePolynesieChamp < Champs::TextChamp
def for_export
if value.present? && (postal_code_city_label = APIGeo::API.commune_by_postal_code_city_label(value))
[postal_code_city_label[:code_postal].to_s, postal_code_city_label[:commune], postal_code_city_label[:ile], postal_code_city_label[:archipel]]
else
['', '', '', '']
end
end

def self.options
APIGeo::API.codes_postaux_de_polynesie
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/champs/commune_de_polynesie_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
# type_de_champ_id :integer
#
class Champs::CommuneDePolynesieChamp < Champs::TextChamp
def for_export
if value.present? && (city_postal_code = APIGeo::API.commune_by_city_postal_code(value))
[city_postal_code[:commune], city_postal_code[:code_postal].to_s, city_postal_code[:ile], city_postal_code[:archipel]]
else
['', '', '', '']
end
end

def self.options
APIGeo::API.communes_de_polynesie
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class TypesDeChamp::CodePostalDePolynesieTypeDeChamp < TypesDeChamp::TextTypeDeChamp
def libelle_for_export(index)
[libelle, "#{libelle} (Commune)", "#{libelle} (Ile)", "#{libelle} (Archipel)"][index]
end
end
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class TypesDeChamp::CommuneDePolynesieTypeDeChamp < TypesDeChamp::TextTypeDeChamp
def libelle_for_export(index)
[libelle, "#{libelle} (Code postal)", "#{libelle} (Ile)", "#{libelle} (Archipel)"][index]
end
end
4 changes: 2 additions & 2 deletions config/initializers/02_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

API_TE_FENUA_URL = ENV.fetch("API_TE_FENUA_URL", "https://www.tefenua.gov.pf/api")

API_ENTREPRISE_PF_AUTH = ENV.fetch("API_ENTREPRISE_PF_AUTH", "https://auth.gov.pf/auth/realms/Itaiete/protocol/openid-connect/token")
API_ENTREPRISE_PF_URL = ENV.fetch("API_ENTREPRISE_PF_URL", "https://www.i-taiete.gov.pf/api/v2")
API_ISPF_AUTH_URL = ENV.fetch("API_ISPF_AUTH_URL", "https://auth.gov.pf/auth/realms/Itaiete/protocol/openid-connect/token")
API_ISPF_URL = ENV.fetch("API_ISPF_URL", "https://api.gov.pf/i-taiete")

API_CPS_AUTH = ENV.fetch("API_CPS_AUTH", "https://connect.cps.pf/auth/realms/TatouAssures/protocol/openid-connect/token")
API_CPS_URL = ENV.fetch("API_CPS_URL", "https://tatouapi.cps.pf")
Expand Down
1 change: 1 addition & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ defaults: &defaults
api_ispf_entreprise:
user: <%= ENV['API_ISPF_USER'] %>
pwd: <%= ENV['API_ISPF_PASSWORD'] %>
gravitee: <%= ENV['API_ISPF_GRAVITEE_KEY'] %>
api_cps:
username: <%= ENV['API_CPS_USERNAME'] %>
password: <%= ENV['API_CPS_PASSWORD'] %>
Expand Down
12 changes: 6 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ version: '3.3'
#
# APP_HOST: host:port pointing to the application to allows creation of absolute links
#
# API_ENTREPRISE_PF_AUTH keycloak URL to connect to iteiete realm
# API_ENTREPRISE_PF_URL itaiete url
# API_ISPF_AUTH_URL keycloak URL to connect to iteiete realm
# API_ISPF_URL itaiete url
# API_ISPF_USER: ISPF API configuration
# API_ISPF_PASSWORD
#
Expand Down Expand Up @@ -63,8 +63,8 @@ services:
- API_CPS_USERNAME
- API_EDUCATION_URL
- API_ENTREPRISE_DEFAULT_SIRET
- API_ENTREPRISE_PF_AUTH
- API_ENTREPRISE_PF_URL
- API_ISPF_AUTH_URL
- API_ISPF_URL
- API_GEO_URL
- API_ISPF_PASSWORD
- API_ISPF_USER
Expand Down Expand Up @@ -178,8 +178,8 @@ services:
- API_CPS_USERNAME
- API_EDUCATION_URL
- API_ENTREPRISE_DEFAULT_SIRET
- API_ENTREPRISE_PF_AUTH
- API_ENTREPRISE_PF_URL
- API_ISPF_AUTH_URL
- API_ISPF_URL
- APPLICATION_BASE_URL
- APPLICATION_NAME
- APP_HOST
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/users/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@
end

#----- Pf
stub_request(:get, Regexp.quote("#{API_ENTREPRISE_PF_URL}/etablissements/Entreprise/#{siret}"))
stub_request(:get, Regexp.quote("#{API_ISPF_URL}/etablissements/Entreprise/#{siret}"))
.to_return(status: api_etablissement_status, body: api_etablissement_body)

if api_current_status_response
has_issues = api_current_status_response.include?("502") || api_current_status_response.include?("HASISSUES")
stub_request(:get, API_ENTREPRISE_PF_URL).to_return(status: has_issues ? 502 : 200)
stub_request(:get, API_ISPF_URL).to_return(status: has_issues ? 502 : 200)
end
end

Expand Down
85 changes: 20 additions & 65 deletions spec/fixtures/cassettes/pf_api_entreprise.yml

Large diffs are not rendered by default.

76 changes: 21 additions & 55 deletions spec/fixtures/cassettes/pf_api_entreprise_not_found.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading