From a6f17a8b806596434af64a004a09d82ea0305ce6 Mon Sep 17 00:00:00 2001 From: Mike Moyer <87040148+mmoyer-va@users.noreply.github.com> Date: Fri, 7 Mar 2025 12:11:23 -0500 Subject: [PATCH] MHV-68159 Migration to new MHV API Gateway --- config/features.yml | 4 +++ .../client/concerns/mhv_jwt_session_client.rb | 9 +++++- lib/medical_records/client.rb | 19 ++++++++++-- lib/medical_records/configuration.rb | 6 +++- lib/medical_records/phr_mgr/client.rb | 6 ++-- lib/medical_records/phr_mgr/configuration.rb | 13 +++++++-- .../config/initializers/fhir_read_patch.rb | 29 +++++++++++++++++++ .../config/initializers/fhir_search_patch.rb | 5 ++-- 8 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 modules/my_health/config/initializers/fhir_read_patch.rb diff --git a/config/features.yml b/config/features.yml index bec2a6e15e9..6050141d22a 100644 --- a/config/features.yml +++ b/config/features.yml @@ -1043,6 +1043,10 @@ features: actor_type: user description: Show/hide content related to Vitals in Medical Records enable_in_development: true + mhv_medical_records_migrate_to_api_gateway: + actor_type: user + description: Enables the switch to the new MHV API Gateway endpoints + enable_in_development: true mhv_medical_records_phr_refresh_on_login: actor_type: user description: Enables/disables the PHR refresh for MHV users when logging into VA.gov diff --git a/lib/common/client/concerns/mhv_jwt_session_client.rb b/lib/common/client/concerns/mhv_jwt_session_client.rb index f2683f31f2c..6c8e9b397c8 100644 --- a/lib/common/client/concerns/mhv_jwt_session_client.rb +++ b/lib/common/client/concerns/mhv_jwt_session_client.rb @@ -78,7 +78,11 @@ def validate_session_params def get_session_tagged Sentry.set_tags(error: 'mhv_session') - env = perform(:post, '/mhvapi/security/v1/login', auth_body, auth_headers) + env = if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + perform(:post, '/v1/security/login', auth_body, auth_headers) + else + perform(:post, '/mhvapi/security/v1/login', auth_body, auth_headers) + end Sentry.get_current_scope.tags.delete(:error) env end @@ -93,6 +97,9 @@ def patient_fhir_id def auth_headers config.base_request_headers.merge('Content-Type' => 'application/json') + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + config.base_request_headers.merge('x-api-key' => Settings.mhv.medical_records.x_api_key) + end end def auth_body diff --git a/lib/medical_records/client.rb b/lib/medical_records/client.rb index c8dea6b5016..9ddfa08554b 100644 --- a/lib/medical_records/client.rb +++ b/lib/medical_records/client.rb @@ -14,7 +14,7 @@ class Client < Common::Client::Base include Common::Client::Concerns::MhvFhirSessionClient # Default number of records to request per call when searching - DEFAULT_COUNT = 9999 + DEFAULT_COUNT = 3000 # LOINC codes for clinical notes PHYSICIAN_PROCEDURE_NOTE = '11506-3' # Physician procedure note @@ -43,7 +43,11 @@ class Client < Common::Client::Base # @return [String] Base path for dependent URLs # def base_path - "#{Settings.mhv.medical_records.host}/fhir/" + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + "#{Settings.mhv.api_gateway.hosts.fhir}/fhir/" + else + "#{Settings.mhv.medical_records.host}/fhir/" + end end ## @@ -214,6 +218,10 @@ def fhir_search(fhir_model, params) # def fhir_search_query(fhir_model, params) default_headers = { 'Cache-Control': 'no-cache' } + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + default_headers.merge('x-api-key' => Settings.mhv.medical_records.x_api_key) + end + params[:headers] = default_headers.merge(params.fetch(:headers, {})) params[:search][:parameters].merge!(_count: DEFAULT_COUNT) @@ -224,7 +232,12 @@ def fhir_search_query(fhir_model, params) end def fhir_read(fhir_model, id) - result = fhir_client.read(fhir_model, id) + default_headers = {} + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + default_headers.merge!('x-api-key' => Settings.mhv.medical_records.x_api_key) + end + + result = fhir_client.read(fhir_model, id, nil, nil, { headers: default_headers }) handle_api_errors(result) if result.resource.nil? result.resource end diff --git a/lib/medical_records/configuration.rb b/lib/medical_records/configuration.rb index 46f5cbe4db6..a3ac7218bc2 100644 --- a/lib/medical_records/configuration.rb +++ b/lib/medical_records/configuration.rb @@ -26,7 +26,11 @@ def app_token # @return [String] Base path for dependent URLs # def base_path - Settings.mhv.medical_records.host + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + Settings.mhv.api_gateway.hosts.security + else + Settings.mhv.medical_records.host + end end ## diff --git a/lib/medical_records/phr_mgr/client.rb b/lib/medical_records/phr_mgr/client.rb index 2f4e8e49211..0f2ac00455f 100644 --- a/lib/medical_records/phr_mgr/client.rb +++ b/lib/medical_records/phr_mgr/client.rb @@ -28,7 +28,7 @@ def initialize(icn) # @return [Fixnum] Call status # def post_phrmgr_refresh - response = perform(:post, "refresh/#{@icn}", nil, self.class.configuration.x_auth_key_headers) + response = perform(:post, "refresh/#{@icn}", nil, self.class.configuration.phr_headers) # response_hash = JSON.parse(response.body) response&.status end @@ -39,7 +39,7 @@ def post_phrmgr_refresh # @return [Hash] Patient status # def get_phrmgr_status - response = perform(:get, "status/#{@icn}", nil, self.class.configuration.x_auth_key_headers) + response = perform(:get, "status/#{@icn}", nil, self.class.configuration.phr_headers) response.body end @@ -49,7 +49,7 @@ def get_phrmgr_status # @return - military service record in text format # def get_military_service(edipi) - headers = self.class.configuration.x_auth_key_headers.merge({ 'Accept' => 'text/plain' }) + headers = self.class.configuration.phr_headers.merge({ 'Accept' => 'text/plain' }) response = perform(:get, "dod/vaprofile/#{edipi}", nil, headers) response.body end diff --git a/lib/medical_records/phr_mgr/configuration.rb b/lib/medical_records/phr_mgr/configuration.rb index d13f0ceda91..1dfb6a1c675 100644 --- a/lib/medical_records/phr_mgr/configuration.rb +++ b/lib/medical_records/phr_mgr/configuration.rb @@ -19,14 +19,21 @@ class Configuration < Common::Client::Configuration::REST # @return [String] Base path for dependent URLs # def base_path - "#{Settings.mhv.medical_records.host}/mhvapi/v2/phrmgr/" + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + "#{Settings.mhv.api_gateway.hosts.phrmgr}/v2/phrmgr/" + else + "#{Settings.mhv.medical_records.host}/mhvapi/v2/phrmgr/" + end end ## - # @return [Hash] Headers with X-Authorization-Key header value for dependent URLs + # @return [Hash] Headers with X-Authorization-Key and x-api-key header values for dependent URLs # - def x_auth_key_headers + def phr_headers base_request_headers.merge('X-Authorization-Key' => Settings.mhv.medical_records.x_auth_key) + if Flipper.enabled?(:mhv_medical_records_migrate_to_api_gateway) + base_request_headers.merge('x-api-key' => Settings.mhv.medical_records.x_api_key) + end end ## diff --git a/modules/my_health/config/initializers/fhir_read_patch.rb b/modules/my_health/config/initializers/fhir_read_patch.rb new file mode 100644 index 00000000000..60fc8e3a953 --- /dev/null +++ b/modules/my_health/config/initializers/fhir_read_patch.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# Monkeypatch to allow read to take custom headers. This allows us to inject the x-api-key header +# when accessing FHIR via the MHV API Gateway. +# Patched version: department-of-veterans-affairs/fhir_client (fork of 5.0.3) +module FHIR + module Sections + module Crud + def read(klass, id, format = nil, summary = nil, options = {}) + options = { resource: klass, id:, format: format || @default_format }.merge(options) + options[:summary] = summary if summary + + # Build the default headers (using the provided format if any) + base_headers = {} + base_headers[:accept] = format.to_s if format + + # Grab any custom headers provided in the options + extra_headers = options[:headers] || {} + + # Merge custom headers into the default FHIR headers + reply = get resource_url(options), fhir_headers(base_headers).merge(extra_headers) + + reply.resource = parse_reply(klass, format || @default_format, reply) + reply.resource_class = klass + reply + end + end + end +end diff --git a/modules/my_health/config/initializers/fhir_search_patch.rb b/modules/my_health/config/initializers/fhir_search_patch.rb index ca58841b649..ab04b6900e4 100644 --- a/modules/my_health/config/initializers/fhir_search_patch.rb +++ b/modules/my_health/config/initializers/fhir_search_patch.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true # Monkeypatch to allow fhir_search to take custom headers. This is to allow us to bypass the FHIR -# server's cache when requesting a Patient resource. -# Patched version: fhir_client (5.0.3) +# server's cache when requesting a FHIR resource. It also allows us to inject the x-api-key header +# when accessing FHIR via the MHV API Gateway. +# Patched version: department-of-veterans-affairs/fhir_client (fork of 5.0.3) module FHIR module Sections module Search