-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lighthouse Benefits Intake Configuration #16332
Merged
wayne-weibel
merged 2 commits into
master
from
feature/lighthouse-benefits-intake-config
Apr 16, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'common/client/configuration/rest' | ||
require 'faraday/multipart' | ||
|
||
module BenefitsIntake | ||
## | ||
mjknight50 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# HTTP client configuration for the {BenefitsIntake::Service}, | ||
# sets the base path, the base request headers, and a service name for breakers and metrics. | ||
# | ||
class Configuration < Common::Client::Configuration::REST | ||
self.read_timeout = Settings.lighthouse.benefits_intake.timeout || 20 | ||
|
||
## | ||
# @return [Config::Options] Settings for benefits_claims API. | ||
# | ||
def intake_settings | ||
Settings.lighthouse.benefits_intake | ||
end | ||
|
||
## | ||
# @return [String] Base path. | ||
# | ||
def service_path | ||
url = [intake_settings.host, intake_settings.path, intake_settings.version] | ||
url.map { |segment| segment.sub(%r{^/}, '').chomp('/') }.join('/') | ||
end | ||
|
||
## | ||
# @return [String] Service name to use in breakers and metrics. | ||
# | ||
def service_name | ||
'BenefitsIntake' | ||
end | ||
|
||
## | ||
# @return [Hash] The basic headers required for any Lighthouse API call | ||
# | ||
def self.base_request_headers | ||
key = Settings.lighthouse.benefits_intake.api_key | ||
raise "No api_key set for benefits_intake. Please set 'lighthouse.benefits_intake.api_key'" if key.nil? | ||
|
||
super.merge('apikey' => key) | ||
end | ||
|
||
## | ||
# Creates a connection with json parsing and breaker functionality. | ||
# | ||
# @return [Faraday::Connection] a Faraday connection instance. | ||
# | ||
def connection | ||
@conn ||= Faraday.new(service_path, headers: base_request_headers, request: request_options) do |faraday| | ||
faraday.use :breakers | ||
faraday.use Faraday::Response::RaiseError | ||
|
||
faraday.request :multipart | ||
faraday.request :json | ||
|
||
faraday.response :betamocks if use_mocks? | ||
faraday.response :json | ||
faraday.adapter Faraday.default_adapter | ||
end | ||
end | ||
|
||
## | ||
# @return [Boolean] Should the service use mock data in lower environments. | ||
# | ||
def use_mocks? | ||
intake_settings.use_mocks || false | ||
end | ||
|
||
def breakers_error_threshold | ||
80 # breakers will be tripped if error rate reaches 80% over a two minute period. | ||
end | ||
end | ||
end |
104 changes: 104 additions & 0 deletions
104
spec/lib/lighthouse/benefits_intake/configuration_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'common/client/configuration/rest' | ||
require 'lighthouse/benefits_intake/configuration' | ||
|
||
RSpec.describe BenefitsIntake::Configuration do | ||
let(:base) { Common::Client::Configuration::REST } | ||
let(:config) { BenefitsIntake::Configuration.send(:new) } | ||
let(:settings) do | ||
OpenStruct.new({ | ||
host: 'https://sandbox-api.va.gov', | ||
path: '/services/vba_documents', | ||
version: 'v1', | ||
use_mocks: false, | ||
api_key: 'some-long-hash-api-key' | ||
}) | ||
end | ||
|
||
before do | ||
allow(Settings.lighthouse).to receive(:benefits_intake).and_return(settings) | ||
end | ||
|
||
context 'valid settings' do | ||
it 'returns settings' do | ||
expect(config.intake_settings).to eq(settings) | ||
end | ||
|
||
it 'has correct api_key' do | ||
expect(config.intake_settings.api_key).to eq(settings.api_key) | ||
end | ||
|
||
it 'returns service_path' do | ||
valid_path = 'https://sandbox-api.va.gov/services/vba_documents/v1' | ||
expect(config.service_path).to eq(valid_path) | ||
end | ||
|
||
it 'returns use_mocks' do | ||
expect(config.use_mocks?).to eq(settings.use_mocks) | ||
end | ||
end | ||
|
||
context 'expected constants' do | ||
it 'returns service_name' do | ||
expect(config.service_name).to eq('BenefitsIntake') | ||
end | ||
|
||
it 'returns breakers_error_threshold' do | ||
expect(config.breakers_error_threshold).to eq(80) | ||
end | ||
end | ||
|
||
describe '#base_request_headers' do | ||
it 'returns expected headers' do | ||
headers = config.base_request_headers | ||
expected = base.base_request_headers.merge({ 'apikey' => settings.api_key }) | ||
expect(headers).to eq(expected) | ||
end | ||
|
||
it 'errors if missing api_key' do | ||
allow(Settings.lighthouse.benefits_intake).to receive(:api_key).and_return(nil) | ||
expect { config.base_request_headers }.to raise_error StandardError, /^No api_key set.+/ | ||
end | ||
end | ||
|
||
describe '#connection' do | ||
let(:faraday) { double('faraday') } | ||
|
||
before do | ||
allow(Faraday).to receive(:new).and_yield(faraday) | ||
|
||
allow(config).to receive(:service_path).and_return('service_path') | ||
allow(config).to receive(:base_request_headers).and_return('base_request_headers') | ||
allow(config).to receive(:request_options).and_return('request_options') | ||
allow(config).to receive(:use_mocks?).and_return(true) | ||
end | ||
|
||
it 'returns existing connection' do | ||
config.instance_variable_set(:@conn, 'TEST') | ||
|
||
expect(Faraday).not_to receive(:new) | ||
expect(config.connection).to eq('TEST') | ||
end | ||
|
||
it 'creates the connection' do | ||
expect(Faraday).to receive(:new).with('service_path', headers: 'base_request_headers', request: 'request_options') | ||
|
||
expect(faraday).to receive(:use).once.with(:breakers) | ||
expect(faraday).to receive(:use).once.with(Faraday::Response::RaiseError) | ||
|
||
expect(faraday).to receive(:request).once.with(:multipart) | ||
expect(faraday).to receive(:request).once.with(:json) | ||
|
||
expect(faraday).to receive(:response).once.with(:betamocks) # use_mocks? => true | ||
expect(faraday).to receive(:response).once.with(:json) | ||
|
||
expect(faraday).to receive(:adapter).once.with(Faraday.default_adapter) | ||
|
||
config.connection | ||
end | ||
end | ||
|
||
# end RSpec.describe | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you be adding this information to the manifest repo (and parameter store)? https://depo-platform-documentation.scrollhelp.site/developer-docs/vets-api-on-eks#VetsAPIonEKS-Secretvalues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rmtolmach Yes absolutely, wanted to get the library up for review before tackling the deployment (ie. needed to know what the configuration would be first)