Class: Jfrog::Saas::Log::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/jfrog/saas/log/connectionmanager.rb

Instance Method Summary collapse

Instance Method Details

#additional_headers(additional_headers) ⇒ Object



44
45
46
# File 'lib/jfrog/saas/log/connectionmanager.rb', line 44

def additional_headers(additional_headers)
  additional_headers.merge(common_headers)
end

#common_headersObject



40
41
42
# File 'lib/jfrog/saas/log/connectionmanager.rb', line 40

def common_headers
  { 'Authorization' => "Bearer #{ConfigHandler.instance.conn_config.access_token}" }
end

#execute(relative_url, params, headers, body, method, gzip_support) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jfrog/saas/log/connectionmanager.rb', line 48

def execute(relative_url, params, headers, body, method, gzip_support)

  response = nil
  method = CommonUtils::HTTP_GET if method.nil?
  connection = get_connection(ConfigHandler.instance.conn_config.jpd_url, gzip_support)
  headers = if headers.nil?
              common_headers
            else
              additional_headers(headers)
            end
  if Faraday::METHODS_WITH_QUERY.include? method
    response = connection.get(relative_url, params, headers) if method == CommonUtils::HTTP_GET
    response = connection.delete(relative_url, params, headers) if method == CommonUtils::HTTP_DELETE
  elsif Faraday::METHODS_WITH_BODY.include? method
    response = connection.post(relative_url, body, headers) if method == CommonUtils::HTTP_POST
    response = connection.put(relative_url, body, headers) if method == CommonUtils::HTTP_PUT
    response = connection.patch(relative_url, body, headers) if method == CommonUtils::HTTP_PATCH
  end
  response
rescue Faraday::SSLError, Faraday::ServerError, Faraday::ConnectionFailed => e
  MessageUtils.instance.log_message(MessageUtils::CONNECTION_ERROR, { "param1": ConfigHandler.instance.conn_config.jpd_url, "param2":e.message, "#{MessageUtils::LOG_LEVEL}": CommonUtils::LOG_ERROR } )
  MessageUtils.instance.log_message(MessageUtils::ERROR_BACKTRACE, { "param1": e.backtrace, "#{MessageUtils::LOG_LEVEL}": CommonUtils::LOG_DEBUG } ) if ConfigHandler.instance.log_config.debug_mode == true
end

#get_connection(jpd_url, gzip_support) ⇒ Object

init and return the connection object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jfrog/saas/log/connectionmanager.rb', line 19

def get_connection(jpd_url, gzip_support)
  Faraday.new(url: jpd_url) do |connection|
    connection.adapter Faraday::Adapter::NetHttp
    connection.use Faraday::Request::UrlEncoded
    connection.use Faraday::FollowRedirects::Middleware
    connection.options.open_timeout = ConfigHandler.instance.conn_config.open_timeout_in_secs
    connection.options.read_timeout = ConfigHandler.instance.conn_config.read_timeout_in_secs
    connection.request(:retry, max: 3,
                               interval: 0.05,
                               interval_randomness: 0.5,
                               backoff_factor: 2,
                               exceptions: [Errno::ETIMEDOUT, Timeout::Error, Faraday::TimeoutError, Faraday::RetriableResponse])
    connection.request :gzip if gzip_support == true
    if ConfigHandler.instance.log_config.debug_mode == true
      connection.use Faraday::Response::Logger do |logger|
        logger.filter(/(Authorization)([^&]+)/, '\1 [REMOVED WHILE LOGGING]')
      end
    end
  end
end