class Sophos::SG::REST::Client

Copyright 2016 Sophos Technology GmbH. All rights reserved. See the LICENSE.txt file for details. Authors: Vincent Landgraf

Constants

DEFAULT_HEADERS
HEADER_ACCEPT
HEADER_CONTENT_TYPE
HEADER_ERR_ACK
HEADER_INSERT
HEADER_LOCK_OVERRIDE
HEADER_SESSION
HEADER_USER_AGENT

Attributes

http[R]
url[R]

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 19
def initialize(url, options = {})
  @http = Sophos::SG::REST::HTTP.new(url, options)
end

Public Instance Methods

create_object(type, attributes, insert = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 35
def create_object(type, attributes, insert = nil)
  post path_objects(type), attributes, insert
end
decode_json(response, req) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 147
def decode_json(response, req)
  body = nil

  if response.body && response.body != ''
    # rubys JSON parse is unable to parse scalar values (number, string,
    # bool, ...) directly, because of this it needs to be wrapped before
    body = JSON.parse('[' + response.body + ']').first
    if body.is_a?(Array) && body.any? && body.first.is_a?(Hash)
      body = body.map { |i| OpenStruct.new(i) }
    elsif body.is_a? Hash
      body = OpenStruct.new(body)
    else
      body
    end
  end

  if response.code.to_i >= 400
    raise Sophos::SG::REST::Error.new(req, response, body)
  end

  body
end
delete(path) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 122
def delete(path)
  do_json_request('DELETE', path) do |req|
    req[HEADER_ERR_ACK] = 'all'
  end
end
destroy_object(type, ref = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 50
def destroy_object(type, ref = nil)
  # if ref is not passed, assume object or hash
  if ref.nil?
    if type.is_a? Hash
      ref = type['_ref'] || type[:_ref]
      type = type['_type'] || type[:_type]
    elsif type.respond_to?(:_type) && type.respond_to?(:_ref)
      ref = type._ref
      type = type._type
    else
      raise ArgumentError, 'type must be a string, hash or object with ' \
        ' _ref and _type defined'
    end
  end

  delete path_object(type, ref)
end
do_json_request(method, path, body = nil) { |req| ... } click to toggle source
# File lib/sophos/sg/rest/client.rb, line 128
def do_json_request(method, path, body = nil)
  body = json_encode(body) unless body.nil?
  req = request(method, path, body)
  yield req if block_given?
  response = @http.request(req)
  decode_json(response, req)
end
get(path) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 98
def get(path)
  do_json_request('GET', path)
end
json_encode(data) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 143
def json_encode(data)
  data.to_json
end
logger=(logger) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 23
def logger=(logger)
  @http.set_debug_output(logger)
end
node(id) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 76
def node(id)
  get nodes_path(id)
end
nodes() click to toggle source
# File lib/sophos/sg/rest/client.rb, line 68
def nodes
  get nodes_path
end
nodes_path(id = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 84
def nodes_path(id = nil)
  base = File.join(@http.url.path, 'nodes') + '/'
  base = File.join(base, id) if id
  base
end
object(type, ref) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 31
def object(type, ref)
  get path_object(type, ref)
end
objects(type) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 27
def objects(type)
  get path_objects(type)
end
patch(path, data) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 116
def patch(path, data)
  do_json_request('PATCH', path, data) do |req|
    req[HEADER_CONTENT_TYPE] = HEADER_ACCEPT
  end
end
patch_object(type, ref, attributes) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 39
def patch_object(type, ref, attributes)
  patch path_object(type, ref), attributes
end
path_object(type, ref) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 90
def path_object(type, ref)
  File.join(@http.url.path, 'objects', type, ref)
end
path_objects(type) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 94
def path_objects(type)
  File.join(@http.url.path, 'objects', type) + '/'
end
post(path, data, insert = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 102
def post(path, data, insert = nil)
  do_json_request('POST', path, data) do |req|
    req[HEADER_CONTENT_TYPE] = HEADER_ACCEPT
    req[HEADER_INSERT] = insert if insert
  end
end
put(path, data, insert = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 109
def put(path, data, insert = nil)
  do_json_request('PUT', path, data) do |req|
    req[HEADER_CONTENT_TYPE] = HEADER_ACCEPT
    req[HEADER_INSERT] = insert if insert
  end
end
request(method, path, body = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 136
def request(method, path, body = nil)
  req = Net::HTTPGenericRequest.new(method, !body.nil?, true, path, DEFAULT_HEADERS)
  req.basic_auth @http.url.user, @http.url.password
  req.body = body
  req
end
update_node(id, value) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 80
def update_node(id, value)
  put nodes_path(id), value
end
update_nodes(hash) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 72
def update_nodes(hash)
  patch nodes_path, hash
end
update_object(type, attributes, insert = nil) click to toggle source
# File lib/sophos/sg/rest/client.rb, line 43
def update_object(type, attributes, insert = nil)
  h = attributes.to_h
  ref = h['_ref'] || h[:_ref]
  raise ArgumentError, "Object _ref must be set! #{h.inspect}" if ref.nil?
  put path_object(type, ref), h, insert
end