nxt_http_client 0.1.9 → 0.2.0
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 +14 -4
- data/lib/nxt_http_client/client.rb +13 -3
- data/lib/nxt_http_client/default_config.rb +1 -1
- data/lib/nxt_http_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63b2822eb210af59ee6f1c93d4eb3f7e7cae8d2e8f3d0d7af736a6b4ab16409e
|
4
|
+
data.tar.gz: 8508f16697f48fbb517c64b9c6663103b8a18f35fcb2bee2fc038e9d4e2613df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a22e52e7f2dc4f8444d514c615f8f7972684dde4225720080290360c2cbb2f2df0f59e492f27ba588f0187619d88d9c6c392425dcebdd0e02c1786eaf026a4c7
|
7
|
+
data.tar.gz: 67cb746af5f0844b548bcaaa40aca1cdcb298080de1e4e5b1e049c1b4ad0f7ffcccbc364439dcbb3deb3f90236806cecb9a14a9b33c7535bfa11b3eddb4c6ba0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -28,12 +28,17 @@ class MyClient < NxtHttpClient
|
|
28
28
|
|
29
29
|
configure do |config|
|
30
30
|
config.base_url = 'www.example.com'
|
31
|
-
|
31
|
+
# In your subclasses you probably want to deep_merge options in order to not overwrite options inherited
|
32
|
+
# from the parent class. Of course this will not influence the parent class and you can also reset them
|
33
|
+
# to a new hash here.
|
34
|
+
config.request_options.deep_merge!(
|
32
35
|
headers: { API_KEY: '1993' },
|
33
36
|
method: :get,
|
34
37
|
followlocation: true
|
35
|
-
|
36
|
-
|
38
|
+
)
|
39
|
+
# Be aware that the result of x_request_id_proc will be hashed into the cache key and thus might cause
|
40
|
+
# your request not to be cached if not used properly
|
41
|
+
config.x_request_id_proc = -> { ('a'..'z').to_a.shuffle.take(10).join }
|
37
42
|
end
|
38
43
|
|
39
44
|
register_response_handler do |handler|
|
@@ -50,7 +55,12 @@ class MyClient < NxtHttpClient
|
|
50
55
|
end
|
51
56
|
|
52
57
|
after_fire do |request, result, response|
|
53
|
-
# Will be called after fire
|
58
|
+
# Will be called after fire. You probably want to return the result here in order for your code
|
59
|
+
# to be able to access the result from the response handler from before.
|
60
|
+
|
61
|
+
# In case one of the response handler callbacks raises an error
|
62
|
+
# after fire will receive the error as the result and you may want to reraise the error in that case
|
63
|
+
raise result if result.is_a?(StandardError)
|
54
64
|
end
|
55
65
|
|
56
66
|
def fetch_details
|
@@ -10,6 +10,10 @@ module NxtHttpClient
|
|
10
10
|
opts = default_config.request_options.with_indifferent_access.deep_merge(opts.with_indifferent_access)
|
11
11
|
opts[:headers] ||= {}
|
12
12
|
|
13
|
+
if default_config.x_request_id_proc
|
14
|
+
opts[:headers]['X-Request-ID'] ||= default_config.x_request_id_proc.call
|
15
|
+
end
|
16
|
+
|
13
17
|
if opts[:cache] ||= false
|
14
18
|
strategy = opts.delete(:cache)
|
15
19
|
|
@@ -52,15 +56,21 @@ module NxtHttpClient
|
|
52
56
|
end
|
53
57
|
|
54
58
|
result = nil
|
59
|
+
error = nil
|
55
60
|
|
56
61
|
request.on_complete do |response|
|
57
62
|
callback = response_handler.callback_for_response(response)
|
58
63
|
result = callback && instance_exec(response, &callback) || response
|
59
|
-
|
64
|
+
rescue StandardError => e
|
65
|
+
error = e
|
66
|
+
ensure
|
60
67
|
after_fire_callback = self.class.after_fire_callback
|
61
|
-
after_fire_callback && instance_exec(request, response, result, &after_fire_callback)
|
62
68
|
|
63
|
-
|
69
|
+
if after_fire_callback
|
70
|
+
result = instance_exec(request, response, (result || error), &after_fire_callback)
|
71
|
+
else
|
72
|
+
result || (raise error)
|
73
|
+
end
|
64
74
|
end
|
65
75
|
|
66
76
|
request.run
|
@@ -2,7 +2,7 @@ module NxtHttpClient
|
|
2
2
|
CONFIGURABLE_OPTIONS = %i[request_options base_url x_request_id_proc]
|
3
3
|
|
4
4
|
DefaultConfig = Struct.new('DefaultConfig', *CONFIGURABLE_OPTIONS) do
|
5
|
-
def initialize(request_options: ActiveSupport::HashWithIndifferentAccess.new, base_url: '', x_request_id_proc:
|
5
|
+
def initialize(request_options: ActiveSupport::HashWithIndifferentAccess.new, base_url: '', x_request_id_proc: nil)
|
6
6
|
self.request_options = request_options
|
7
7
|
self.base_url = base_url
|
8
8
|
self.x_request_id_proc = x_request_id_proc
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Robecke
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: exe
|
13
13
|
cert_chain: []
|
14
|
-
date: 2019-08-
|
14
|
+
date: 2019-08-03 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: typhoeus
|