diff --git a/app/lib/api_tchap/api.rb b/app/lib/api_tchap/api.rb new file mode 100644 index 000000000..0dec71d53 --- /dev/null +++ b/app/lib/api_tchap/api.rb @@ -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 diff --git a/app/lib/api_tchap/hs_adapter.rb b/app/lib/api_tchap/hs_adapter.rb new file mode 100644 index 000000000..cdf2aced2 --- /dev/null +++ b/app/lib/api_tchap/hs_adapter.rb @@ -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 diff --git a/config/initializers/02_urls.rb b/config/initializers/02_urls.rb index 12b2d2e2e..c238ade67 100644 --- a/config/initializers/02_urls.rb +++ b/config/initializers/02_urls.rb @@ -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") diff --git a/spec/lib/api_tchap/hs_adapter_spec.rb b/spec/lib/api_tchap/hs_adapter_spec.rb new file mode 100644 index 000000000..082435d9c --- /dev/null +++ b/spec/lib/api_tchap/hs_adapter_spec.rb @@ -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