Method: Rbeapi::Eapilib::EapiConnection#send

Defined in:
lib/rbeapi/eapilib.rb

#send(data, opts) ⇒ Hash

This method will send the request to the node over the specified transport and return a response message with the contents from the eAPI response. eAPI responds to request messages with either a success message or failure message.

Examples:

eAPI Response - success

{
  "jsonrpc": "2.0",
  "result": [
    {},
    {},
    {
      "warnings": [
        <message>
      ]
    },
  ],
  "id": <reqid>
}

eAPI Response - failure

{
  "jsonrpc": "2.0",
  "error": {
    "code": <int>,
    "message": <string>,
    "data": [
      {},
      {},
      {
        "errors": [
          <message>
        ]
      }
    ]
  },
  "id": <reqid>
}

Parameters:

  • data (Hash)

    A hash containing the body of the request message. This should be a valid eAPI request message.

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • open_timeout (Float)

    Number of seconds to wait for the eAPI connection to open.

  • read_timeout (Float)

    Number of seconds to wait for one block of eAPI results to be read (via one read(2) call).

Returns:

  • (Hash)

    Returns the response message as a Ruby hash object.

Raises:

  • (CommandError)

    Raised if an eAPI failure response is return from the destination node.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/rbeapi/eapilib.rb', line 269

def send(data, opts)
  request = Net::HTTP::Post.new('/command-api')
  request.body = JSON.dump(data)
  request.basic_auth @username, @password

  open_timeout = opts.fetch(:open_timeout, @open_timeout)
  read_timeout = opts.fetch(:read_timeout, @read_timeout)

  begin
    @transport.open_timeout = open_timeout
    @transport.read_timeout = read_timeout
    response = @transport.request(request)
    decoded = JSON(response.body)

    if decoded.include?('error')
      code = decoded['error']['code']
      msg = decoded['error']['message']
      raise CommandError.new(msg, code)
    end
  rescue Timeout::Error
    raise ConnectionError, 'unable to connect to eAPI'
  end

  decoded
end