Class: Sophos::SG::REST::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sophos/sg/rest/client.rb

Overview

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

Constant Summary collapse

HEADER_USER_AGENT =
"#{self} (#{Sophos::SG::REST::VERSION})".freeze
HEADER_ACCEPT =
'application/json'.freeze
HEADER_CONTENT_TYPE =
'Content-Type'.freeze
HEADER_ERR_ACK =
'X-Restd-Err-Ack'.freeze
HEADER_SESSION =
'X-Restd-Session'.freeze
HEADER_INSERT =
'X-Restd-Insert'.freeze
HEADER_LOCK_OVERRIDE =
'X-Restd-Lock-Override'.freeze
DEFAULT_HEADERS =
{
  'Accept' => HEADER_ACCEPT,
  'User-Agent' => HEADER_USER_AGENT
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
# File 'lib/sophos/sg/rest/client.rb', line 19

def initialize(url, options = {})
  @http = Sophos::SG::REST::HTTP.new(url, options)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



17
18
19
# File 'lib/sophos/sg/rest/client.rb', line 17

def http
  @http
end

#urlObject (readonly)

Returns the value of attribute url.



17
18
19
# File 'lib/sophos/sg/rest/client.rb', line 17

def url
  @url
end

Instance Method Details

#create_object(type, attributes, insert = nil) ⇒ Object



35
36
37
# 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) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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) ⇒ Object



122
123
124
125
126
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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| ... } ⇒ Object

Yields:

  • (req)


128
129
130
131
132
133
134
# 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) ⇒ Object



98
99
100
# File 'lib/sophos/sg/rest/client.rb', line 98

def get(path)
  do_json_request('GET', path)
end

#json_encode(data) ⇒ Object



143
144
145
# File 'lib/sophos/sg/rest/client.rb', line 143

def json_encode(data)
  data.to_json
end

#logger=(logger) ⇒ Object



23
24
25
# File 'lib/sophos/sg/rest/client.rb', line 23

def logger=(logger)
  @http.set_debug_output(logger)
end

#node(id) ⇒ Object



76
77
78
# File 'lib/sophos/sg/rest/client.rb', line 76

def node(id)
  get nodes_path(id)
end

#nodesObject



68
69
70
# File 'lib/sophos/sg/rest/client.rb', line 68

def nodes
  get nodes_path
end

#nodes_path(id = nil) ⇒ Object



84
85
86
87
88
# 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) ⇒ Object



31
32
33
# File 'lib/sophos/sg/rest/client.rb', line 31

def object(type, ref)
  get path_object(type, ref)
end

#objects(type) ⇒ Object



27
28
29
# File 'lib/sophos/sg/rest/client.rb', line 27

def objects(type)
  get path_objects(type)
end

#patch(path, data) ⇒ Object



116
117
118
119
120
# 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) ⇒ Object



39
40
41
# 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) ⇒ Object



90
91
92
# 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) ⇒ Object



94
95
96
# 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) ⇒ Object



102
103
104
105
106
107
# 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) ⇒ Object



109
110
111
112
113
114
# 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) ⇒ Object



136
137
138
139
140
141
# 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) ⇒ Object



80
81
82
# File 'lib/sophos/sg/rest/client.rb', line 80

def update_node(id, value)
  put nodes_path(id), value
end

#update_nodes(hash) ⇒ Object



72
73
74
# File 'lib/sophos/sg/rest/client.rb', line 72

def update_nodes(hash)
  patch nodes_path, hash
end

#update_object(type, attributes, insert = nil) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# 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