feat(file retrieval): add api_recherche_entreprises_service

This commit is contained in:
simon lehericey 2023-12-09 17:43:24 +01:00
parent c1199af83d
commit a60a082ec5
5 changed files with 858 additions and 0 deletions

View file

@ -0,0 +1,35 @@
class APIRechercheEntreprisesService
include Dry::Monads[:result]
def self.collectivite_territoriale?(siret:)
response = APIRechercheEntreprisesService.new.call(siret:)
return false if response.failure?
response.success&.dig(:complements, :collectivite_territoriale).present?
end
def call(siret:)
result = API::Client.new.(url: "#{url}?q=#{siret}")
return result if result.failure?
body = result.success.body
return Success(nil) if body[:results].empty?
# the api returns the matching structure in the first element if it exists
structure = body[:results][0]
# safety check : the api does fuzzy matching, so we need to check that the siret matches
return Failure() if structure[:matching_etablissements].all? { _1[:siret] != siret }
Success(structure)
end
private
def url
"#{API_RECHERCHE_ENTREPRISE_URL}/search"
end
end