Class: RedfishClient::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/redfish_client/connector.rb

Overview

Connector serves as a low-level wrapper around HTTP calls that are used to retrieve data from the service API. It abstracts away implementation details such as sending the proper headers in request, which do not change between resource fetches.

Library users should treat this class as an implementation detail and use higer-level Resource instead.

Constant Summary collapse

DEFAULT_HEADERS =

Default headers, as required by Redfish spec https://redfish.dmtf.org/schemas/DSP0266_1.4.0.html#request-headers

{
  "Accept" => "application/json",
  "OData-Version" => "4.0"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url, verify = true) ⇒ Connector

Create new connector.

Parameters:

  • url (String)

    base url of the Redfish service

  • verify (Boolean) (defaults to: true)

    verify SSL certificate of the service



26
27
28
29
30
31
32
33
34
# File 'lib/redfish_client/connector.rb', line 26

def initialize(url, verify = true)
  @url = url
  @headers = DEFAULT_HEADERS.dup
  middlewares = Excon.defaults[:middlewares] +
    [Excon::Middleware::RedirectFollower]
  @connection = Excon.new(@url,
                          ssl_verify_peer: verify,
                          middlewares: middlewares)
end

Instance Method Details

#add_headers(headers) ⇒ Object

Add HTTP headers to the requests made by the connector.

Parameters:

  • headers (Hash<String, String>)

    headers to be added



39
40
41
# File 'lib/redfish_client/connector.rb', line 39

def add_headers(headers)
  @headers.merge!(headers)
end

#delete(path) ⇒ Excon::Response

Issue DELETE requests to the service.

Parameters:

  • path (String)

    path to the resource, relative to the base

Returns:

  • (Excon::Response)

    response object



83
84
85
# File 'lib/redfish_client/connector.rb', line 83

def delete(path)
  @connection.delete(path: path, headers: @headers)
end

#get(path) ⇒ Excon::Response

Issue GET request to service.

Parameters:

  • path (String)

    path to the resource, relative to the base url

Returns:

  • (Excon::Response)

    response object



57
58
59
# File 'lib/redfish_client/connector.rb', line 57

def get(path)
  @connection.get(path: path, headers: @headers)
end

#patch(path, data = nil) ⇒ Excon::Response

Issue PATCH requests to the service.

Parameters:

  • path (String)

    path to the resource, relative to the base

  • data (Hash) (defaults to: nil)

    data to be sent over the socket

Returns:

  • (Excon::Response)

    response object



75
76
77
# File 'lib/redfish_client/connector.rb', line 75

def patch(path, data = nil)
  @connection.patch(prepare_request_params(path, data))
end

#post(path, data = nil) ⇒ Excon::Response

Issue POST requests to the service.

Parameters:

  • path (String)

    path to the resource, relative to the base

  • data (Hash) (defaults to: nil)

    data to be sent over the socket, JSON encoded

Returns:

  • (Excon::Response)

    response object



66
67
68
# File 'lib/redfish_client/connector.rb', line 66

def post(path, data = nil)
  @connection.post(prepare_request_params(path, data))
end

#remove_headers(headers) ⇒ Object

Remove HTTP headers from requests made by the connector.

Headers that are not currently set are silently ignored and no error is raised.

Parameters:

  • headers (List<String>)

    headers to remove



49
50
51
# File 'lib/redfish_client/connector.rb', line 49

def remove_headers(headers)
  headers.each { |h| @headers.delete(h) }
end