Skip to content

Commit

Permalink
close #21
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Mar 4, 2020
1 parent 1195dd4 commit 735e4d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
36 changes: 36 additions & 0 deletions lib/nice_http/http_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ 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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(arg)
resp = @http.get(path, headers_t)
end
data = resp.body
manage_response(resp, data)
end
Expand Down Expand Up @@ -204,6 +209,12 @@ def post(*arguments)
data = resp.body
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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(*arguments)
resp = @http.post(path, data, headers_t)
end
data = resp.body
end
rescue Exception => stack
Expand Down Expand Up @@ -286,6 +297,11 @@ 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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(*arguments)
resp = @http.send_request("PUT", path, data, headers_t)
end
data = resp.body
rescue Exception => stack
@logger.warn stack
Expand Down Expand Up @@ -353,6 +369,11 @@ 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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(*arguments)
resp = @http.patch(path, data, headers_t)
end
data = resp.body
rescue Exception => stack
@logger.warn stack
Expand Down Expand Up @@ -435,10 +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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(argument)
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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(argument)
resp = @http.request(request)
end
end
data = resp.body
rescue Exception => stack
Expand Down Expand Up @@ -492,6 +523,11 @@ 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]
@logger.warn "Not authorized. Trying to generate a new token."
path, data, headers_t = manage_request(argument)
resp = @http.head(path, headers_t)
end
data = resp.body
rescue Exception => stack
@logger.warn stack
Expand Down
6 changes: 5 additions & 1 deletion lib/nice_http/manage_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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 @@ -236,7 +237,10 @@ def manage_request(*arguments)
end
headers_t.each do |k, v|
# for lambdas
headers_t[k] = v.call if v.is_a?(Proc)
if v.is_a?(Proc)
headers_t[k] = v.call
@prev_request[:contains_lambda] = true
end
end
@prev_request[:path] = path
@prev_request[:data] = data
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.1'
s.version = '1.8.2'
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 735e4d3

Please sign in to comment.