Class: XClarityClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/xclarity_client/connection/connection.rb

Overview

Handles the LXCA connection providing some services to interact with the API.

Constant Summary collapse

HEADER_MESSAGE =
'XClarityClient::Connection'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • configuration (Hash)
    • the data to create a connection with the LXCA

Options Hash (configuration):

  • :host (String)

    the LXCA host

  • :username (String)

    the LXCA username

  • :password (String)

    the username password

  • :port (String)

    the LXCA port

  • :auth_type (String)

    the type of the authentication (‘token’, ‘basic_auth’)

  • :verify_ssl (String) — default: 'PEER', 'NONE'
  • :user_agent_label (String)

    Api gem client identification

  • :timeout (String)

    the limit time in seconds



25
26
27
28
29
30
31
# File 'lib/xclarity_client/connection/connection.rb', line 25

def initialize(configuration)
  @connection = build(configuration)
  @connection_multipart = build(configuration, true)
  @connection_net_http = build(configuration, false, true)
  @timeout = configuration.timeout
  @configuration = configuration
end

Instance Method Details

#do_delete(uri = '') ⇒ Object

Does a DELETE request to an LXCA endpoint

Parameters:

  • uri (String) (defaults to: '')
    • endpoint to do the request



99
100
101
# File 'lib/xclarity_client/connection/connection.rb', line 99

def do_delete(uri = '')
  build_request(:delete, uri)
end

#do_get(uri = "", query: {}, headers: {}, n_http: false) ⇒ Object

Does a GET request to an LXCA endpoint

Parameters:

  • uri (String) (defaults to: "")
    • endpoint to do the request

  • query (Hash) (defaults to: {})
    • params to query the endpoint resources

  • headers (Hash) (defaults to: {})
    • add headers to the request



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xclarity_client/connection/connection.rb', line 39

def do_get(uri = "", query: {}, headers: {}, n_http: false)
  url_query = query.size > 0 ? "?" + query.map {|k, v| "#{k}=#{v}"}.join("&") : ""
  Timeout.timeout(@timeout) do
    con = n_http ? @connection_net_http : @connection
    con.get do |req|
      req.url(uri + url_query)
      headers.map { |key, value| req.headers[key] = value }
    end
  end
rescue Faraday::Error::ConnectionFailed, Timeout::Error => e
  msg = "Error trying to send a GET to #{uri + url_query} "\
        "the reason: #{e.message}"
  $lxca_log.error(HEADER_MESSAGE + ' do_get', msg)
  Faraday::Response.new
end

#do_get_file_download(url, file_path) ⇒ Object



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
# File 'lib/xclarity_client/connection/connection.rb', line 55

def do_get_file_download(url, file_path)
  host = @configuration.host
  username = @configuration.username
  password = @configuration.password

  uri = 'https://' + host + url unless host.include?('https')
  uri = host + url if host.include?('https')

  uri = URI(uri)
  Net::HTTP.start(uri.host, uri.port,
                  :use_ssl     => true,
                  :verify_mode => 0) do |http|

    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth(username, password)

    http.request(request) do |response|
      open file_path, 'wb' do |io|
        response.read_body do |chunk|
          io.write(chunk)
        end
      end
    end
  end
end

#do_post(uri = '', body = '', multipart = false) ⇒ Object

Does a POST request to an LXCA endpoint

Parameters:

  • uri (String) (defaults to: '')
    • endpoint to do the request

  • body (JSON) (defaults to: '')
    • json to be sent in request body



86
87
88
# File 'lib/xclarity_client/connection/connection.rb', line 86

def do_post(uri = '', body = '', multipart = false)
  build_request(:post, uri, body, multipart)
end

#do_put(uri = '', body = '') ⇒ Object

Does a PUT request to an LXCA endpoint

Parameters:

  • uri (String) (defaults to: '')
    • endpoint to do the request

  • body (JSON) (defaults to: '')
    • json to be sent in request body



93
94
95
# File 'lib/xclarity_client/connection/connection.rb', line 93

def do_put(uri = '', body = '')
  build_request(:put, uri, body)
end