rxg_client 0.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/rxg_client.rb +117 -29
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e7faddbac99675ec4042eb40ac013e70aa005c98
4
- data.tar.gz: 36edf972b42b8012e37b6dc04b828952bbe1e074
2
+ SHA256:
3
+ metadata.gz: 90d8e88c17b0befbe0616dcb6b94762d8fd4a881f73d28b9377acdff25d99861
4
+ data.tar.gz: 0fd92c212241c0a3bfa4a3edcf248ef88b7ca6ea7a3792a60dc43bd2b4cbd2cd
5
5
  SHA512:
6
- metadata.gz: b1626b8e2375bb84512059f249caaf7bd3b386be98c18f69ca49378b43a2ebc7aeaae4cfd2d2f092e108a734e74554ac2d2cb9ad3dbdd29707b08efbb14ee8f7
7
- data.tar.gz: be0004f6d585196a0f6d61e99001f79cdb020114130b581ccd786f622bcc97396222e59bd5d944d6bfd3d047cbc22124dcf3cfdddce508c8ff59de14fe7edffc
6
+ metadata.gz: 3e4fdf6070115ec4903ff715f14882116186b55c0c00cf5e1640586cc74473df1603cab7734a0094475e7a1f3b0c8006b37485f2d375a572251cda61376c8f6b
7
+ data.tar.gz: c19615651a096a6d424443f0de0ae3ac000e0e9585df8b12f645dc7716f68d3a213834f19d64ca37ac26f543c8d4e3362e45d9033bede28fd52f42a819b6b43b
@@ -1,37 +1,128 @@
1
1
  class RxgClient
2
- require 'delegate'
3
2
  require 'httparty'
4
3
 
5
4
  include HTTParty
6
5
 
7
- attr_accessor :api_key, :hostname, :request_format, :raise_exceptions
6
+ attr_accessor :api_key, :hostname, :base_uri, :fleet, :request_format,
7
+ :raise_exceptions, :verify_ssl, :auth_method, :default_timeout, :debug_output
8
8
 
9
9
  def request_format= (requested_format)
10
10
  raise HTTParty::UnsupportedFormat unless [ :json, :xml ].include?(requested_format.to_sym)
11
11
  @request_format = requested_format
12
12
  end
13
13
 
14
- def initialize(hostname, api_key, request_format: :json, default_timeout: 5, raise_exceptions: false, verify_ssl: false)
14
+ # The following options can be configured when initializing the client:
15
+ # - default_timeout: the amount of time in seconds to wait for a response.
16
+ # default is 5
17
+ # - raise_exceptions: true or false.
18
+ # default is true
19
+ # - verify_ssl: true or false.
20
+ # default is true
21
+ # If using an IP, must be false.
22
+ # - fleet: pass true if authentication should use the 'fleetkey' header
23
+ # instead of apikey.
24
+ # default is false
25
+ # - auth_method: must be one of: :headers, :query
26
+ # default is :headers
27
+ # If fleet is true, headers will always be used
28
+ # - debug: pass a logger or $stdout to have debug_output logged, or nil to disable.
29
+ # default is nil
30
+ # - base_uri: provide an alternative base_uri, either a full URL or just the
31
+ # path to append to the hostname.
32
+ # default uses the admin/scaffolds context to access the traditional API
33
+ def initialize(hostname, api_key, request_format: :json, default_timeout: 5,
34
+ raise_exceptions: false, verify_ssl: false, fleet: false, debug_output: nil,
35
+ base_uri: 'admin/scaffolds', auth_method: :headers)
15
36
 
16
37
  self.api_key = api_key
17
-
18
- self.request_format = request_format.to_sym
19
- self.class.format self.request_format
20
38
 
21
39
  self.hostname = hostname
22
- self.class.base_uri "https://#{self.hostname}/admin/scaffolds"
23
-
24
- self.class.default_timeout default_timeout
40
+
41
+ self.set_base_uri(base_uri)
42
+
43
+ self.fleet = fleet
44
+
45
+ self.default_timeout = default_timeout
25
46
 
26
47
  self.raise_exceptions = raise_exceptions
27
48
 
28
- self.class.default_options.update(verify: verify_ssl)
49
+ self.verify_ssl = verify_ssl
50
+
51
+ self.debug_output = debug_output
52
+
53
+ self.request_format = request_format.to_sym
54
+
55
+ self.auth_method = auth_method
56
+
29
57
  end
30
58
 
31
- def auth
32
- {api_key: self.api_key}
59
+
60
+ # change the active base_uri
61
+ def set_base_uri(base_uri)
62
+ if base_uri =~ /^https?:\/\//
63
+ self.base_uri = base_uri
64
+ else
65
+ self.base_uri = "https://#{self.hostname}/#{base_uri.delete_prefix('/')}"
66
+ end
67
+ end
68
+
69
+ # temporarily change the base_uri for the duration of the provided block, then
70
+ # change it back to its previous value
71
+ def with_base_uri(new_base_uri, &blk)
72
+ if block_given?
73
+ begin
74
+ old_uri = self.base_uri
75
+
76
+ set_base_uri(new_base_uri)
77
+
78
+ blk.call
79
+ ensure
80
+ set_base_uri(old_uri)
81
+ end
82
+ end
33
83
  end
34
84
 
