Class: BenefitsDocuments::Configuration

Inherits:
Common::Client::Configuration::REST show all
Defined in:
lib/lighthouse/benefits_documents/configuration.rb

Overview

HTTP client configuration for the BenefitsClaims::Service, sets the base path, the base request headers, and a service name for breakers and metrics.

Constant Summary collapse

SYSTEM_NAME =
'VA.gov'
API_SCOPES =
%w[documents.read documents.write].freeze
BASE_PATH =
'services/benefits-documents/v1'
DOCUMENTS_PATH =
"#{BASE_PATH}/documents".freeze
DOCUMENTS_STATUS_PATH =
"#{BASE_PATH}/uploads/status".freeze
TOKEN_PATH =
'oauth2/benefits-documents/system/v1/token'
QA_TESTING_DOMAIN =
Settings.lighthouse.benefits_documents.host

Instance Attribute Summary

Attributes inherited from Common::Client::Configuration::Base

#base_request_headers, #open_timeout, #read_timeout, #request_types, #user_agent

Instance Method Summary collapse

Methods inherited from Common::Client::Configuration::Base

#breakers_error_threshold, #breakers_exception_handler, #breakers_matcher, #breakers_service, #create_new_breakers_service, #current_module, #request_options, #service_exception

Instance Method Details

#access_token(lighthouse_client_id = nil, lighthouse_rsa_key_path = nil, options = {}) ⇒ Object (private)



148
149
150
151
152
153
154
155
156
157
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 148

def access_token(lighthouse_client_id = nil, lighthouse_rsa_key_path = nil, options = {})
  if get_access_token?
    token_service(
      lighthouse_client_id,
      lighthouse_rsa_key_path,
      options[:aud_claim_url],
      options[:host]
    ).get_token(options[:auth_params])
  end
end

#base_api_path(host = nil) ⇒ Object



44
45
46
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 44

def base_api_path(host = nil)
  "#{base_path(host)}/"
end

#base_path(host = nil) ⇒ String

Returns Base path for veteran_verification URLs.

Parameters:

  • host (String) (defaults to: nil)

    (optional): a configurable base url host if the client application does not want to use the default

Returns:

  • (String)

    Base path for veteran_verification URLs.



40
41
42
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 40

def base_path(host = nil)
  (host || documents_settings.host).to_s
end

#connection(api_path = base_api_path) ⇒ Faraday::Connection

Creates a Faraday connection with parsing json and breakers functionality.

Returns:

  • (Faraday::Connection)

    a Faraday connection instance.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 121

def connection(api_path = base_api_path)
  @conn ||= Faraday.new(api_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, content_type: /\bjson/
    faraday.adapter Faraday.default_adapter
  end
end

#documents_settingsConfig::Options

Returns Settings for benefits_claims API.

Returns:

  • (Config::Options)

    Settings for benefits_claims API.



27
28
29
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 27

def documents_settings
  Settings.lighthouse.benefits_documents
end

#documents_status_access_tokenObject (private)



174
175
176
177
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 174

def documents_status_access_token
  # Lighthouse requires the documents status endpoint be tested on the QA testing domain
  ENV['RAILS_ENV'] == 'test' ? access_token(nil, nil, { host:  QA_TESTING_DOMAIN }) : access_token
end

#documents_status_api_connectionObject (private)



179
180
181
182
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 179

def documents_status_api_connection
  # Lighthouse requires the documents status endpoint be tested on the QA testing domain
  ENV['RAILS_ENV'] == 'test' ? connection(QA_TESTING_DOMAIN) : connection
end

#generate_upload_body(document_data, file_body) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 72

def generate_upload_body(document_data, file_body)
  payload = {}
  data = {
    data: {
      systemName: SYSTEM_NAME,
      docType: document_data[:document_type],
      claimId: document_data[:claim_id],
      participantId: document_data[:participant_id],
      fileName: document_data[:file_name],
      # In theory one document can correspond to multiple tracked items
      # To do that, add multiple query parameters
      trackedItemIds: document_data[:tracked_item_id]
    }
  }

  payload[:parameters] = Faraday::Multipart::ParamPart.new(
    data.to_json,
    'application/json'
  )

  file = Tempfile.new(document_data[:file_name])
  File.write(file, file_body)

  mime_type = MimeMagic.by_path(document_data[:file_name]).type
  payload[:file] = Faraday::UploadIO.new(file, mime_type)

  payload
end

#get_access_token?Boolean (private)

Returns:

  • (Boolean)


144
145
146
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 144

def get_access_token?
  !use_mocks? || Settings.betamocks.recording
end

#get_documents_status(lighthouse_document_request_ids) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 101

def get_documents_status(lighthouse_document_request_ids)
  headers = {
    'Authorization' => "Bearer #{documents_status_access_token}",
    'Content-Type' => 'application/json'
  }

  body = {
    data: {
      requestIds: lighthouse_document_request_ids
    }
  }.to_json

  documents_status_api_connection.post(DOCUMENTS_STATUS_PATH, body, headers)
end

#global_settingsObject



31
32
33
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 31

def global_settings
  Settings.lighthouse.auth
end

#post(file_body, document_data, lighthouse_client_id = nil, lighthouse_rsa_key_path = nil, options = {}) ⇒ Faraday::Response

Returns response from POST request.

Returns:

  • (Faraday::Response)

    response from POST request



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 58

def post(file_body, document_data, lighthouse_client_id = nil, lighthouse_rsa_key_path = nil, options = {})
  headers = { 'Authorization' => "Bearer #{
    access_token(
      lighthouse_client_id,
      lighthouse_rsa_key_path,
      options
    )
  }",
              'Content-Type' => 'multipart/form-data' }

  body = generate_upload_body(document_data, file_body)
  connection.post(DOCUMENTS_PATH, body, headers)
end

#service_nameString

Returns Service name to use in breakers and metrics.

Returns:

  • (String)

    Service name to use in breakers and metrics.



51
52
53
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 51

def service_name
  'BenefitsDocuments'
end

#token_service(lighthouse_client_id, lighthouse_rsa_key_path, aud_claim_url = nil, host = nil) ⇒ BenefitsClaims::AccessToken::Service (private)

Returns Service used to generate access tokens.

Returns:

  • (BenefitsClaims::AccessToken::Service)

    Service used to generate access tokens.



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 162

def token_service(lighthouse_client_id, lighthouse_rsa_key_path, aud_claim_url = nil, host = nil)
  lighthouse_client_id = global_settings.ccg.client_id if lighthouse_client_id.nil?
  lighthouse_rsa_key_path = global_settings.ccg.rsa_key if lighthouse_rsa_key_path.nil?
  host ||= base_path(host)
  url = "#{host}/#{TOKEN_PATH}"
  aud_claim_url ||= documents_settings.access_token.aud_claim_url

  @token_service ||= Auth::ClientCredentials::Service.new(
    url, API_SCOPES, lighthouse_client_id, aud_claim_url, lighthouse_rsa_key_path, 'benefits-documents'
  )
end

#use_mocks?Boolean (private)

Returns Should the service use mock data in lower environments.

Returns:

  • (Boolean)

    Should the service use mock data in lower environments.



140
141
142
# File 'lib/lighthouse/benefits_documents/configuration.rb', line 140

def use_mocks?
  documents_settings.use_mocks || false
end