Class: MedicalCopays::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common::Client::Concerns::Monitoring
Defined in:
app/services/medical_copays/request.rb

Overview

An object responsible for making HTTP calls to the VBS service

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.medical_copays.request'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Constructor Details

#initializeRequest

Returns a new instance of Request.



34
35
36
# File 'app/services/medical_copays/request.rb', line 34

def initialize
  @settings = endpoint_settings
end

Instance Attribute Details

#hostObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/medical_copays/request.rb', line 15

class Request
  extend Forwardable
  include Common::Client::Concerns::Monitoring

  STATSD_KEY_PREFIX = 'api.medical_copays.request'

  attr_reader :settings

  def_delegators :settings, :base_path, :host, :service_name, :url

  ##
  # Builds a MedicalCopays::Request instance
  #
  # @return [MedicalCopays::Request] an instance of this class
  #
  def self.build
    new
  end

  def initialize
    @settings = endpoint_settings
  end

  ##
  # Make a HTTP POST call to the VBS service in order to obtain user copays
  #
  # @param path [String]
  # @param params [Hash]
  #
  # @return [Faraday::Response]
  #
  def post(path, params)
    if Flipper.enabled?(:debts_copay_logging) && !Rails.env.development?
      with_monitoring_and_error_handling do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    else
      with_monitoring do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    end
  end

  def with_monitoring_and_error_handling(&)
    with_monitoring(2, &)
  rescue => e
    handle_error(e)
  end

  def handle_error(error)
    Rails.logger.error("MedicalCopays::Request error: #{error.message}")
    raise error
  end

  ##
  # Make a HTTP GET call to the VBS service in order to obtain copays or PDFs by id
  #
  # @param path [String]
  #
  # @return [Faraday::Response]
  #
  def get(path)
    with_monitoring do
      connection.get(path)
    end
  end

  ##
  # Create a connection object that manages the attributes
  # and the middleware stack for making our HTTP requests to VBS
  #
  # @return [Faraday::Connection]
  #
  def connection
    Faraday.new(url:, headers:, request: request_options) do |conn|
      conn.request :json
      conn.use :breakers
      conn.use Faraday::Response::RaiseError
      conn.response :raise_custom_error, error_prefix: service_name
      conn.response :json
      conn.response :betamocks if mock_enabled?
      conn.adapter Faraday.default_adapter
    end
  end

  ##
  # Request options can be passed to the connection constructor
  # and will be applied to all requests. This is the Common::Client config
  # see: lib/common/client/configuration/base.rb
  #
  # open_timeout: The max number of seconds to wait for the connection to be established.
  # time_out: The max number of seconds to wait for the request to complete.
  #
  # @return [Hash]
  def request_options
    {
      open_timeout: 15,
      timeout: 50
    }
  end

  ##
  # HTTP request headers for the VBS API
  #
  # @return [Hash]
  #
  def headers
    {
      'Host' => host,
      'Content-Type' => 'application/json',
      api_key => settings.api_key
    }
  end

  ##
  # Betamocks enabled status from settings
  #
  # @return [Boolean]
  #
  def mock_enabled?
    settings.mock || false
  end

  private

  def api_key
    Flipper.enabled?(:medical_copays_api_key_change) ? 'apiKey' : 'x-api-key'
  end

  def endpoint_settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

#service_nameObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/medical_copays/request.rb', line 15

