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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe54eb2ecb7b7c7025699424d538ab43c730dd93728557b427551317e6c60363
4
- data.tar.gz: 703d8d45fe804609d39ded9806ecc6c0771a3dba9cb3414df890ffed6589bcca
3
+ metadata.gz: 63b2822eb210af59ee6f1c93d4eb3f7e7cae8d2e8f3d0d7af736a6b4ab16409e
4
+ data.tar.gz: 8508f16697f48fbb517c64b9c6663103b8a18f35fcb2bee2fc038e9d4e2613df
5
5
  SHA512:
6
- metadata.gz: 68dbda40d531999f1f9fde9bfa17726ec6801f6f6d50db4a365dfc4586a1bdae3006d98c792a7f3fa58bc20144484a4044cd0e854b9a30ea4b98059bfb59bc82
7
- data.tar.gz: a66fbc5a9aba10fbb4fbcf90d384e6bfd6bb5ca591bb7e8e8507670453167c84bee06390e69223bf6948b4c1251ecc5285db2ef298f121b41d90f5fc7504f40f
6
+ metadata.gz: a22e52e7f2dc4f8444d514c615f8f7972684dde4225720080290360c2cbb2f2df0f59e492f27ba588f0187619d88d9c6c392425dcebdd0e02c1786eaf026a4c7
7
+ data.tar.gz: 67cb746af5f0844b548bcaaa40aca1cdcb298080de1e4e5b1e049c1b4ad0f7ffcccbc364439dcbb3deb3f90236806cecb9a14a9b33c7535bfa11b3eddb4c6ba0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_http_client (0.1.9)
4
+ nxt_http_client (0.2.0)
5
5
  activesupport (~> 5.2)
6
6
  typhoeus
7
7
 
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
- config.request_options = {
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
- config.x_request_id_proc = -> { ('a'..'z').to_a.shuffle.take(10).join } # config to -> { SecureRandom.uuid }
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
- result
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: -> { SecureRandom.uuid })
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
@@ -1,3 +1,3 @@
1
1
  module NxtHttpClient
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  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.9
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-02 00:00:00.000000000 Z
14
+ date: 2019-08-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: typhoeus