Skip to content

Commit

Permalink
Use api.gov.pf (gravity) to access i-taiete
Browse files Browse the repository at this point in the history
  • Loading branch information
maatinito committed Nov 6, 2023
1 parent cc04903 commit 1850eef
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 161 deletions.
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
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.

4 changes: 2 additions & 2 deletions spec/services/api_entreprise_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@

context "when api entreprise is up" do
before do
stub_request(:get, "https://www.i-taiete.gov.pf/api/v2").to_return(body: body, status: status)
stub_request(:get, API_ISPF_URL).to_return(body: body, status: status)
end

it "returns true" do
Expand All @@ -135,7 +135,7 @@

context "when api entreprise is down" do
before do
stub_request(:get, "https://www.i-taiete.gov.pf/api/v2").to_timeout
stub_request(:get, API_ISPF_URL).to_timeout
end

it "returns false" do
Expand Down
1 change: 1 addition & 0 deletions spec/support/vcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
c.cassette_library_dir = 'spec/fixtures/cassettes'
c.configure_rspec_metadata!
c.ignore_hosts 'test.host', 'chromedriver.storage.googleapis.com'
c.filter_sensitive_data("<GRAVITEE>") { Rails.application.secrets.api_ispf_entreprise[:gravitee] }
end

0 comments on commit 1850eef

Please sign in to comment.