nxt_http_client 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -3
- data/lib/nxt_http_client.rb +1 -0
- data/lib/nxt_http_client/client.rb +7 -3
- data/lib/nxt_http_client/client_dsl.rb +50 -58
- data/lib/nxt_http_client/default_config.rb +11 -0
- data/lib/nxt_http_client/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5bdc0b6838780fe319613c5269ea5c0077cbc3ba621b69203d0902c15943c27f
|
4
|
+
data.tar.gz: 02fb6a39d21c5aa8661ddc591eee8324bf044898143f133b6f0478751a1726c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f157b49c5dc97c40650235d8828fb2f1e5d7917b65f3a42ca8ab6a517b621c6a89e747143ce782d50ecef8bb7f7c2bf1726f0daee7d78b321988758748a61d5b
|
7
|
+
data.tar.gz: 52086988a87eda709d21fa1114388411ee26e1d019366647199b039362073ff771c86064be3bc268bbce8edf9aab1dce7ad3e6aa78eed5c9ac76cdea6b9cb173
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,6 +33,7 @@ class MyClient < NxtHttpClient
|
|
33
33
|
method: :get,
|
34
34
|
followlocation: true
|
35
35
|
}
|
36
|
+
defaults.x_request_id_proc = -> { ('a'..'z').to_a.shuffle.take(10).join } # defaults to -> { SecureRandom.uuid }
|
36
37
|
end
|
37
38
|
|
38
39
|
register_response_handler do |handler|
|
@@ -41,8 +42,11 @@ class MyClient < NxtHttpClient
|
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
|
-
before_fire do |request|
|
45
|
-
# Will be called before fire
|
45
|
+
before_fire do |request, handler|
|
46
|
+
# Will be called before fire
|
47
|
+
handler.on!(200) do |response|
|
48
|
+
# reconfigure your handler before fire
|
49
|
+
end
|
46
50
|
end
|
47
51
|
|
48
52
|
after_fire do |request, result, response|
|
@@ -70,7 +74,8 @@ end
|
|
70
74
|
|
71
75
|
### register_defaults
|
72
76
|
|
73
|
-
Register default request options on the class level.
|
77
|
+
Register default request options on the class level. Available options are `request_options` that are passed directly to
|
78
|
+
the underlying Typhoeus Request. Then there is `base_url` and `x_request_id_proc`.
|
74
79
|
|
75
80
|
### register_response_handler
|
76
81
|
|
data/lib/nxt_http_client.rb
CHANGED
@@ -4,10 +4,10 @@ module NxtHttpClient
|
|
4
4
|
CACHE_STRATEGIES = %w[global thread]
|
5
5
|
|
6
6
|
def build_request(url, **opts)
|
7
|
-
base_url = opts.delete(:base_url) ||
|
7
|
+
base_url = opts.delete(:base_url) || default_config.base_url
|
8
8
|
url = [base_url, url].reject(&:blank?).join('/').gsub(/([^https?:]\/\/)/, '/')
|
9
9
|
|
10
|
-
opts =
|
10
|
+
opts = default_config.request_options.with_indifferent_access.deep_merge(opts.with_indifferent_access)
|
11
11
|
opts[:headers] ||= {}
|
12
12
|
|
13
13
|
if opts[:cache] ||= false
|
@@ -37,7 +37,7 @@ module NxtHttpClient
|
|
37
37
|
request = build_request(url, opts.except(:response_handler))
|
38
38
|
|
39
39
|
before_fire_callback = self.class.before_fire_callback
|
40
|
-
before_fire_callback && instance_exec(request, &before_fire_callback)
|
40
|
+
before_fire_callback && instance_exec(request, response_handler, &before_fire_callback)
|
41
41
|
|
42
42
|
if response_handler.callbacks['headers']
|
43
43
|
request.on_headers do |response|
|
@@ -77,5 +77,9 @@ module NxtHttpClient
|
|
77
77
|
def dup_handler_from_class
|
78
78
|
self.class.response_handler.dup
|
79
79
|
end
|
80
|
+
|
81
|
+
def default_config
|
82
|
+
self.class.default_config
|
83
|
+
end
|
80
84
|
end
|
81
85
|
end
|
@@ -1,60 +1,52 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def inherited(child)
|
54
|
-
child.instance_variable_set(:@response_handler, @response_handler.dup)
|
55
|
-
child.instance_variable_set(:@base_url, @base_url)
|
56
|
-
child.instance_variable_set(:@before_fire_callback, @before_fire_callback.dup)
|
57
|
-
child.instance_variable_set(:@after_fire_callback, @after_fire_callback.dup)
|
58
|
-
child.instance_variable_set(:@defaults, OpenStruct.new(**@defaults.to_h.deep_dup))
|
1
|
+
module NxtHttpClient
|
2
|
+
module ClientDsl
|
3
|
+
def register_defaults(opts = {}, &block)
|
4
|
+
@default_config ||= DefaultConfig.new(**opts)
|
5
|
+
@default_config.tap { |d| block.call(d) }
|
6
|
+
@default_config
|
7
|
+
end
|
8
|
+
|
9
|
+
def before_fire(&block)
|
10
|
+
@before_fire_callback = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_fire_callback
|
14
|
+
@before_fire_callback
|
15
|
+
end
|
16
|
+
|
17
|
+
def after_fire(&block)
|
18
|
+
@after_fire_callback = block
|
19
|
+
end
|
20
|
+
|
21
|
+
def after_fire_callback
|
22
|
+
@after_fire_callback
|
23
|
+
end
|
24
|
+
|
25
|
+
def default_config
|
26
|
+
@default_config ||= DefaultConfig.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def register_response_handler(handler = nil, &block)
|
30
|
+
@response_handler = handler
|
31
|
+
@response_handler ||= dup_handler_from_ancestor_or_new
|
32
|
+
@response_handler.configure(&block) if block_given?
|
33
|
+
@response_handler
|
34
|
+
end
|
35
|
+
|
36
|
+
def response_handler
|
37
|
+
@response_handler
|
38
|
+
end
|
39
|
+
|
40
|
+
def dup_handler_from_ancestor_or_new
|
41
|
+
handler_from_ancestor = ancestors[1].instance_variable_get(:@response_handler)
|
42
|
+
handler_from_ancestor && handler_from_ancestor.dup || NxtHttpClient::ResponseHandler.new
|
43
|
+
end
|
44
|
+
|
45
|
+
def inherited(child)
|
46
|
+
child.instance_variable_set(:@response_handler, @response_handler.dup)
|
47
|
+
child.instance_variable_set(:@before_fire_callback, @before_fire_callback.dup)
|
48
|
+
child.instance_variable_set(:@after_fire_callback, @after_fire_callback.dup)
|
49
|
+
child.instance_variable_set(:@default_config, DefaultConfig.new(**default_config.to_h.deep_dup))
|
50
|
+
end
|
59
51
|
end
|
60
52
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module NxtHttpClient
|
2
|
+
CONFIGURABLE_OPTIONS = %i[request_options base_url x_request_id_proc]
|
3
|
+
|
4
|
+
DefaultConfig = Struct.new('DefaultConfig', *CONFIGURABLE_OPTIONS) do
|
5
|
+
def initialize(request_options: ActiveSupport::HashWithIndifferentAccess.new, base_url: '', x_request_id_proc: -> { SecureRandom.uuid })
|
6
|
+
self.request_options = request_options
|
7
|
+
self.base_url = base_url
|
8
|
+
self.x_request_id_proc = x_request_id_proc
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nxt_http_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Robecke
|
@@ -173,6 +173,7 @@ files:
|
|
173
173
|
- lib/nxt_http_client.rb
|
174
174
|
- lib/nxt_http_client/client.rb
|
175
175
|
- lib/nxt_http_client/client_dsl.rb
|
176
|
+
- lib/nxt_http_client/default_config.rb
|
176
177
|
- lib/nxt_http_client/response_handler.rb
|
177
178
|
- lib/nxt_http_client/version.rb
|
178
179
|
- nxt_http_client.gemspec
|