Skip to content
Snippets Groups Projects
Verified Commit 1079bf3e authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Allow specifying reasons for all resources

parent ea2ae9c6
No related branches found
No related tags found
No related merge requests found
Pipeline #5467 failed
...@@ -77,9 +77,9 @@ module Passwordstate ...@@ -77,9 +77,9 @@ module Passwordstate
raise "Your version of Passwordstate (#{version}) doesn't support the requested feature" unless version? compare raise "Your version of Passwordstate (#{version}) doesn't support the requested feature" unless version? compare
end end
def request(method, api_path, options = {}) def request(method, api_path, query: nil, reason: nil, **options)
uri = URI(server_url + "/#{api_type}/" + api_path) uri = URI(server_url + "/#{api_type}/" + api_path)
uri.query = URI.encode_www_form(options.fetch(:query)) if options.key? :query uri.query = URI.encode_www_form(query) unless query.nil?
uri.query = nil if uri.query.nil? || uri.query.empty? uri.query = nil if uri.query.nil? || uri.query.empty?
req_obj = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new uri req_obj = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new uri
...@@ -92,7 +92,7 @@ module Passwordstate ...@@ -92,7 +92,7 @@ module Passwordstate
req_obj.ntlm_auth(auth_data[:username], auth_data[:password]) if api_type == :winapi req_obj.ntlm_auth(auth_data[:username], auth_data[:password]) if api_type == :winapi
headers.each { |h, v| req_obj[h] = v } headers.each { |h, v| req_obj[h] = v }
req_obj['APIKey'] = auth_data[:apikey] if api_type == :api req_obj['APIKey'] = auth_data[:apikey] if api_type == :api
req_obj['Reason'] = options.fetch(:reason) if options.key?(:reason) && version?('>= 8.4.8449') req_obj['Reason'] = reason if !reason.nil? && version?('>= 8.4.8449')
print_http req_obj print_http req_obj
res_obj = http.request req_obj res_obj = http.request req_obj
......
...@@ -36,9 +36,10 @@ module Passwordstate ...@@ -36,9 +36,10 @@ module Passwordstate
def self.all(client, query = {}) def self.all(client, query = {})
path = query.fetch(:_api_path, api_path) path = query.fetch(:_api_path, api_path)
reason = query.delete(:_reason)
query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' } query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }
[client.request(:get, path, query: query)].flatten.map do |object| [client.request(:get, path, query: query, reason: reason)].flatten.map do |object|
new object.merge(_client: client) new object.merge(_client: client)
end end
end end
...@@ -49,9 +50,10 @@ module Passwordstate ...@@ -49,9 +50,10 @@ module Passwordstate
return new _client: client, index_field => object if query[:_bare] return new _client: client, index_field => object if query[:_bare]
path = query.fetch(:_api_path, api_path) path = query.fetch(:_api_path, api_path)
reason = query.delete(:_reason)
query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' } query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }
resp = client.request(:get, "#{path}/#{object}", query: query).map do |data| resp = client.request(:get, "#{path}/#{object}", query: query, reason: reason).map do |data|
new data.merge(_client: client) new data.merge(_client: client)
end end
return resp.first if resp.one? || resp.empty? return resp.first if resp.one? || resp.empty?
...@@ -61,26 +63,29 @@ module Passwordstate ...@@ -61,26 +63,29 @@ module Passwordstate
def self.post(client, data, query = {}) def self.post(client, data, query = {})
path = query.fetch(:_api_path, api_path) path = query.fetch(:_api_path, api_path)
reason = query.delete(:_reason)
data = passwordstateify_hash data data = passwordstateify_hash data
query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' } query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }
new [client.request(:post, path, body: data, query: query)].flatten.first.merge(_client: client) new [client.request(:post, path, body: data, query: query, reason: reason)].flatten.first.merge(_client: client)
end end
def self.put(client, data, query = {}) def self.put(client, data, query = {})
path = query.fetch(:_api_path, api_path) path = query.fetch(:_api_path, api_path)
reason = query.delete(:_reason)
data = passwordstateify_hash data data = passwordstateify_hash data
query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' } query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }
client.request :put, path, body: data, query: query client.request :put, path, body: data, query: query, reason: reason
end end
def self.delete(client, object, query = {}) def self.delete(client, object, query = {})
path = query.fetch(:_api_path, api_path) path = query.fetch(:_api_path, api_path)
reason = query.delete(:_reason)
query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' } query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }
object = object.send(object.class.send(index_field)) if object.is_a? Resource object = object.send(object.class.send(index_field)) if object.is_a? Resource
client.request :delete, "#{path}/#{object}", query: query client.request :delete, "#{path}/#{object}", query: query, reason: reason
end end
def self.passwordstateify_hash(hash) def self.passwordstateify_hash(hash)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment