nice_http 1.8.2 → 1.8.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15cb5c2a1e8c6df4d82ead3b1185f981d556c94c1ec358fa868d83a7a2cfb643
4
- data.tar.gz: 7893ef6a779be11d71f8c5c7f468fd063ea41af0f6d63fbe3451c164e0fc3573
3
+ metadata.gz: 8d4be3fb22aecfd4d84cb2c9384da399cf7a521742bba214f5e2746491120f4b
4
+ data.tar.gz: c2a143357e458385078c5e6709100d06ae473f7363282eb9c36c766618631dcf
5
5
  SHA512:
6
- metadata.gz: ebdd1df7c1b70729affae816609537cf637f6e3ccf38f6533bba24f941e60bccccc21695f6f96bd0d14f1f96ae9579cddbfea7d552823af888d25906d994a4ba
7
- data.tar.gz: aa3e345d27836d891183909917679dc98520e0f0f9d64ce6c0d7588f521415ac26e7e94a63c4a328e5100a5e0e968a79aae3532ecce6df2b755e1a796bcba47a
6
+ metadata.gz: 7e752817f9ca1b87b944904435c2a507b41cc8c76e0744ff10a4db1f21ea7009a75255d97b45d6a381cd7f1ed30d91ef1fc46371fb935e6cd18482df3867f693
7
+ data.tar.gz: 6269a98fa4e85bf2a9b13069d279bd58f184267b5b38342a1aa5c3cfbe855207bbacb94b91f5f22e2735426fc726326be381edaab27229f943455569ce157e3d
data/README.md CHANGED
@@ -29,6 +29,11 @@ Example that creates 1000 good random and unique requests to register an user an
29
29
  - [Responses](#Responses)
30
30
  - [Special settings](#Special-settings)
31
31
  - [Authentication requests](#Authentication-requests)
32
+ - [Basic Authentication](#Basic-Authentication)
33
+ - [OpenID](#OpenID)
34
+ - [OAuth2](#OAuth2)
35
+ - [JWT Token](#JWT-Token)
36
+ - [lambda on headers](#lambda-on-headers)
32
37
  - [Http logs](#Http-logs)
33
38
  - [Multithreading](#Multithreading)
34
39
  - [Http stats](#Http-stats)
@@ -117,6 +122,7 @@ NiceHttp.defaults = {
117
122
  host: 'reqres.in',
118
123
  ssl: true,
119
124
  port: 443,
125
+ timeout: 15, #seconds
120
126
  debug: false,
121
127
  log: "./my_logs.log",
122
128
  headers: {"api-key": "the api key"}
@@ -241,6 +247,19 @@ If the request hash contains a key :method with one of these possible values: :g
241
247
  resp = @http.send_request req
242
248
  ```
243
249
 
250
+ You can always access to the last request as a Hash object by: `NiceHttp.request`
251
+
252
+ If you want to change the value of all headers using the value of the request on runtime, use `lambda`, `NiceHttp.requests` and `NiceHttp.request`:
253
+
254
+ ```ruby
255
+ NiceHttp.requests = {
256
+ headers: {
257
+ Referer: lambda { "http://myserver.com" + NiceHttp.request.path }
258
+ }
259
+ }
260
+ ```
261
+
262
+ You can use `NiceHttp.requests` to specify certain `headers` or `data` that will apply on all requests sent.
244
263
 
245
264
  ## Responses
246
265
 
@@ -271,10 +290,14 @@ Also interesting keys would be: *time_elapsed_total*, *time_elapsed* and many mo
271
290
 
272
291
  *auto_redirect*: (true or false) in case of true it will take care of the auto redirections.
273
292
 
293
+ *timeout*: Integer that will set a time out for the time waiting to connect to a host or waiting for a response.
294
+
274
295
  ## Authentication requests
275
296
 
276
297
  All we need to do is to add to our request the correct authentication tokens, seeds, headers.
277
298
 
299
+ ### Basic Authentication
300
+
278
301
  For example for Basic Authentication we need to add to the authorization header a seed generated with the user and password we want ot authenticate
279
302
 
280
303
  ```ruby
@@ -305,6 +328,8 @@ Remember for other kind of authentication systems NiceHttp take care of the redi
305
328
 
306
329
  In case you want or need to control the redirections by yourself instead of allowing NiceHttp to do it, then set ```@http.auto_redirect = false```
307
330
 
331
+ ### OpenID
332
+
308
333
  An example using OpenID authentication:
309
334
 
310
335
  ```ruby
@@ -349,17 +374,65 @@ The output:
349
374
 
350
375
  ```
351
376
 
377
+ ### OAuth2
378
+
352
379
  You can see on the next link how to get the OAuth2 token for Microsoft Azure and add it to your Http connection header.
353
380
 
354
381
  https://gist.github.com/MarioRuiz/d3525185024737885c0c9afa6dc8b9e5
355
382
 
383
+ ### JWT token
384
+
385
+ An example for Google using JWT
386
+
387
+ my_json_key_file.json:
388
+ ```json
389
+ {
390
+ "type": "service_account",
391
+ "project_id": "example",
392
+ "private_key_id": "fjdslkafldkasfadsjflkjdsaklfjasdklfjlkdsjfl",
393
+ "private_key": "-----BEGIN PRIVATE KEY-----....==\n-----END PRIVATE KEY-----\n",
394
+ "client_email": "example@example.iam.gserviceaccount.com",
395
+ "client_id": "46545646",
396
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
397
+ "token_uri": "https://oauth2.googleapis.com/token",
398
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
399
+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/example%40example.iam.gserviceaccount.com"
400
+ }
401
+ ```
402
+
403
+ ```ruby
404
+ require 'jwt'
405
+ require 'nice_hash'
406
+ def generate_jwt(audience, json_key_file)
407
+ json = File.open(json_key_file).read.json
408
+ now = Time.new
409
+ payload = {
410
+ iss: json.client_email,
411
+ sub: json.client_email,
412
+ aud: audience,
413
+ exp: (now + 3600).to_i,
414
+ iat: (now - 60).to_i,
415
+ kid: json.private_key_id
416
+ }
417
+ jwt_token = JWT.encode payload, OpenSSL::PKey::RSA.new(json.private_key), "RS256"
418
+ return jwt_token
419
+ end
420
+
421
+ NiceHttp.headers = {
422
+ Authorization: lambda { "Bearer " + generate_jwt('https:/myhost.com', './my_json_key_file.json') }
423
+ }
424
+
425
+ ```
426
+
427
+ ### lambda on headers
428
+
356
429
  If you need a new token every time a new http connection is created you can use `lambda`
357
430
 
358
431
  ```ruby
359
432
  NiceHttp.headers[:Authorization] = lambda {get_token()}
360
433
  ```
361
434
 
362
- NiceHttp will call the get_token method you created every time a new Http connection is created.
435
+ NiceHttp will call the get_token method you created every time a new connection is created.
363
436
 
364
437
  ## Http logs
365
438
 
@@ -489,7 +562,7 @@ RESPONSE:
489
562
 
490
563
  ```
491
564
 
492
- If you want to get the last request sent or the last response use `NiceHttp.last_request` or `NiceHttp.last_response`
565
+ If you want to get the last request sent or the last response as a message use `NiceHttp.last_request` or `NiceHttp.last_response`. If you want to access the last request as a Hash use `NiceHttp.request`
493
566
 
494
567
  Also you can collect all data sent and received by setting `NiceHttp.capture = true` and all data will be stored on `NiceHttp.captured` as an Array of Strings (Request+Response).
495
568
 
@@ -514,7 +587,7 @@ threads = []
514
587
  end
515
588
  end
516
589
 
517
- t.each(&:join)
590
+ threads.each(&:join)
518
591
 
519
592
  # log files: nice_http_0.log, nice_http_1.log... nice_http_39.log
520
593
  ```
@@ -7,13 +7,14 @@ require_relative "nice_http/http_methods"
7
7
 
8
8
  ######################################################
9
9
  # Attributes you can access using NiceHttp.the_attribute:
10
- # :host, :port, :ssl, :headers, :debug, :log, :log_headers, :proxy_host, :proxy_port,
10
+ # :host, :port, :ssl, :timeout, :headers, :debug, :log, :log_headers, :proxy_host, :proxy_port,
11
11
  # :last_request, :last_response, :request_id, :use_mocks, :connections,
12
- # :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured
12
+ # :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured, :request, :requests
13
13
  #
14
14
  # @attr [String] host The host to be accessed
15
15
  # @attr [Integer] port The port number
16
16
  # @attr [Boolean] ssl If you use ssl or not
17
+ # @attr [Integer] timeout Max time to wait until connected to the host or getting a response.
17
18
  # @attr [Hash] headers Contains the headers you will be using on your connection
18
19
  # @attr [Boolean] debug In case true shows all the details of the communication with the host
19
20
  # @attr [String] log_path The path where the logs will be stored. By default empty string.
@@ -29,6 +30,8 @@ require_relative "nice_http/http_methods"
29
30
  # @attr [Integer] proxy_port the proxy port to be used
30
31
  # @attr [String] last_request The last request with all the content sent
31
32
  # @attr [String] last_response Only in case :debug is true, the last response with all the content
33
+ # @attr [Hash] request The last request with all the content sent
34
+ # @attr [Hash] requests The defaults for all requests. keys: :headers and :data
32
35
  # @attr [String] request_id If the response includes a requestId, will be stored here
33
36
  # @attr [Boolean] use_mocks If true, in case the request hash includes a :mock_response key, it will be used as the response instead
34
37
  # @attr [Array] connections It will include all the active connections (NiceHttp instances)
@@ -69,9 +72,9 @@ class NiceHttp
69
72
  end
70
73
 
71
74
  class << self
72
- attr_accessor :host, :port, :ssl, :headers, :debug, :log_path, :log, :proxy_host, :proxy_port, :log_headers,
73
- :last_request, :last_response, :request_id, :use_mocks, :connections,
74
- :active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured
75
+ attr_accessor :host, :port, :ssl, :timeout, :headers, :debug, :log_path, :log, :proxy_host, :proxy_port, :log_headers,
76
+ :last_request, :last_response, :request, :request_id, :use_mocks, :connections,
77
+ :active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured, :requests
75
78
  end
76
79
 
77
80
  at_exit do
@@ -87,6 +90,7 @@ class NiceHttp
87
90
  @host = nil
88
91
  @port = 80
89
92
  @ssl = false
93
+ @timeout = nil
90
94
  @headers = {}
91
95
  @values_for = {}
92
96
  @debug = false
@@ -96,6 +100,8 @@ class NiceHttp
96
100
  @proxy_host = nil
97
101
  @proxy_port = nil
98
102
  @last_request = nil
103
+ @request = nil
104
+ @requests = nil
99
105
  @last_response = nil
100
106
  @request_id = ""
101
107
  @use_mocks = false
@@ -133,18 +139,19 @@ class NiceHttp
133
139
  subclass.reset!
134
140
  end
135
141
 
136
- attr_reader :host, :port, :ssl, :debug, :log, :log_path, :proxy_host, :proxy_port, :response, :num_redirects
142
+ attr_reader :host, :port, :ssl, :timeout, :debug, :log, :log_path, :proxy_host, :proxy_port, :response, :num_redirects
137
143
  attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger, :values_for, :log_headers
138
144
 
139
145
  ######################################################
140
146
  # Change the default values for NiceHttp supplying a Hash
141
147
  #
142
- # @param par [Hash] keys: :host, :port, :ssl, :headers, :debug, :log, :log_path, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for, :create_stats, :log_headers, :capture
148
+ # @param par [Hash] keys: :host, :port, :ssl, :timeout, :headers, :debug, :log, :log_path, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for, :create_stats, :log_headers, :capture
143
149
  ######################################################
144
150
  def self.defaults=(par = {})
145
151
  @host = par[:host] if par.key?(:host)
146
152
  @port = par[:port] if par.key?(:port)
147
153
  @ssl = par[:ssl] if par.key?(:ssl)
154
+ @timeout = par[:timeout] if par.key?(:timeout)
148
155
  @headers = par[:headers].dup if par.key?(:headers)
149
156
  @values_for = par[:values_for].dup if par.key?(:values_for)
150
157
  @debug = par[:debug] if par.key?(:debug)
@@ -296,6 +303,7 @@ class NiceHttp
296
303
  # host -- example.com. (default blank screen)
297
304
  # port -- port for the connection. 80 (default)
298
305
  # ssl -- true, false (default)
306
+ # timeout -- integer or nil (default)
299
307
  # headers -- hash with the headers
300
308
  # values_for -- hash with the values_for
301
309
  # debug -- true, false (default)
@@ -323,6 +331,7 @@ class NiceHttp
323
331
  @port = self.class.port
324
332
  @prepath = ""
325
333
  @ssl = self.class.ssl
334
+ @timeout = self.class.timeout
326
335
  @headers = self.class.headers.dup
327
336
  @values_for = self.class.values_for.dup
328
337
  @debug = self.class.debug
@@ -353,6 +362,7 @@ class NiceHttp
353
362
  @host = args[:host] if args.keys.include?(:host)
354
363
  @port = args[:port] if args.keys.include?(:port)
355
364
  @ssl = args[:ssl] if args.keys.include?(:ssl)
365
+ @timeout = args[:timeout] if args.keys.include?(:timeout)
356
366
  @headers = args[:headers].dup if args.keys.include?(:headers)
357
367
  @values_for = args[:values_for].dup if args.keys.include?(:values_for)
358
368
  @debug = args[:debug] if args.keys.include?(:debug)
@@ -448,6 +458,7 @@ class NiceHttp
448
458
  raise InfoMissing, :port if @port.to_s == ""
449
459
  raise InfoMissing, :host if @host.to_s == ""
450
460
  raise InfoMissing, :ssl unless @ssl.is_a?(TrueClass) or @ssl.is_a?(FalseClass)
461
+ raise InfoMissing, :timeout unless @timeout.is_a?(Integer) or @timeout.nil?
451
462
  raise InfoMissing, :debug unless @debug.is_a?(TrueClass) or @debug.is_a?(FalseClass)
452
463
  raise InfoMissing, :auto_redirect unless auto_redirect.is_a?(TrueClass) or auto_redirect.is_a?(FalseClass)
453
464
  raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass)
@@ -461,18 +472,26 @@ class NiceHttp
461
472
  @http.use_ssl = @ssl
462
473
  @http.set_debug_output $stderr if @debug
463
474
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
475
+ unless @timeout.nil?
476
+ @http.open_timeout = @timeout
477
+ @http.read_timeout = @timeout
478
+ end
464
479
  @http.start
465
480
  else
466
481
  @http = Net::HTTP.new(@host, @port)
467
482
  @http.use_ssl = @ssl
468
483
  @http.set_debug_output $stderr if @debug
469
484
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
485
+ unless @timeout.nil?
486
+ @http.open_timeout = @timeout
487
+ @http.read_timeout = @timeout
488
+ end
470
489
  @http.start
471
490
  end
472
491
 
473
492
  @message_server = "(#{self.object_id}):"
474
493
 
475
- log_message = "(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, mode:#{@mode}, proxy_host: #{@proxy_host.to_s()}, proxy_port: #{@proxy_port.to_s()} "
494
+ log_message = "(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, timeout:#{@timeout}, mode:#{@mode}, proxy_host: #{@proxy_host.to_s()}, proxy_port: #{@proxy_port.to_s()} "
476
495
 
477
496
  @logger.info(log_message)
478
497
  @message_server += " Http connection: "
@@ -487,6 +506,7 @@ class NiceHttp
487
506
  end
488
507
  @auto_redirect = auto_redirect
489
508
  # for the case we have headers following nice_hash implementation
509
+ @headers_orig = @headers.dup
490
510
  @headers = @headers.generate
491
511
 
492
512
  self.class.active += 1
@@ -494,6 +514,7 @@ class NiceHttp
494
514
  rescue Exception => stack
495
515
  puts stack
496
516
  @logger.fatal stack
517
+ raise stack
497
518
  end
498
519
  end
499
520
 
@@ -85,10 +85,18 @@ module NiceHttpHttpMethods
85
85
  else
86
86
  @start_time_net = Time.now if @start_time_net.nil?
87
87
  resp = @http.get(path, headers_t)
88
- if resp.code == 401 and @prev_request[:contains_lambda]
89
- @logger.warn "Not authorized. Trying to generate a new token."
90
- path, data, headers_t = manage_request(arg)
91
- resp = @http.get(path, headers_t)
88
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
89
+ try = false
90
+ @headers_orig.each do |k,v|
91
+ if v.is_a?(Proc) and headers_t.key?(k)
92
+ try = true
93
+ headers_t[k] = v.call
94
+ end
95
+ end
96
+ if try
97
+ @logger.warn "Not authorized. Trying to generate a new token."
98
+ resp = @http.get(path, headers_t)
99
+ end
92
100
  end
93
101
  data = resp.body
94
102
  manage_response(resp, data)
@@ -99,7 +107,8 @@ module NiceHttpHttpMethods
99
107
  @http.finish()
100
108
  @http.start()
101
109
  @start_time_net = Time.now if @start_time_net.nil?
102
- resp = @http.get(path)
110
+ @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
111
+ resp = @http.get(path, headers_t)
103
112
  data = resp.body
104
113
  manage_response(resp, data)
105
114
  end
@@ -210,10 +219,18 @@ module NiceHttpHttpMethods
210
219
  else
211
220
  resp = @http.post(path, data, headers_t)
212
221
  #todo: do it also for forms and multipart
213
- if resp.code == 401 and @prev_request[:contains_lambda]
214
- @logger.warn "Not authorized. Trying to generate a new token."
215
- path, data, headers_t = manage_request(*arguments)
216
- resp = @http.post(path, data, headers_t)
222
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
223
+ try = false
224
+ @headers_orig.each do |k,v|
225
+ if v.is_a?(Proc) and headers_t.key?(k)
226
+ try = true
227
+ headers_t[k] = v.call
228
+ end
229
+ end
230
+ if try
231
+ @logger.warn "Not authorized. Trying to generate a new token."
232
+ resp = @http.post(path, data, headers_t)
233
+ end
217
234
  end
218
235
  data = resp.body
219
236
  end
@@ -223,6 +240,7 @@ module NiceHttpHttpMethods
223
240
  @http.finish()
224
241
  @http.start()
225
242
  @start_time_net = Time.now if @start_time_net.nil?
243
+ @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
226
244
  resp, data = @http.post(path, data, headers_t)
227
245
  end
228
246
  manage_response(resp, data)
@@ -297,10 +315,18 @@ module NiceHttpHttpMethods
297
315
  begin
298
316
  @start_time_net = Time.now if @start_time_net.nil?
299
317
  resp = @http.send_request("PUT", path, data, headers_t)
300
- if resp.code == 401 and @prev_request[:contains_lambda]
301
- @logger.warn "Not authorized. Trying to generate a new token."
302
- path, data, headers_t = manage_request(*arguments)
303
- resp = @http.send_request("PUT", path, data, headers_t)
318
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
319
+ try = false
320
+ @headers_orig.each do |k,v|
321
+ if v.is_a?(Proc) and headers_t.key?(k)
322
+ try = true
323
+ headers_t[k] = v.call
324
+ end
325
+ end
326
+ if try
327
+ @logger.warn "Not authorized. Trying to generate a new token."
328
+ resp = @http.send_request("PUT", path, data, headers_t)
329
+ end
304
330
  end
305
331
  data = resp.body
306
332
  rescue Exception => stack
@@ -308,6 +334,7 @@ module NiceHttpHttpMethods
308
334
  @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
309
335
  @http.finish()
310
336
  @http.start()
337
+ @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
311
338
  @start_time_net = Time.now if @start_time_net.nil?
312
339
  resp, data = @http.send_request("PUT", path, data, headers_t)
313
340
  end
@@ -369,10 +396,18 @@ module NiceHttpHttpMethods
369
396
  begin
370
397
  @start_time_net = Time.now if @start_time_net.nil?
371
398
  resp = @http.patch(path, data, headers_t)
372
- if resp.code == 401 and @prev_request[:contains_lambda]
373
- @logger.warn "Not authorized. Trying to generate a new token."
374
- path, data, headers_t = manage_request(*arguments)
375
- resp = @http.patch(path, data, headers_t)
399
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
400
+ try = false
401
+ @headers_orig.each do |k,v|
402
+ if v.is_a?(Proc) and headers_t.key?(k)
403
+ try = true
404
+ headers_t[k] = v.call
405
+ end
406
+ end
407
+ if try
408
+ @logger.warn "Not authorized. Trying to generate a new token."
409
+ resp = @http.patch(path, data, headers_t)
410
+ end
376
411
  end
377
412
  data = resp.body
378
413
  rescue Exception => stack
@@ -380,6 +415,7 @@ module NiceHttpHttpMethods
380
415
  @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
381
416
  @http.finish()
382
417
  @http.start()
418
+ @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
383
419
  @start_time_net = Time.now if @start_time_net.nil?
384
420
  resp, data = @http.patch(path, data, headers_t)
385
421
  end
@@ -456,19 +492,37 @@ module NiceHttpHttpMethods
456
492
  @start_time_net = Time.now if @start_time_net.nil?
457
493
  if data.to_s == ""
458
494
  resp = @http.delete(path, headers_t)
459
- if resp.code == 401 and @prev_request[:contains_lambda]
460
- @logger.warn "Not authorized. Trying to generate a new token."
461
- path, data, headers_t = manage_request(argument)
462
- resp = @http.delete(path, headers_t)
495
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
496
+ try = false
497
+ @headers_orig.each do |k,v|
498
+ if v.is_a?(Proc) and headers_t.key?(k)
499
+ try = true
500
+ headers_t[k] = v.call
501
+ end
502
+ end
503
+ if try
504
+ @logger.warn "Not authorized. Trying to generate a new token."
505
+ resp = @http.delete(path, headers_t)
506
+ end
463
507
  end
464
508
  else
465
509
  request = Net::HTTP::Delete.new(path, headers_t)
466
510
  request.body = data
467
511
  resp = @http.request(request)
468
- if resp.code == 401 and @prev_request[:contains_lambda]
469
- @logger.warn "Not authorized. Trying to generate a new token."
470
- path, data, headers_t = manage_request(argument)
471
- resp = @http.request(request)
512
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
513
+ try = false
514
+ @headers_orig.each do |k,v|
515
+ if v.is_a?(Proc) and headers_t.key?(k)
516
+ try = true
517
+ headers_t[k] = v.call
518
+ end
519
+ end
520
+ if try
521
+ @logger.warn "Not authorized. Trying to generate a new token."
522
+ request = Net::HTTP::Delete.new(path, headers_t)
523
+ request.body = data
524
+ resp = @http.request(request)
525
+ end
472
526
  end
473
527
  end
474
528
  data = resp.body
@@ -477,8 +531,9 @@ module NiceHttpHttpMethods
477
531
  @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
478
532
  @http.finish()
479
533
  @http.start()
534
+ @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
480
535
  @start_time_net = Time.now if @start_time_net.nil?
481
- resp, data = @http.delete(path)
536
+ resp, data = @http.delete(path, headers_t)
482
537
  end
483
538
  manage_response(resp, data)
484
539
 
@@ -523,10 +578,18 @@ module NiceHttpHttpMethods
523
578
  begin
524
579
  @start_time_net = Time.now if @start_time_net.nil?
525
580
  resp = @http.head(path, headers_t)
526
- if resp.code == 401 and @prev_request[:contains_lambda]
527
- @logger.warn "Not authorized. Trying to generate a new token."
528
- path, data, headers_t = manage_request(argument)
529
- resp = @http.head(path, headers_t)
581
+ if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
582
+ try = false
583
+ @headers_orig.each do |k,v|
584
+ if v.is_a?(Proc) and headers_t.key?(k)
585
+ try = true
586
+ headers_t[k] = v.call
587
+ end
588
+ end
589
+ if try
590
+ @logger.warn "Not authorized. Trying to generate a new token."
591
+ resp = @http.head(path, headers_t)
592
+ end
530
593
  end
531
594
  data = resp.body
532
595
  rescue Exception => stack
@@ -534,8 +597,9 @@ module NiceHttpHttpMethods
534
597
  @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
535
598
  @http.finish()
536
599
  @http.start()
600
+ @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
537
601
  @start_time_net = Time.now if @start_time_net.nil?
538
- resp, data = @http.head(path)
602
+ resp, data = @http.head(path, headers_t)
539
603
  end
540
604
  manage_response(resp, data)
541
605
  return @response
@@ -13,7 +13,10 @@ module NiceHttpManageRequest
13
13
  require "json"
14
14
 
15
15
  @prev_request = Hash.new() if @prev_request.nil?
16
- @prev_request[:contains_lambda] = false
16
+ @defaults_request = self.class.requests if @defaults_request.nil? and self.class.requests.is_a?(Hash)
17
+ @request = Hash.new() if @request.nil?
18
+ @defaults_request = Hash.new() unless @defaults_request.is_a?(Hash)
19
+
17
20
  begin
18
21
  content_type_included = false
19
22
  path = ""
@@ -21,6 +24,8 @@ module NiceHttpManageRequest
21
24
 
22
25
  @response = Hash.new()
23
26
  headers_t = @headers.dup()
27
+ headers_t.merge!(@defaults_request[:headers]) if @defaults_request.key?(:headers)
28
+
24
29
  cookies_to_set_str = ""
25
30
  if arguments.size == 3
26
31
  path = arguments[0]
@@ -125,6 +130,13 @@ module NiceHttpManageRequest
125
130
  }
126
131
  end
127
132
  elsif data.kind_of?(Hash)
133
+ data.merge!(@defaults_request[:data]) if @defaults_request.key?(:data)
134
+ #lambdas on data only supports on root of the hash
135
+ data.each do |k, v|
136
+ if v.is_a?(Proc)
137
+ data[k] = v.call
138
+ end
139
+ end
128
140
  if arguments[0].include?(:values_for)
129
141
  data = data.set_values(arguments[0][:values_for])
130
142
  end
@@ -192,6 +204,40 @@ module NiceHttpManageRequest
192
204
  headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
193
205
  end
194
206
 
207
+ if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
208
+ data = data.to_s().encode(encoding, "UTF-8")
209
+ end
210
+ @request[:path] = path
211
+ @request[:data] = data
212
+ @request[:headers] = headers_t
213
+ @request[:method] = method_s.upcase
214
+ if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
215
+ @request[:name] = arguments[0][:name]
216
+ end
217
+ self.class.request = @request
218
+ headers_t.each do |k, v|
219
+ # for lambdas
220
+ if v.is_a?(Proc)
221
+ headers_t[k] = v.call
222
+ end
223
+ end
224
+ @request[:headers] = headers_t
225
+ self.class.request = @request
226
+
227
+ if @debug or @prev_request[:path] != path or @prev_request[:headers] != headers_t or @prev_request[:data] != data
228
+ show_headers_data = true
229
+ else
230
+ show_headers_data = false
231
+ end
232
+
233
+ @prev_request[:path] = path
234
+ @prev_request[:data] = data
235
+ @prev_request[:headers] = headers_t
236
+ @prev_request[:method] = method_s.upcase
237
+ if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
238
+ @prev_request[:name] = arguments[0][:name]
239
+ end
240
+
195
241
  headers_ts = ""
196
242
 
197
243
  if @log_headers == :none
@@ -217,7 +263,7 @@ module NiceHttpManageRequest
217
263
  message += "#{method_s.upcase} Request"
218
264
  end
219
265
  message += "\n path: " + path.to_s() + "\n"
220
- if @debug or @prev_request[:path] != path or @prev_request[:headers] != headers_t or @prev_request[:data] != data
266
+ if show_headers_data
221
267
  message += " headers: {" + headers_ts.to_s() + "}\n"
222
268
  message += " data: " + data_s.to_s() + "\n"
223
269
  message = @message_server + "\n" + message
@@ -232,23 +278,6 @@ module NiceHttpManageRequest
232
278
  @logger.info(message)
233
279
  end
234
280
 
235
- if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
236
- data = data.to_s().encode(encoding, "UTF-8")
237
- end
238
- headers_t.each do |k, v|
239
- # for lambdas
240
- if v.is_a?(Proc)
241
- headers_t[k] = v.call
242
- @prev_request[:contains_lambda] = true
243
- end
244
- end
245
- @prev_request[:path] = path
246
- @prev_request[:data] = data
247
- @prev_request[:headers] = headers_t
248
- @prev_request[:method] = method_s.upcase
249
- if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
250
- @prev_request[:name] = arguments[0][:name]
251
- end
252
281
  return path, data, headers_t
253
282
  rescue Exception => stack
254
283
  @logger.fatal(stack)
metadata CHANGED
@@ -1,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.2
4
+ version: 1.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-04 00:00:00.000000000 Z
11
+ date: 2020-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nice_hash
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.15'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 1.15.4
19
+ version: '1.17'
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.17'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '1.15'
30
27
  - - ">="
31
28
  - !ruby/object:Gem::Version
32
- version: 1.15.4
29
+ version: '1.17'
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.17'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rspec
35
35
  requirement: !ruby/object:Gem::Requirement