rxg_client 1.0.1 → 1.1.0

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 +4 -4
  2. data/lib/rxg_client.rb +88 -35
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bef3b2acf98fd65afe6eb1147133d1d9bbe5cfd86d8e1a3e0a71ec90069d04af
4
- data.tar.gz: bfd2186b41c7220bf278e0aac274056abb8d778a2fffe8b909b8350991f7d6ca
3
+ metadata.gz: 7c30f4d0b9eef58f4347b1fb045df4181692f46be10babf27a9d810bfae171de
4
+ data.tar.gz: 55f2531f6de0bcc7d0e9ba157112661cf824ecd679203d9e2195f8a73187de6e
5
5
  SHA512:
6
- metadata.gz: f9cf653e5dc2909c2ca35cd7540bf5ad9078d3cc22002e75c4c31641f1e7d68133ab9a27b23e7ea9415fa787c24c8e42864d363e2f89004d9203963146d2e14a
7
- data.tar.gz: 89c833d17d2061e306d343e2609bdc396e199935a7454958826fd4cd3ffb60866d3c4ef574727927578d041265815fcc15566ac2449ee2cf86638f9878ffbb8c
6
+ metadata.gz: fc7d0587dc72037db8b49604e63a233c9943ea93cf6ff66c0892f9c29e87b1a9723e20961b65f2644ff071609c00650c49c11e2d501e61a23ec2a39b0e108034
7
+ data.tar.gz: 5fdaea89ca075608bc08ee87509b7517c020295845a9375b973e8be3b59c79a7efdbae147e86ab0cce9afe20107722ac2e7da2788e52379398e6318f4a1f0b02
@@ -3,7 +3,8 @@ class RxgClient
3
3
 
4
4
  include HTTParty
5
5
 
6
- attr_accessor :api_key, :hostname, :request_format, :raise_exceptions, :auth
6
+ attr_accessor :api_key, :hostname, :base_uri, :fleet, :request_format,
7
+ :raise_exceptions, :verify_ssl, :auth_method, :default_timeout, :debug_output,
7
8
 
8
9
  def request_format= (requested_format)
9
10
  raise HTTParty::UnsupportedFormat unless [ :json, :xml ].include?(requested_format.to_sym)
@@ -26,42 +27,101 @@ class RxgClient
26
27
  # If fleet is true, headers will always be used
27
28
  # - debug: pass a logger or $stdout to have debug_output logged, or nil to disable.
28
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
29
33
  def initialize(hostname, api_key, request_format: :json, default_timeout: 5,
30
- raise_exceptions: false, verify_ssl: false, fleet: false, debug: nil,
31
- auth_method: :headers)
34
+ raise_exceptions: false, verify_ssl: false, fleet: false, debug_output: nil,
35
+ base_uri: 'admin/scaffolds', auth_method: :headers)
32
36
 
33
37
  self.api_key = api_key
34
38
 
35
- self.request_format = request_format.to_sym
36
- self.class.format self.request_format
37
-
38
39
  self.hostname = hostname
39
- self.class.base_uri "https://#{self.hostname}/admin/scaffolds"
40
40
 
41
- self.class.default_timeout default_timeout
41
+ self.set_base_uri(base_uri)
42
+
43
+ self.fleet = fleet
44
+
45
+ self.default_timeout = default_timeout
42
46
 
43
47
  self.raise_exceptions = raise_exceptions
44
48
 
45
- self.class.default_options.update(verify: verify_ssl)
49
+ self.verify_ssl = verify_ssl
46
50
 
47
- self.class.debug_output debug
51
+ self.debug_output = debug
48
52
 
49
- case auth_method
50
- when :headers # compatible with rXg version 11.442 or later
51
- if fleet
52
- self.class.headers({ 'fleetkey' => self.api_key })
53
- else
54
- self.class.headers({ 'api_key' => self.api_key })
53
+ self.request_format = request_format.to_sym
54
+
55
+ self.auth_method = auth_method
56
+
57
+ end
58
+
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)
55
81
  end
56
- when :query
57
- if fleet
58
- self.class.headers({ 'fleetkey' => self.api_key })
59
- else
60
- self.class.default_params({ 'api_key' => self.api_key })
82
+ end
83
+ end
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
61
94
  end
95
+ h
62
96
  end
63
97
  end
64
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
+ puts "/#{action.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
+ }
119
+ response = self.class.send(http_method, "/#{action.delete_prefix('/')}", **default_args.merge(args))
120
+ response.success? ? self.parse(response.body) : raise(response.message)
121
+ end
122
+ end
123
+
124
+
65
125
  def parse(body)
66
126
  return {success: true} if body == ""
67
127
  begin
@@ -92,37 +152,31 @@ class RxgClient
92
152
 
93
153
  # create a record in the given table with the attributes provided in new_record
94
154
  def create(table, new_record)
95
- response = self.class.post("/#{table}/create/index.#{self.request_format}", body: {record: new_record})
96
- response.success? ? self.parse(response.body) : raise(response.message)
155
+ self.post("/#{table}/create", body: {record: new_record})
97
156
  end
98
157
 
99
158
  # list all records from the given table
100
159
  def list(table)
101
- response = self.class.get("/#{table}/index.#{self.request_format}")
102
- response.success? ? self.parse(response.body) : raise(response.message)
160
+ self.get("/#{table}")
103
161
  end
104
162
 
105
163
  def search(table, search_params)
106
- response = self.class.post("/#{table}/index.#{self.request_format}", body: search_params)
107
- response.success? ? self.parse(response.body) : raise(response.message)
164
+ self.post("/#{table}/index", body: search_params)
108
165
  end
109
166
 
110
167
  # return the record from the given table having the given id
111
168
  def show(table, id)
112
- response = self.class.get("/#{table}/show/#{id}.#{self.request_format}")
113
- response.success? ? self.parse(response.body) : raise(response.message)
169
+ self.get("/#{table}/show/#{id}")
114
170
  end
115
171
 
116
172
  # update a record from the given table, having the given id, with the updated attributes provided in updated_record_hash
117
173
  def update(table, id, updated_record_hash)
118
- response = self.class.post("/#{table}/update/#{id}.#{self.request_format}", body: {record: updated_record_hash})
119
- response.success? ? self.parse(response.body) : raise(response.message)
174
+ self.post("/#{table}/update/#{id}", body: {record: updated_record_hash})
120
175
  end
121
176
 
122
177
  # destroy a record from the given table having the given id
123
178
  def destroy(table, id)
124
- response = self.class.post("/#{table}/destroy/#{id}.#{self.request_format}")
125
- response.success? ? self.parse(response.body) : raise(response.message)
179
+ self.post("/#{table}/destroy/#{id}")
126
180
  end
127
181
 
128
182
  def execute(table, request)
@@ -134,8 +188,7 @@ class RxgClient
134
188
  # method_args - A serialized Array or Hash of the argument(s) expected by the method.
135
189
  # example method call:
136
190
  # 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]})
137
- response = self.class.post("/#{table}/execute.#{self.request_format}", body: {request: request})
138
- response.success? ? self.parse(response.body) : raise(response.message)
191
+ self.post("/#{table}/execute", body: {request: request})
139
192
  end
140
193
 
141
194
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rxg_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lannar Dean