85
+
86
+
87
+ def default_header
88
+ @headers ||= begin
89
+ h = { 'Accept' => "application/#{self.request_format}" }
90
+ if self.fleet
91
+ h['fleetkey'] = self.api_key
92
+ elsif self.auth_method == :headers # compatible with rXg version 11.442 or later
93
+ h['apikey'] = self.api_key
94
+ end
95
+ h
96
+ end
97
+ end
98
+
99
+ def default_query
100
+ case self.auth_method
101
+ when :query
102
+ { api_key: self.api_key }
103
+ when :headers
104
+ { }
105
+ end
106
+ end
107
+
108
+ %i(post get put patch delete).each do |http_method|
109
+ define_method(http_method) do |action, **args|
110
+ action = "/#{action.to_s.delete_prefix('/')}"
111
+ default_args = {
112
+ :headers => self.default_header.merge(args.delete(:headers) || {}),
113
+ :query => self.default_query.merge(args.delete(:query) || {}).presence,
114
+ :base_uri => self.base_uri,
115
+ :timeout => self.default_timeout,
116
+ :format => self.request_format,
117
+ :debug_output => self.debug_output,
118
+ :verify => self.verify_ssl
119
+ }
120
+ response = self.class.send(http_method, action, **default_args.merge(args))
121
+ response.success? ? self.parse(response.body) : raise(response.message)
122
+ end
123
+ end
124
+
125
+
35
126
  def parse(body)
36
127
  return {success: true} if body == ""
37
128
  begin
@@ -40,7 +131,7 @@ class RxgClient
40
131
  result = JSON.parse(body)
41
132
  when :xml
42
133
  result = Hash.from_xml(body)
43
- else
134
+ else
44
135
  raise "Request format should be one of: :json, :xml"
45
136
  end
46
137
 
@@ -62,32 +153,31 @@ class RxgClient
62
153
 
63
154
  # create a record in the given table with the attributes provided in new_record
64
155
  def create(table, new_record)
65
- response = self.class.post("/#{table}/create/index.#{self.request_format}", { body: {record: new_record}.merge(self.auth) })
66
- response.success? ? self.parse(response.body) : raise(response.message)
156
+ self.post("/#{table}/create", body: {record: new_record})
67
157
  end
68
-
158
+
69
159
  # list all records from the given table
70
160
  def list(table)
71
- response = self.class.get("/#{table}/index.#{self.request_format}", {body: self.auth})
72
- response.success? ? self.parse(response.body) : raise(response.message)
161
+ self.get("/#{table}")
162
+ end
163
+
164
+ def search(table, search_params)
165
+ self.post("/#{table}/index", body: search_params)
73
166
  end
74
167
 
75
168
  # return the record from the given table having the given id
76
169
  def show(table, id)
77
- response = self.class.get("/#{table}/show/#{id}.#{self.request_format}", {body: self.auth})
78
- response.success? ? self.parse(response.body) : raise(response.message)
170
+ self.get("/#{table}/show/#{id}")
79
171
  end
80
172
 
81
173
  # update a record from the given table, having the given id, with the updated attributes provided in updated_record_hash
82
174
  def update(table, id, updated_record_hash)
83
- response = self.class.post("/#{table}/update/#{id}.#{self.request_format}", {body: {record: updated_record_hash}.merge(self.auth)})
84
- response.success? ? self.parse(response.body) : raise(response.message)
175
+ self.post("/#{table}/update/#{id}", body: {record: updated_record_hash})
85
176
  end
86
177
 
87
178
  # destroy a record from the given table having the given id
88
179
  def destroy(table, id)
89
- response = self.class.post("/#{table}/destroy/#{id}.#{self.request_format}", {body: self.auth})
90
- response.success? ? self.parse(response.body) : raise(response.message)
180
+ self.post("/#{table}/destroy/#{id}")
91
181
  end
92
182
 
93
183
  def execute(table, request)
@@ -99,8 +189,7 @@ class RxgClient
99
189
  # method_args - A serialized Array or Hash of the argument(s) expected by the method.
100
190
  # example method call:
101
191
  # node.execute("shared_credential_groups", {record_id: 7, method_name: "make_login_session", method_args:["192.168.20.111", "00:00:00:00:00:05", "test", 1]})
102
- response = self.class.post("/#{table}/execute.#{self.request_format}", {query: self.auth, body: {request: request}})
103
- response.success? ? self.parse(response.body) : raise(response.message)
192
+ self.post("/#{table}/execute", body: {request: request})
104
193
  end
105
194
 
106
195
  private
@@ -122,12 +211,11 @@ class RxgClient
122
211
  end
123
212
 
124
213
  # The listed methods will be rescued from all StandardError exceptions, and the code within
125
- # the block will be executed.
126
- rescue_from StandardError, :create, :list, :show, :update, :destroy, :execute do |exception, instance|
214
+ # the block will be executed.
215
+ rescue_from StandardError, :create, :list, :show, :update, :destroy, :execute, :search do |exception, instance|
127
216
  puts exception.message
128
217
 
129
218
  raise exception if instance.raise_exceptions
130
219
  end
131
220
 
132
-
133
221
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rxg_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lannar Dean
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -51,8 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubyforge_project:
55
- rubygems_version: 2.6.11
54
+ rubygems_version: 3.0.6
56
55
  signing_key:
57
56
  specification_version: 4
58
57
  summary: RXG API Client