class Request
  extend Forwardable
  include Common::Client::Concerns::Monitoring

  STATSD_KEY_PREFIX = 'api.medical_copays.request'

  attr_reader :settings

  def_delegators :settings, :base_path, :host, :service_name, :url

  ##
  # Builds a MedicalCopays::Request instance
  #
  # @return [MedicalCopays::Request] an instance of this class
  #
  def self.build
    new
  end

  def initialize
    @settings = endpoint_settings
  end

  ##
  # Make a HTTP POST call to the VBS service in order to obtain user copays
  #
  # @param path [String]
  # @param params [Hash]
  #
  # @return [Faraday::Response]
  #
  def post(path, params)
    if Flipper.enabled?(:debts_copay_logging) && !Rails.env.development?
      with_monitoring_and_error_handling do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    else
      with_monitoring do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    end
  end

  def with_monitoring_and_error_handling(&)
    with_monitoring(2, &)
  rescue => e
    handle_error(e)
  end

  def handle_error(error)
    Rails.logger.error("MedicalCopays::Request error: #{error.message}")
    raise error
  end

  ##
  # Make a HTTP GET call to the VBS service in order to obtain copays or PDFs by id
  #
  # @param path [String]
  #
  # @return [Faraday::Response]
  #
  def get(path)
    with_monitoring do
      connection.get(path)
    end
  end

  ##
  # Create a connection object that manages the attributes
  # and the middleware stack for making our HTTP requests to VBS
  #
  # @return [Faraday::Connection]
  #
  def connection
    Faraday.new(url:, headers:, request: request_options) do |conn|
      conn.request :json
      conn.use :breakers
      conn.use Faraday::Response::RaiseError
      conn.response :raise_custom_error, error_prefix: service_name
      conn.response :json
      conn.response :betamocks if mock_enabled?
      conn.adapter Faraday.default_adapter
    end
  end

  ##
  # Request options can be passed to the connection constructor
  # and will be applied to all requests. This is the Common::Client config
  # see: lib/common/client/configuration/base.rb
  #
  # open_timeout: The max number of seconds to wait for the connection to be established.
  # time_out: The max number of seconds to wait for the request to complete.
  #
  # @return [Hash]
  def request_options
    {
      open_timeout: 15,
      timeout: 50
    }
  end

  ##
  # HTTP request headers for the VBS API
  #
  # @return [Hash]
  #
  def headers
    {
      'Host' => host,
      'Content-Type' => 'application/json',
      api_key => settings.api_key
    }
  end

  ##
  # Betamocks enabled status from settings
  #
  # @return [Boolean]
  #
  def mock_enabled?
    settings.mock || false
  end

  private

  def api_key
    Flipper.enabled?(:medical_copays_api_key_change) ? 'apiKey' : 'x-api-key'
  end

  def endpoint_settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

#settingsConfig::Options

Returns:

  • (Config::Options)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/medical_copays/request.rb', line 15

class Request
  extend Forwardable
  include Common::Client::Concerns::Monitoring

  STATSD_KEY_PREFIX = 'api.medical_copays.request'

  attr_reader :settings

  def_delegators :settings, :base_path, :host, :service_name, :url

  ##
  # Builds a MedicalCopays::Request instance
  #
  # @return [MedicalCopays::Request] an instance of this class
  #
  def self.build
    new
  end

  def initialize
    @settings = endpoint_settings
  end

  ##
  # Make a HTTP POST call to the VBS service in order to obtain user copays
  #
  # @param path [String]
  # @param params [Hash]
  #
  # @return [Faraday::Response]
  #
  def post(path, params)
    if Flipper.enabled?(:debts_copay_logging) && !Rails.env.development?
      with_monitoring_and_error_handling do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    else
      with_monitoring do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    end
  end

  def with_monitoring_and_error_handling(&)
    with_monitoring(2, &)
  rescue => e
    handle_error(e)
  end

  def handle_error(error)
    Rails.logger.error("MedicalCopays::Request error: #{error.message}")
    raise error
  end

  ##
  # Make a HTTP GET call to the VBS service in order to obtain copays or PDFs by id
  #
  # @param path [String]
  #
  # @return [Faraday::Response]
  #
  def get(path)
    with_monitoring do
      connection.get(path)
    end
  end

  ##
  # Create a connection object that manages the attributes
  # and the middleware stack for making our HTTP requests to VBS
  #
  # @return [Faraday::Connection]
  #
  def connection
    Faraday.new(url:, headers:, request: request_options) do |conn|
      conn.request :json
      conn.use :breakers
      conn.use Faraday::Response::RaiseError
      conn.response :raise_custom_error, error_prefix: service_name
      conn.response :json
      conn.response :betamocks if mock_enabled?
      conn.adapter Faraday.default_adapter
    end
  end

  ##
  # Request options can be passed to the connection constructor
  # and will be applied to all requests. This is the Common::Client config
  # see: lib/common/client/configuration/base.rb
  #
  # open_timeout: The max number of seconds to wait for the connection to be established.
  # time_out: The max number of seconds to wait for the request to complete.
  #
  # @return [Hash]
  def request_options
    {
      open_timeout: 15,
      timeout: 50
    }
  end

  ##
  # HTTP request headers for the VBS API
  #
  # @return [Hash]
  #
  def headers
    {
      'Host' => host,
      'Content-Type' => 'application/json',
      api_key => settings.api_key
    }
  end

  ##
  # Betamocks enabled status from settings
  #
  # @return [Boolean]
  #
  def mock_enabled?
    settings.mock || false
  end

  private

  def api_key
    Flipper.enabled?(:medical_copays_api_key_change) ? 'apiKey' : 'x-api-key'
  end

  def endpoint_settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

#urlObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/services/medical_copays/request.rb', line 15

class Request
  extend Forwardable
  include Common::Client::Concerns::Monitoring

  STATSD_KEY_PREFIX = 'api.medical_copays.request'

  attr_reader :settings

  def_delegators :settings, :base_path, :host, :service_name, :url

  ##
  # Builds a MedicalCopays::Request instance
  #
  # @return [MedicalCopays::Request] an instance of this class
  #
  def self.build
    new
  end

  def initialize
    @settings = endpoint_settings
  end

  ##
  # Make a HTTP POST call to the VBS service in order to obtain user copays
  #
  # @param path [String]
  # @param params [Hash]
  #
  # @return [Faraday::Response]
  #
  def post(path, params)
    if Flipper.enabled?(:debts_copay_logging) && !Rails.env.development?
      with_monitoring_and_error_handling do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    else
      with_monitoring do
        connection.post(path) do |req|
          req.body = Oj.dump(params)
        end
      end
    end
  end

  def with_monitoring_and_error_handling(&)
    with_monitoring(2, &)
  rescue => e
    handle_error(e)
  end

  def handle_error(error)
    Rails.logger.error("MedicalCopays::Request error: #{error.message}")
    raise error
  end

  ##
  # Make a HTTP GET call to the VBS service in order to obtain copays or PDFs by id
  #
  # @param path [String]
  #
  # @return [Faraday::Response]
  #
  def get(path)
    with_monitoring do
      connection.get(path)
    end
  end

  ##
  # Create a connection object that manages the attributes
  # and the middleware stack for making our HTTP requests to VBS
  #
  # @return [Faraday::Connection]
  #
  def connection
    Faraday.new(url:, headers:, request: request_options) do |conn|
      conn.request :json
      conn.use :breakers
      conn.use Faraday::Response::RaiseError
      conn.response :raise_custom_error, error_prefix: service_name
      conn.response :json
      conn.response :betamocks if mock_enabled?
      conn.adapter Faraday.default_adapter
    end
  end

  ##
  # Request options can be passed to the connection constructor
  # and will be applied to all requests. This is the Common::Client config
  # see: lib/common/client/configuration/base.rb
  #
  # open_timeout: The max number of seconds to wait for the connection to be established.
  # time_out: The max number of seconds to wait for the request to complete.
  #
  # @return [Hash]
  def request_options
    {
      open_timeout: 15,
      timeout: 50
    }
  end

  ##
  # HTTP request headers for the VBS API
  #
  # @return [Hash]
  #
  def headers
    {
      'Host' => host,
      'Content-Type' => 'application/json',
      api_key => settings.api_key
    }
  end

  ##
  # Betamocks enabled status from settings
  #
  # @return [Boolean]
  #
  def mock_enabled?
    settings.mock || false
  end

  private

  def api_key
    Flipper.enabled?(:medical_copays_api_key_change) ? 'apiKey' : 'x-api-key'
  end

  def endpoint_settings
    Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
  end
end

Class Method Details

.buildMedicalCopays::Request

Builds a MedicalCopays::Request instance

Returns:



30
31
32
# File 'app/services/medical_copays/request.rb', line 30

def self.build
  new
end

Instance Method Details

#api_keyObject (private)



144
145
146
# File 'app/services/medical_copays/request.rb', line 144

def api_key
  Flipper.enabled?(:medical_copays_api_key_change) ? 'apiKey' : 'x-api-key'
end

#connectionFaraday::Connection

Create a connection object that manages the attributes and the middleware stack for making our HTTP requests to VBS

Returns:

  • (Faraday::Connection)


92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/medical_copays/request.rb', line 92

def connection
  Faraday.new(url:, headers:, request: request_options) do |conn|
    conn.request :json
    conn.use :breakers
    conn.use Faraday::Response::RaiseError
    conn.response :raise_custom_error, error_prefix: service_name
    conn.response :json
    conn.response :betamocks if mock_enabled?
    conn.adapter Faraday.default_adapter
  end
end

#endpoint_settingsObject (private)



148
149
150
# File 'app/services/medical_copays/request.rb', line 148

def endpoint_settings
  Flipper.enabled?(:medical_copays_api_key_change) ? Settings.mcp.vbs_v2 : Settings.mcp.vbs
end

#get(path) ⇒ Faraday::Response

Make a HTTP GET call to the VBS service in order to obtain copays or PDFs by id

Parameters:

  • path (String)

Returns:

  • (Faraday::Response)


80
81
82
83
84
# File 'app/services/medical_copays/request.rb', line 80

def get(path)
  with_monitoring do
    connection.get(path)
  end
end

#handle_error(error) ⇒ Object



68
69
70
71
# File 'app/services/medical_copays/request.rb', line 68

def handle_error(error)
  Rails.logger.error("MedicalCopays::Request error: #{error.message}")
  raise error
end

#headersHash

HTTP request headers for the VBS API

Returns:

  • (Hash)


125
126
127
128
129
130
131
# File 'app/services/medical_copays/request.rb', line 125

def headers
  {
    'Host' => host,
    'Content-Type' => 'application/json',
    api_key => settings.api_key
  }
end

#mock_enabled?Boolean

Betamocks enabled status from settings

Returns:

  • (Boolean)


138
139
140
# File 'app/services/medical_copays/request.rb', line 138

def mock_enabled?
  settings.mock || false
end

#post(path, params) ⇒ Faraday::Response

Make a HTTP POST call to the VBS service in order to obtain user copays

Parameters:

  • path (String)
  • params (Hash)

Returns:

  • (Faraday::Response)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/medical_copays/request.rb', line 46

def post(path, params)
  if Flipper.enabled?(:debts_copay_logging) && !Rails.env.development?
    with_monitoring_and_error_handling do
      connection.post(path) do |req|
        req.body = Oj.dump(params)
      end
    end
  else
    with_monitoring do
      connection.post(path) do |req|
        req.body = Oj.dump(params)
      end
    end
  end
end

#request_optionsHash

Request options can be passed to the connection constructor and will be applied to all requests. This is the Common::Client config see: lib/common/client/configuration/base.rb

open_timeout: The max number of seconds to wait for the connection to be established. time_out: The max number of seconds to wait for the request to complete.

Returns:

  • (Hash)


113
114
115
116
117
118
# File 'app/services/medical_copays/request.rb', line 113

def request_options
  {
    open_timeout: 15,
    timeout: 50
  }
end

#with_monitoring_and_error_handlingObject



62
63
64
65
66
# File 'app/services/medical_copays/request.rb', line 62

def with_monitoring_and_error_handling(&)
  with_monitoring(2, &)
rescue => e
  handle_error(e)
end