add tchap hs adapter

This commit is contained in:
Christophe Robillard 2023-03-31 17:19:02 +02:00
parent 3705dc2553
commit 53dd2955e4
4 changed files with 57 additions and 0 deletions

22
app/lib/api_tchap/api.rb Normal file
View file

@ -0,0 +1,22 @@
class APITchap::API
class ResourceNotFound < StandardError
end
def self.get_hs(email)
call([API_TCHAP_URL, "info?medium=email&address=#{email}"].join('/'))
end
private
def self.call(url)
response = Typhoeus.get(url)
if response.success?
response.body
else
message = response.code == 0 ? response.return_message : response.code.to_s
Rails.logger.error "[APITchap] Error on #{url}: #{message}"
raise ResourceNotFound
end
end
end

View file

@ -0,0 +1,15 @@
class APITchap::HsAdapter
def initialize(email)
@email = email
end
def to_hs
data_source[:hs]
end
private
def data_source
@data_source ||= JSON.parse(APITchap::API.get_hs(@email), symbolize_names: true)
end
end

View file

@ -5,6 +5,7 @@ API_ENTREPRISE_URL = ENV.fetch("API_ENTREPRISE_URL", "https://entreprise.api.gou
API_EDUCATION_URL = ENV.fetch("API_EDUCATION_URL", "https://data.education.gouv.fr/api/records/1.0")
API_GEO_URL = ENV.fetch("API_GEO_URL", "https://geo.api.gouv.fr")
API_PARTICULIER_URL = ENV.fetch("API_PARTICULIER_URL", "https://particulier.api.gouv.fr/api")
API_TCHAP_URL = ENV.fetch("API_TCHAP_URL", "https://matrix.agent.tchap.gouv.fr/_matrix/identity/api/v1")
HELPSCOUT_API_URL = ENV.fetch("HELPSCOUT_API_URL", "https://api.helpscout.net/v2")
PIPEDRIVE_API_URL = ENV.fetch("PIPEDRIVE_API_URL", "https://api.pipedrive.com/v1")
SENDINBLUE_API_URL = ENV.fetch("SENDINBLUE_API_URL", "https://in-automate.sendinblue.com/api/v2")

View file

@ -0,0 +1,19 @@
describe APITchap::HsAdapter do
let(:adapter) { described_class.new(email) }
let(:email) { "louise@mjc.gouv.fr" }
subject { adapter.to_hs }
before do
stub_request(:get, /https:\/\/matrix.agent.tchap.gouv.fr\/_matrix\/identity\/api\/v1\/info\?address=#{email}&medium=email/)
.to_return(body: body, status: status)
end
context 'with normal body' do
let(:body) { "{\"hs\": \"agent.educpop.gouv.fr\" }" }
let(:status) { 200 }
it 'returns hs' do
subject
expect(subject).to eq "agent.educpop.gouv.fr"
end
end
end