nice_http 1.8.4 → 1.8.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +76 -3
- data/lib/nice_http.rb +28 -8
- data/lib/nice_http/http_methods.rb +75 -39
- data/lib/nice_http/manage_request.rb +47 -17
- metadata +15 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f88fbc1d97ebbb1dd7ea2464a9995b79d35fd6d22aefd619c0b4fc4f057d500
|
4
|
+
data.tar.gz: 9bfaa4bda75880a32123571c462286dd9ddbdd6bf2fad0af24d995021222e5cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9dd3dd96512970be6bf92bbc2a67f79f2078162d710d0d11648c007958aaef104e65d1360ac1a89b392fd85d8613160957e6d17eb342ffb35c409c4cc560f56
|
7
|
+
data.tar.gz: 39b97030cd3cc3aa7b43aa500e6c8a659aab9504617666d4b98f9a20fa36f39c62bc6ff26b60d2e430081580c52212c1ff2372a0357ed93a2ed602e428d63ca7
|
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
|
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
|
-
|
590
|
+
threads.each(&:join)
|
518
591
|
|
519
592
|
# log files: nice_http_0.log, nice_http_1.log... nice_http_39.log
|
520
593
|
```
|
data/lib/nice_http.rb
CHANGED
@@ -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: "
|
@@ -495,6 +514,7 @@ class NiceHttp
|
|
495
514
|
rescue Exception => stack
|
496
515
|
puts stack
|
497
516
|
@logger.fatal stack
|
517
|
+
raise stack
|
498
518
|
end
|
499
519
|
end
|
500
520
|
|
@@ -85,7 +85,7 @@ 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 @headers_orig.values.map(&:class).include?(Proc)
|
88
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
89
89
|
try = false
|
90
90
|
@headers_orig.each do |k,v|
|
91
91
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -103,13 +103,19 @@ module NiceHttpHttpMethods
|
|
103
103
|
end
|
104
104
|
rescue Exception => stack
|
105
105
|
@logger.warn stack
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
106
|
+
if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
|
107
|
+
@logger.warn "The connection seems to be closed in the host machine. Timeout."
|
108
|
+
return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
|
109
|
+
else
|
110
|
+
@logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
|
111
|
+
@http.finish()
|
112
|
+
@http.start()
|
113
|
+
@start_time_net = Time.now if @start_time_net.nil?
|
114
|
+
@headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
|
115
|
+
resp = @http.get(path, headers_t)
|
116
|
+
data = resp.body
|
117
|
+
manage_response(resp, data)
|
118
|
+
end
|
113
119
|
end
|
114
120
|
if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
|
115
121
|
if @num_redirects <= 30
|
@@ -218,7 +224,7 @@ module NiceHttpHttpMethods
|
|
218
224
|
else
|
219
225
|
resp = @http.post(path, data, headers_t)
|
220
226
|
#todo: do it also for forms and multipart
|
221
|
-
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
|
227
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
222
228
|
try = false
|
223
229
|
@headers_orig.each do |k,v|
|
224
230
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -235,11 +241,17 @@ module NiceHttpHttpMethods
|
|
235
241
|
end
|
236
242
|
rescue Exception => stack
|
237
243
|
@logger.warn stack
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
244
|
+
if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
|
245
|
+
@logger.warn "The connection seems to be closed in the host machine. Timeout."
|
246
|
+
return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
|
247
|
+
else
|
248
|
+
@logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
|
249
|
+
@http.finish()
|
250
|
+
@http.start()
|
251
|
+
@start_time_net = Time.now if @start_time_net.nil?
|
252
|
+
@headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
|
253
|
+
resp, data = @http.post(path, data, headers_t)
|
254
|
+
end
|
243
255
|
end
|
244
256
|
manage_response(resp, data)
|
245
257
|
if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
|
@@ -313,7 +325,7 @@ module NiceHttpHttpMethods
|
|
313
325
|
begin
|
314
326
|
@start_time_net = Time.now if @start_time_net.nil?
|
315
327
|
resp = @http.send_request("PUT", path, data, headers_t)
|
316
|
-
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
|
328
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
317
329
|
try = false
|
318
330
|
@headers_orig.each do |k,v|
|
319
331
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -329,11 +341,17 @@ module NiceHttpHttpMethods
|
|
329
341
|
data = resp.body
|
330
342
|
rescue Exception => stack
|
331
343
|
@logger.warn stack
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
344
|
+
if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
|
345
|
+
@logger.warn "The connection seems to be closed in the host machine. Timeout."
|
346
|
+
return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
|
347
|
+
else
|
348
|
+
@logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
|
349
|
+
@http.finish()
|
350
|
+
@http.start()
|
351
|
+
@headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
|
352
|
+
@start_time_net = Time.now if @start_time_net.nil?
|
353
|
+
resp, data = @http.send_request("PUT", path, data, headers_t)
|
354
|
+
end
|
337
355
|
end
|
338
356
|
manage_response(resp, data)
|
339
357
|
|
@@ -393,7 +411,7 @@ module NiceHttpHttpMethods
|
|
393
411
|
begin
|
394
412
|
@start_time_net = Time.now if @start_time_net.nil?
|
395
413
|
resp = @http.patch(path, data, headers_t)
|
396
|
-
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
|
414
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
397
415
|
try = false
|
398
416
|
@headers_orig.each do |k,v|
|
399
417
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -409,11 +427,17 @@ module NiceHttpHttpMethods
|
|
409
427
|
data = resp.body
|
410
428
|
rescue Exception => stack
|
411
429
|
@logger.warn stack
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
430
|
+
if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
|
431
|
+
@logger.warn "The connection seems to be closed in the host machine. Timeout."
|
432
|
+
return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
|
433
|
+
else
|
434
|
+
@logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
|
435
|
+
@http.finish()
|
436
|
+
@http.start()
|
437
|
+
@headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
|
438
|
+
@start_time_net = Time.now if @start_time_net.nil?
|
439
|
+
resp, data = @http.patch(path, data, headers_t)
|
440
|
+
end
|
417
441
|
end
|
418
442
|
manage_response(resp, data)
|
419
443
|
if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
|
@@ -488,7 +512,7 @@ module NiceHttpHttpMethods
|
|
488
512
|
@start_time_net = Time.now if @start_time_net.nil?
|
489
513
|
if data.to_s == ""
|
490
514
|
resp = @http.delete(path, headers_t)
|
491
|
-
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
|
515
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
492
516
|
try = false
|
493
517
|
@headers_orig.each do |k,v|
|
494
518
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -505,7 +529,7 @@ module NiceHttpHttpMethods
|
|
505
529
|
request = Net::HTTP::Delete.new(path, headers_t)
|
506
530
|
request.body = data
|
507
531
|
resp = @http.request(request)
|
508
|
-
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
|
532
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
509
533
|
try = false
|
510
534
|
@headers_orig.each do |k,v|
|
511
535
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -524,11 +548,17 @@ module NiceHttpHttpMethods
|
|
524
548
|
data = resp.body
|
525
549
|
rescue Exception => stack
|
526
550
|
@logger.warn stack
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
551
|
+
if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
|
552
|
+
@logger.warn "The connection seems to be closed in the host machine. Timeout."
|
553
|
+
return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
|
554
|
+
else
|
555
|
+
@logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
|
556
|
+
@http.finish()
|
557
|
+
@http.start()
|
558
|
+
@headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
|
559
|
+
@start_time_net = Time.now if @start_time_net.nil?
|
560
|
+
resp, data = @http.delete(path, headers_t)
|
561
|
+
end
|
532
562
|
end
|
533
563
|
manage_response(resp, data)
|
534
564
|
|
@@ -573,7 +603,7 @@ module NiceHttpHttpMethods
|
|
573
603
|
begin
|
574
604
|
@start_time_net = Time.now if @start_time_net.nil?
|
575
605
|
resp = @http.head(path, headers_t)
|
576
|
-
if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc)
|
606
|
+
if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
|
577
607
|
try = false
|
578
608
|
@headers_orig.each do |k,v|
|
579
609
|
if v.is_a?(Proc) and headers_t.key?(k)
|
@@ -589,11 +619,17 @@ module NiceHttpHttpMethods
|
|
589
619
|
data = resp.body
|
590
620
|
rescue Exception => stack
|
591
621
|
@logger.warn stack
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
622
|
+
if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
|
623
|
+
@logger.warn "The connection seems to be closed in the host machine. Timeout."
|
624
|
+
return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
|
625
|
+
else
|
626
|
+
@logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
|
627
|
+
@http.finish()
|
628
|
+
@http.start()
|
629
|
+
@headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
|
630
|
+
@start_time_net = Time.now if @start_time_net.nil?
|
631
|
+
resp, data = @http.head(path, headers_t)
|
632
|
+
end
|
597
633
|
end
|
598
634
|
manage_response(resp, data)
|
599
635
|
return @response
|
@@ -13,6 +13,9 @@ module NiceHttpManageRequest
|
|
13
13
|
require "json"
|
14
14
|
|
15
15
|
@prev_request = Hash.new() if @prev_request.nil?
|
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)
|
16
19
|
|
17
20
|
begin
|
18
21
|
content_type_included = false
|
@@ -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
|
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,22 +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
|
-
end
|
243
|
-
end
|
244
|
-
@prev_request[:path] = path
|
245
|
-
@prev_request[:data] = data
|
246
|
-
@prev_request[:headers] = headers_t
|
247
|
-
@prev_request[:method] = method_s.upcase
|
248
|
-
if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
|
249
|
-
@prev_request[:name] = arguments[0][:name]
|
250
|
-
end
|
251
281
|
return path, data, headers_t
|
252
282
|
rescue Exception => stack
|
253
283
|
@logger.fatal(stack)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nice_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Ruiz
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nice_hash
|
@@ -16,40 +16,40 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.17'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 1.
|
22
|
+
version: '1.17'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '1.
|
29
|
+
version: '1.17'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 1.
|
32
|
+
version: '1.17'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rspec
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - ">="
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: 3.8.0
|
40
37
|
- - "~>"
|
41
38
|
- !ruby/object:Gem::Version
|
42
39
|
version: '3.8'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 3.8.0
|
43
43
|
type: :development
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - ">="
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: 3.8.0
|
50
47
|
- - "~>"
|
51
48
|
- !ruby/object:Gem::Version
|
52
49
|
version: '3.8'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 3.8.0
|
53
53
|
description: NiceHttp -- simplest library for accessing and testing HTTP and REST
|
54
54
|
resources. Get http logs and statistics automatically. Use hashes on your requests.
|
55
55
|
Access JSON even easier.
|
@@ -87,8 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
89
|
requirements: []
|
90
|
-
rubygems_version: 3.
|
91
|
-
signing_key:
|
90
|
+
rubygems_version: 3.2.3
|
91
|
+
signing_key:
|
92
92
|
specification_version: 4
|
93
93
|
summary: NiceHttp -- simplest library for accessing and testing HTTP and REST resources.
|
94
94
|
Get http logs and statistics automatically. Use hashes on your requests. Access
|