Module: ALGOSEC_SDK::Rest

Included in:
Client
Defined in:
lib/algosec-sdk/rest.rb

Overview

Contains all the methods for making API REST calls

Constant Summary collapse

RESPONSE_CODE_OK =
200
RESPONSE_CODE_CREATED =
201
RESPONSE_CODE_ACCEPTED =
202
RESPONSE_CODE_NO_CONTENT =
204
RESPONSE_CODE_BAD_REQUEST =
400
RESPONSE_CODE_UNAUTHORIZED =
401
RESPONSE_CODE_NOT_FOUND =
404

Instance Method Summary collapse

Instance Method Details

#init_http_clientObject



26
27
28
29
30
31
# File 'lib/algosec-sdk/rest.rb', line 26

def init_http_client
  @http_client = ALGOSEC_SDK::AdvancedJSONClient.new(force_basic_auth: true)
  @http_client.proxy = nil if @disable_proxy
  @http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @ssl_enabled
  @http_client.set_auth(@host, @user, @password)
end

#response_handler(response) ⇒ Hash

Handle the response for rest call.

If an asynchronous task was started, this waits for it to complete.

Parameters:

  • response (HTTPResponse)

Returns:

  • (Hash)

    The parsed JSON body

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/algosec-sdk/rest.rb', line 111

def response_handler(response)
  case response.status
  when RESPONSE_CODE_OK # Synchronous read/query
    response.body
  when RESPONSE_CODE_CREATED # Synchronous add
    response.body
    # when RESPONSE_CODE_ACCEPTED # Asynchronous add, update or delete
    # return response.body #
    # @logger.debug "Waiting for task: #{response.headers['location']}"
    # task = wait_for(response.headers['location'])
    # return true unless task['associatedResource'] && task['associatedResource']['resourceUri']
    # resource_data = rest_get(task['associatedResource']['resourceUri'])
    # return JSON.parse(resource_data.body)
  when RESPONSE_CODE_NO_CONTENT # Synchronous delete
    {}
  when RESPONSE_CODE_BAD_REQUEST
    raise BadRequest, "400 BAD REQUEST #{response.body}"
  when RESPONSE_CODE_UNAUTHORIZED
    raise Unauthorized, "401 UNAUTHORIZED #{response.body}"
  when RESPONSE_CODE_NOT_FOUND
    raise NotFound, "404 NOT FOUND #{response.body}"
  else
    raise RequestError, "#{response.status} #{response.body}"
  end
end

#rest_api(type, path, options = {}) ⇒ NetHTTPResponse

Make a restful API request to the AlgoSec

Parameters:

  • type (Symbol)

    the rest method/type Options are :get, :post, :put, :patch, and :delete

  • path (String)

    the path for the request. Usually starts with “/rest/”

  • options (Hash) (defaults to: {})

    the options for the request

Options Hash (options):

  • :body (String)

    Hash to be converted into json and set as the request body

  • :Content-Type (String) — default: 'application/json'

    Set to nil or :none to have this option removed

Returns:

  • (NetHTTPResponse)

    The response object

Raises:

  • (InvalidRequest)

    if the request is invalid

  • (SocketError)

    if a connection could not be made

  • (OpenSSL::SSL::SSLError)

    if SSL validation of the AlgoSec’s certificate failed



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/algosec-sdk/rest.rb', line 43

def rest_api(type, path, options = {})
  raise InvalidRequest, 'Must specify path' unless path
  raise InvalidRequest, 'Must specify type' unless type
  @logger.debug "Making :#{type} rest call to #{@host}#{path}"

  uri = "#{@host}#{path}"
  response = send_request(type, uri, options)
  @logger.debug "  Response: Code=#{response.status}. Headers=#{response.headers}\n  Body=#{response.body}"
  response
rescue OpenSSL::SSL::SSLError => e
  msg = 'SSL verification failed for the request. Please either:'
  msg += "\n  1. Install the necessary certificate(s) into your cert store"
  msg += ". Using cert store: #{ENV['SSL_CERT_FILE']}" if ENV['SSL_CERT_FILE']
  msg += "\n  2. Set the :ssl_enabled option to false for your AlgoSec client (not recommended)"
  @logger.error msg
  raise e
rescue SocketError => e
  msg = "Failed to connect to AlgoSec host #{@host}!\n"
  @logger.error msg
  raise e
end

#rest_delete(path, options = {}) ⇒ Object

Make a restful DELETE request Parameters & return value align with those of the rest_api method above



91
92
93
# File 'lib/algosec-sdk/rest.rb', line 91

def rest_delete(path, options = {})
  rest_api(:delete, path, options)
end

#rest_get(path, options = {}) ⇒ Object

Make a restful GET request Parameters & return value align with those of the rest_api method above



67
68
69
# File 'lib/algosec-sdk/rest.rb', line 67

def rest_get(path, options = {})
  rest_api(:get, path, options)
end

#rest_patch(path, options = {}) ⇒ Object

Make a restful PATCH request Parameters & return value align with those of the rest_api method above



85
86
87
# File 'lib/algosec-sdk/rest.rb', line 85

def rest_patch(path, options = {})
  rest_api(:patch, path, options)
end

#rest_post(path, options = {}) ⇒ Object

Make a restful POST request Parameters & return value align with those of the rest_api method above



73
74
75
# File 'lib/algosec-sdk/rest.rb', line 73

def rest_post(path, options = {})
  rest_api(:post, path, options)
end

#rest_put(path, options = {}) ⇒ Object

Make a restful PUT request Parameters & return value align with those of the rest_api method above



79
80
81
# File 'lib/algosec-sdk/rest.rb', line 79

def rest_put(path, options = {})
  rest_api(:put, path, options)
end