From 1079bf3ef5e846e962a2b0dcc6b3414f5a709806 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Mon, 28 Oct 2019 09:48:39 +0100
Subject: [PATCH] Allow specifying reasons for all resources

---
 lib/passwordstate/client.rb   |  6 +++---
 lib/passwordstate/resource.rb | 15 ++++++++++-----
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/lib/passwordstate/client.rb b/lib/passwordstate/client.rb
index 95b99f1..ef808e7 100644
--- a/lib/passwordstate/client.rb
+++ b/lib/passwordstate/client.rb
@@ -77,9 +77,9 @@ module Passwordstate
       raise "Your version of Passwordstate (#{version}) doesn't support the requested feature" unless version? compare
     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.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?
 
       req_obj = Net::HTTP.const_get(method.to_s.capitalize.to_sym).new uri
@@ -92,7 +92,7 @@ module Passwordstate
       req_obj.ntlm_auth(auth_data[:username], auth_data[:password]) if api_type == :winapi
       headers.each { |h, v| req_obj[h] = v }
       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
       res_obj = http.request req_obj
diff --git a/lib/passwordstate/resource.rb b/lib/passwordstate/resource.rb
index 0ba7d8e..76bffa8 100644
--- a/lib/passwordstate/resource.rb
+++ b/lib/passwordstate/resource.rb
@@ -36,9 +36,10 @@ module Passwordstate
 
     def self.all(client, query = {})
       path = query.fetch(:_api_path, api_path)
+      reason = query.delete(:_reason)
       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)
       end
     end
@@ -49,9 +50,10 @@ module Passwordstate
       return new _client: client, index_field => object if query[:_bare]
 
       path = query.fetch(:_api_path, api_path)
+      reason = query.delete(:_reason)
       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)
       end
       return resp.first if resp.one? || resp.empty?
@@ -61,26 +63,29 @@ module Passwordstate
 
     def self.post(client, data, query = {})
       path = query.fetch(:_api_path, api_path)
+      reason = query.delete(:_reason)
       data = passwordstateify_hash data
       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
 
     def self.put(client, data, query = {})
       path = query.fetch(:_api_path, api_path)
+      reason = query.delete(:_reason)
       data = passwordstateify_hash data
       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
 
     def self.delete(client, object, query = {})
       path = query.fetch(:_api_path, api_path)
+      reason = query.delete(:_reason)
       query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }
 
       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
 
     def self.passwordstateify_hash(hash)
-- 
GitLab