Class: CARMA::Client::MuleSoftClient

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/carma/client/mule_soft_client.rb

Defined Under Namespace

Classes: RecordParseError

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.carma.mulesoft'

Instance Method Summary collapse

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

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Instance Method Details

#bearer_tokenObject (private)



81
82
83
# File 'lib/carma/client/mule_soft_client.rb', line 81

def bearer_token
  @bearer_token ||= CARMA::Client::MuleSoftAuthTokenClient.new.new_bearer_token
end

#create_submission_v2(payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/carma/client/mule_soft_client.rb', line 17

def create_submission_v2(payload)
  with_monitoring do
    res = if Flipper.enabled?(:cg1010_oauth_2_enabled)
            perform_post(payload)
          else
            do_post('v2/application/1010CG/submit', payload, config.settings.async_timeout)
          end

    raise RecordParseError if res.dig('record', 'hasErrors')

    res
  end
end

#do_post(resource, payload, timeout = config.timeout) ⇒ Hash (private)

Parameters:

  • resource (String)

    REST-ful path component

  • payload (String)

    JSON payload to submit

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
44
45
# File 'lib/carma/client/mule_soft_client.rb', line 36

def do_post(resource, payload, timeout = config.timeout)
  with_monitoring do
    Rails.logger.info "[Form 10-10CG] Submitting to '#{resource}'"
    args = post_args(resource, payload, timeout)
    resp = perform(*args)
    Sentry.set_extras(response_body: resp.body)
    raise_error_unless_success(resource, resp.status)
    JSON.parse(resp.body)
  end
end

#get_body(payload) ⇒ Object (private)



92
93
94
# File 'lib/carma/client/mule_soft_client.rb', line 92

def get_body(payload)
  payload.is_a?(String) ? payload : payload.to_json
end

#handle_response(resource, response) ⇒ Object (private)



96
97
98
99
100
# File 'lib/carma/client/mule_soft_client.rb', line 96

def handle_response(resource, response)
  Sentry.set_extras(response_body: response.body)
  raise_error_unless_success(resource, response.status)
  JSON.parse(response.body)
end

#headersObject (private)



85
86
87
88
89
90
# File 'lib/carma/client/mule_soft_client.rb', line 85

def headers
  {
    'Authorization' => "Bearer #{bearer_token}",
    'Content-Type' => 'application/json'
  }
end

#perform_post(payload) ⇒ Object (private)

New Authentication strategy Call Mulesoft with bearer token



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/carma/client/mule_soft_client.rb', line 64

def perform_post(payload)
  resource = 'v2/application/1010CG/submit'
  with_monitoring do
    Rails.logger.info "[Form 10-10CG] Submitting to '#{resource}' using bearer token"

    response = perform(
      :post,
      resource,
      get_body(payload),
      headers,
      { timeout: config.settings.async_timeout }
    )

    handle_response(resource, response)
  end
end

#post_args(resource, payload, timeout) ⇒ Array (private)

Returns:

  • (Array)


48
49
50
51
52
53
# File 'lib/carma/client/mule_soft_client.rb', line 48

def post_args(resource, payload, timeout)
  body = payload.is_a?(String) ? payload : payload.to_json
  headers = config.base_request_headers
  opts = { timeout: }
  [:post, resource, body, headers, opts]
end

#raise_error_unless_success(resource, status) ⇒ Object (private)



55
56
57
58
59
60
# File 'lib/carma/client/mule_soft_client.rb', line 55

def raise_error_unless_success(resource, status)
  Rails.logger.info "[Form 10-10CG] Submission to '#{resource}' resource resulted in response code #{status}"
  return if [200, 201, 202].include? status

  raise Common::Exceptions::SchemaValidationErrors, ["Expecting 200 status but received #{status}"]
end