Skip to content

Commit

Permalink
fix #21
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Mar 4, 2020
1 parent 735e4d3 commit fc6398e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/nice_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ def initialize(args = {})
end
@auto_redirect = auto_redirect
# for the case we have headers following nice_hash implementation
@headers_orig = @headers.dup
@headers = @headers.generate
self.class.active += 1
Expand Down
30 changes: 16 additions & 14 deletions lib/nice_http/http_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def get(arg, save_data: '')
else
@start_time_net = Time.now if @start_time_net.nil?
resp = @http.get(path, headers_t)
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(arg)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
resp = @http.get(path, headers_t)
end
data = resp.body
Expand Down Expand Up @@ -210,9 +210,9 @@ def post(*arguments)
else
resp = @http.post(path, data, headers_t)
#todo: do it also for forms and multipart
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(*arguments)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
resp = @http.post(path, data, headers_t)
end
data = resp.body
Expand Down Expand Up @@ -297,9 +297,9 @@ def put(*arguments)
begin
@start_time_net = Time.now if @start_time_net.nil?
resp = @http.send_request("PUT", path, data, headers_t)
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(*arguments)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
resp = @http.send_request("PUT", path, data, headers_t)
end
data = resp.body
Expand Down Expand Up @@ -369,9 +369,9 @@ def patch(*arguments)
begin
@start_time_net = Time.now if @start_time_net.nil?
resp = @http.patch(path, data, headers_t)
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(*arguments)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
resp = @http.patch(path, data, headers_t)
end
data = resp.body
Expand Down Expand Up @@ -456,18 +456,20 @@ def delete(argument)
@start_time_net = Time.now if @start_time_net.nil?
if data.to_s == ""
resp = @http.delete(path, headers_t)
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(argument)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
resp = @http.delete(path, headers_t)
end
else
request = Net::HTTP::Delete.new(path, headers_t)
request.body = data
resp = @http.request(request)
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(argument)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
request = Net::HTTP::Delete.new(path, headers_t)
request.body = data
resp = @http.request(request)
end
end
Expand Down Expand Up @@ -523,9 +525,9 @@ def head(argument)
begin
@start_time_net = Time.now if @start_time_net.nil?
resp = @http.head(path, headers_t)
if resp.code == 401 and @prev_request[:contains_lambda]
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(argument)
@headers_orig.each { |k,v| headers_t[k] = v.call if v.is_a?(Proc)}
resp = @http.head(path, headers_t)
end
data = resp.body
Expand Down
3 changes: 1 addition & 2 deletions lib/nice_http/manage_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def manage_request(*arguments)
require "json"

@prev_request = Hash.new() if @prev_request.nil?
@prev_request[:contains_lambda] = false

begin
content_type_included = false
path = ""
Expand Down Expand Up @@ -239,7 +239,6 @@ def manage_request(*arguments)
# for lambdas
if v.is_a?(Proc)
headers_t[k] = v.call
@prev_request[:contains_lambda] = true
end
end
@prev_request[:path] = path
Expand Down
2 changes: 1 addition & 1 deletion nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.8.2'
s.version = '1.8.3'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.authors = ["Mario Ruiz"]
Expand Down

0 comments on commit fc6398e

Please sign in to comment.