Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-farsi committed May 27, 2024
1 parent 9bc3367 commit afbb592
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 4 deletions.
81 changes: 77 additions & 4 deletions app/bridges/hubee_cert_dc_bridge.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class HubeeCertDCBridge < ApplicationBridge
class HubEECertDCBridge < ApplicationBridge

def perform
@authorization_request = @authorization_request.decorate
Expand All @@ -18,7 +18,7 @@ def perform
validated_at,
scopes
)
@authorization_request.update({ linked_token_manager_id: linked_token_manager_id })
# @authorization_request.update({ linked_token_manager_id: linked_token_manager_id })
end

private
Expand All @@ -31,15 +31,16 @@ def create_enrollment_in_token_manager(
validated_at,
scopes
)
response = ApiSirene.call(siret)
etablissement = INSEESireneAPIClient.new.etablissement(siret: siret)["etablissement"]

response = format_etablissement(etablissement)

denomination = response[:denomination]
sigle = response[:sigle]
code_postal = response[:code_postal]
code_commune = response[:code_commune]
libelle_commune = response[:libelle_commune]

hubee_configuration = Credentials.get(:hubee)
api_host = hubee_configuration[:host]
hubee_auth_url = hubee_configuration[:auth_url]
client_id = hubee_configuration[:client_id]
Expand Down Expand Up @@ -132,4 +133,76 @@ def create_enrollment_in_token_manager(

subscription_ids.join(",")
end

def format_etablissement(etablissement)
is_diffusable = etablissement["statutDiffusionEtablissement"] == "O"

last_periode_etablissement = etablissement["periodesEtablissement"][0]
etat_administratif = last_periode_etablissement["etatAdministratifEtablissement"]

if etat_administratif != "A" # || !is_diffusable
return {
nom_raison_sociale: nil,
siret: @siret,
denomination: nil,
sigle: nil,
adresse: nil,
code_postal: nil,
code_commune: nil,
libelle_commune: nil,
activite_principale: nil,
activite_principale_label: nil,
categorie_juridique: nil,
categorie_juridique_label: nil,
etat_administratif: etat_administratif
}
end

unite_legale = etablissement["uniteLegale"]
adresse_etablissement = etablissement["adresseEtablissement"]

nom_raison_sociale = unite_legale["denominationUniteLegale"]
nom_raison_sociale ||= last_periode_etablissement["denominationUsuelleEtablissement"]
nom = unite_legale["nomUniteLegale"]
prenom_1 = unite_legale["prenom1UniteLegale"]
prenom_2 = unite_legale["prenom2UniteLegale"]
prenom_3 = unite_legale["prenom3UniteLegale"]
prenom_4 = unite_legale["prenom4UniteLegale"]
nom_raison_sociale ||= [prenom_1, prenom_2, prenom_3, prenom_4, nom].reject(&:nil?).join(" ")

numero_voie = adresse_etablissement["numeroVoieEtablissement"]
indice_repetition = adresse_etablissement["indiceRepetitionEtablissement"]
type_voie = adresse_etablissement["typeVoieEtablissement"]
libelle_voie = adresse_etablissement["libelleVoieEtablissement"]
adresse = [numero_voie, indice_repetition, type_voie, libelle_voie].reject(&:nil?).join(" ")

denomination = unite_legale["denominationUniteLegale"]
sigle = unite_legale["sigleUniteLegale"]
code_postal = adresse_etablissement["codePostalEtablissement"]
code_commune = adresse_etablissement["codeCommuneEtablissement"]
libelle_commune = adresse_etablissement["libelleCommuneEtablissement"]
activite_principale = last_periode_etablissement["activitePrincipaleEtablissement"]
activite_principale ||= unite_legale["activitePrincipaleUniteLegale"]
activite_principale_label = CodeNAF.find(activite_principale.delete(".")).libelle
categorie_juridique = unite_legale["categorieJuridiqueUniteLegale"]
categorie_juridique_label = CategorieJuridique.where(code: categorie_juridique).first.libelle

{
nom_raison_sociale: nom_raison_sociale,
siret: @authorization_request.organization[:siret],
denomination: denomination,
sigle: sigle,
adresse: adresse,
code_postal: code_postal,
code_commune: code_commune,
libelle_commune: libelle_commune,
activite_principale: activite_principale,
activite_principale_label: activite_principale_label,
categorie_juridique: categorie_juridique,
categorie_juridique_label: categorie_juridique_label,
etat_administratif: etat_administratif
}
end

def hubee_configuration = Rails.application.credentials.hubee
end
86 changes: 86 additions & 0 deletions lib/http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require "singleton"

class Http
include Singleton

def request(http_verb, options = {})
url = options.fetch(:url)
default_auth_header = "Authorization"
auth_header = options.fetch(:auth_header, default_auth_header)
auth_method = if auth_header == default_auth_header
options.fetch(:use_basic_auth_method, false) ? "Basic " : "Bearer "
else
""
end
use_form_content_type = options.fetch(:use_form_content_type, false)
api_key = options.fetch(:api_key, "") || ""
body = options.fetch(:body, {})
tag = options.fetch(:tag)
timeout = options.fetch(:timeout, 30)

http = Faraday.new do |conn|
conn.request :retry, max: 5
conn.response :raise_error
conn.response :json
conn.options.timeout = timeout
end

http_with_auth =
if api_key.empty?
http
else
http.request :authorization, auth_header, -> { "#{auth_method}#{api_key}" }
http
end

pp ?1*100, "#{auth_method}#{api_key}"

response = if body.empty?
http_with_auth
.send(http_verb, url)
elsif use_form_content_type
http_with_auth
.send(http_verb, url, form: body)
else
http_with_auth
.send(http_verb, url)
end

unless response.status.success?
raise ApplicationController::BadGateway.new(
tag,
url,
response.code,
response.to_s
)
end

response
# rescue HTTP::Error => e
# raise ApplicationController::BadGateway.new(
# tag,
# url,
# nil,
# nil
# ), e.message
# rescue OpenSSL::SSL::SSLError => e
# raise ApplicationController::BadGateway.new(
# tag,
# url,
# nil,
# nil
# ), e.message
end

def get(options)
request(:get, options)
end

def post(options)
request(:post, options)
end

def patch(options)
request(:patch, options)
end
end

0 comments on commit afbb592

Please sign in to comment.