class Rest

The Rest class provides a class implementation and methods for executing REST methods such as GET, POST, PUT and DELETE on the node. This class presents an abstraction

Public Class Methods

delete(conn, url, hdr) click to toggle source

This API performs DELETE request for the given url with switch credentials.

@param conn [Class] Connect object to the node @param url [String] URL for the DELETE request @param hdr [String] Header parameters for the DELETE request

@return [RestClient::Request] Rest output from SONIC in JSON format

# File lib/sonic-rbapi/rest_utils.rb, line 114
def self.delete(conn, url, hdr)
  begin
    Log.file.puts("\n--------------------- DELETE --------------------- ", url, hdr)
    resp = RestClient::Request.execute method: :delete, url: url, headers: hdr, user: conn.getUser, password: conn.getPassword, timeout: 20 ,:verify_ssl => false
    # REST API DELETE always gets empty response for success
    if resp != ''
      Log.file.puts("RESPONSE - ", resp)
      response = JSON.parse(resp)
      response
    else
      Log.file.puts("RESPONSE - empty")
      response = {}
      response
    end
  rescue RestClient::ExceptionWithResponse => err
    Log.file.puts("ERROR: ")
    Log.file.puts err
    Log.file.puts err.response
  end
end
get(conn, url, hdr) click to toggle source

This API performs REST GET request for the given url with switch credentials.

@param conn [Class] Connect object to the node @param url [String] URL for the GET request @param hdr [String] Header parameters for the GET request

@return [RestClient::Request] Rest output from SONIC in JSON format

# File lib/sonic-rbapi/rest_utils.rb, line 29
def self.get(conn, url, hdr)
  begin
    Log.file.puts("\n--------------------- GET --------------------- ", url, hdr)
    resp = RestClient::Request.execute method: :get, url: url, headers: hdr, user: conn.getUser, password: conn.getPassword, timeout: 20 ,:verify_ssl => false
    if resp != ''
      Log.file.puts("RESPONSE - ", resp)
      response = JSON.parse(resp)
      response
    else
      Log.file.puts("RESPONSE - empty")
      response = {}
      response
    end
  rescue RestClient::ExceptionWithResponse => err
    Log.file.puts("ERROR: ")
    Log.file.puts err
    Log.file.puts err.response
  end
end
post(conn, url, hdr, params) click to toggle source

This API performs POST request for the given url with switch credentials.

@param conn [Class] Connect object to the node @param url [String] URL for the POST request @param hdr [String] Header parameters for the POST request @param params [String] JSON body for POST request

@return [RestClient::Request] Rest output from SONIC in JSON format

# File lib/sonic-rbapi/rest_utils.rb, line 86
def self.post(conn, url, hdr, params)
  begin
    Log.file.puts("\n--------------------- POST --------------------- ", url, hdr, params)
    resp = RestClient::Request.execute method: :post, url: url, headers: hdr, payload: params, user: conn.getUser, password: conn.getPassword, timeout: 20 ,:verify_ssl => false
    # REST API POST always gets '{}' response for success
    if resp != ''
      Log.file.puts("RESPONSE - ", resp)
      response = JSON.parse(resp)
      response
    else
      Log.file.puts("RESPONSE - empty")
      response = {}
      response
    end
  rescue RestClient::ExceptionWithResponse => err
    Log.file.puts("ERROR: ")
    Log.file.puts err
    Log.file.puts err.response
  end
end
put(conn, url, hdr, params) click to toggle source

This API performs REST PUT request for the given url with switch credentials.

@param conn [Class] Connect object to the node @param url [String] URL for the PUT request @param hdr [String] Header parameters for the PUT request @param params [String] JSON body for PUT request

@return [RestClient::Request] Rest output from SONIC in JSON format

# File lib/sonic-rbapi/rest_utils.rb, line 57
def self.put(conn, url, hdr, params)
  begin
    Log.file.puts("\n--------------------- PUT --------------------- ", url, hdr, params)
    resp = RestClient::Request.execute method: :put, url: url, headers: hdr, payload: params, user: conn.getUser, password: conn.getPassword, timeout: 20 ,:verify_ssl => false
    # REST API PUT always gets empty response for success
    if resp != ''
      Log.file.puts("RESPONSE - ", resp)
      response = JSON.parse(resp)
      response
    else
      Log.file.puts("RESPONSE - empty")
      response = {}
      response
    end
  rescue RestClient::ExceptionWithResponse => err
    Log.file.puts("ERROR: ")
    Log.file.puts err
    Log.file.puts err.response
  end
end