nxt_http_client 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f15083d70472500d85d5b14313db955407d41c0381253824df06d7b62408be81
4
- data.tar.gz: c8eac41c1f068d3cfb2892eadda053deb928e8bb1595cade76549d53e72db677
3
+ metadata.gz: 6cccf6ffc8c507e36f58161ef7bd6b6412312015f5cf53d26f9ac2ea52244b8c
4
+ data.tar.gz: bc67df2f1885c7a49dabbdf4b49f823ec2f6bd1acba006295b847a1d181ed4aa
5
5
  SHA512:
6
- metadata.gz: edaeb82434e63eaebe71ddfc93d029d91e468d800dc21b42825136e483ef4353f229df48737eb59f373bea51f5365c8d3ba6faea553b93ac4b4fa4f2eed5c376
7
- data.tar.gz: 141abc01af4c33257773c42e4d8f6ca82e59034e9ecdbd49adf837b395d0c345bbc493e0e718f2c779d1befeea4057c0ef9a1b5913315a25419349aeec1a8319
6
+ metadata.gz: 9ed4b1e266af12b23d2d7e4cbfd448a0ae5e9e9cf9fd48358f06b2d515e1d58e8db68fe29742f28908b30f9e93f51e9de9d181c30302c2b098647d23f3417631
7
+ data.tar.gz: f0f391aa5e8ea47fea76d197442b95799a9c6e9d17723dc958612d42dc499c872082b968a906a65f1367f07817ea304bb52d41addc0b1273e2bbb89460c041f4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # v1.1.0 2023-04-03
2
+ - Introduce a Typhoeus::Hydra interface for batch executions
3
+
1
4
  # v1.0.4 2022-06-08
2
5
  - Fix bug where callbacks were shared between unrelated child client class ([#130](https://github.com/nxt-insurance/nxt_http_client/pull/130))
3
6
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_http_client (1.0.4)
4
+ nxt_http_client (1.1.0)
5
5
  activesupport (< 8.0)
6
6
  nxt_registry
7
7
  typhoeus
@@ -9,7 +9,7 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activesupport (7.0.3)
12
+ activesupport (7.0.4.3)
13
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
14
  i18n (>= 1.6, < 2)
15
15
  minitest (>= 5.1)
@@ -17,18 +17,18 @@ GEM
17
17
  addressable (2.8.0)
18
18
  public_suffix (>= 2.0.2, < 5.0)
19
19
  coderay (1.1.3)
20
- concurrent-ruby (1.1.10)
20
+ concurrent-ruby (1.2.2)
21
21
  crack (0.4.5)
22
22
  rexml
23
23
  diff-lcs (1.5.0)
24
- ethon (0.15.0)
24
+ ethon (0.16.0)
25
25
  ffi (>= 1.15.0)
26
26
  ffi (1.15.5)
27
27
  hashdiff (1.0.1)
28
- i18n (1.10.0)
28
+ i18n (1.12.0)
29
29
  concurrent-ruby (~> 1.0)
30
30
  method_source (1.0.0)
31
- minitest (5.15.0)
31
+ minitest (5.18.0)
32
32
  nxt_registry (0.3.10)
33
33
  activesupport
34
34
  nxt_vcr_harness (0.1.4)
@@ -59,7 +59,7 @@ GEM
59
59
  timecop (0.9.5)
60
60
  typhoeus (1.4.0)
61
61
  ethon (>= 0.9.0)
62
- tzinfo (2.0.4)
62
+ tzinfo (2.0.6)
63
63
  concurrent-ruby (~> 1.0)
64
64
  vcr (6.1.0)
65
65
  webmock (3.14.0)
@@ -0,0 +1,26 @@
1
+ module NxtHttpClient
2
+ def self.execute_in_batch(*client_instances, ignore_around_callbacks: false, raise_errors: true)
3
+ client_map = Hash.new do |hash, key|
4
+ hash[key] = { request: nil, error: nil, result: nil }
5
+ end
6
+
7
+ client_instances.each do |client|
8
+ client.singleton_class.include(NxtHttpClient::Client::BatchPatch)
9
+ client.assign_batch_data(client_map[client], ignore_around_callbacks)
10
+ end
11
+
12
+ hydra = Typhoeus::Hydra.new
13
+
14
+ client_instances.each do |client|
15
+ client.call.tap do |request|
16
+ hydra.queue(request)
17
+ end
18
+ end
19
+
20
+ hydra.run
21
+
22
+ client_map.map do |client, response_data|
23
+ client.finish(response_data[:request], response_data[:result], response_data[:error], raise_errors: raise_errors)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,55 @@
1
+ module NxtHttpClient
2
+ class Client
3
+ module BatchPatch
4
+ attr_reader :callback_map, :ignore_around_callbacks
5
+
6
+ def assign_batch_data(callback_map, ignore_around_callbacks)
7
+ @callback_map = callback_map
8
+ @ignore_around_callbacks = ignore_around_callbacks
9
+ end
10
+
11
+ def fire(url = '', **opts, &block)
12
+ response_handler = build_response_handler(opts[:response_handler], &block)
13
+ request = build_request(url, **opts.except(:response_handler))
14
+ callback_map[:request] = request
15
+
16
+ setup_on_headers_callback(request, response_handler)
17
+ setup_on_body_callback(request, response_handler)
18
+
19
+ request.on_complete do |response|
20
+ callback_map[:result] = callback_or_response(response, response_handler)
21
+ rescue StandardError => e
22
+ callback_map[:error] = e
23
+ end
24
+
25
+ if callbacks.any_around_callbacks? && ignore_around_callbacks != true
26
+ raise(
27
+ ArgumentError,
28
+ <<~TXT
29
+ `around_fire` callbacks are not supported when firing batches. \
30
+ Pass `ignore_around_callbacks: true` to `execute_in_batch` \
31
+ in order to acknowledge and muffle this.
32
+ TXT
33
+ )
34
+ end
35
+
36
+ run_before_fire_callbacks(request, response_handler)
37
+
38
+ request
39
+ end
40
+
41
+ def finish(request, result, error, raise_errors: true)
42
+ result = run_after_fire_callbacks(request, request.response, result, error)
43
+
44
+ case [error, raise_errors]
45
+ in [nil, _]
46
+ result
47
+ in [_, true]
48
+ raise error
49
+ in [_, false]
50
+ error
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -30,6 +30,10 @@ module NxtHttpClient
30
30
  end
31
31
  end
32
32
 
33
+ def any_around_callbacks?
34
+ registry.resolve(:around).any?
35
+ end
36
+
33
37
  def any_after_callbacks?
34
38
  registry.resolve(:after).any?
35
39
  end
@@ -1,3 +1,3 @@
1
1
  module NxtHttpClient
2
- VERSION = "1.0.4"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -11,6 +11,8 @@ require 'nxt_http_client/logger'
11
11
  require 'nxt_http_client/fire_callbacks'
12
12
  require 'nxt_http_client/client_dsl'
13
13
  require 'nxt_http_client/client'
14
+ require 'nxt_http_client/client/batch_patch'
15
+ require 'nxt_http_client/batch_execution'
14
16
  require 'nxt_http_client/error'
15
17
 
16
18
  module NxtHttpClient
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: 1.0.4
4
+ version: 1.1.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: 2022-06-08 00:00:00.000000000 Z
14
+ date: 2023-04-14 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: typhoeus
@@ -216,7 +216,9 @@ files:
216
216
  - bin/console
217
217
  - bin/setup
218
218
  - lib/nxt_http_client.rb
219
+ - lib/nxt_http_client/batch_execution.rb
219
220
  - lib/nxt_http_client/client.rb
221
+ - lib/nxt_http_client/client/batch_patch.rb
220
222
  - lib/nxt_http_client/client_dsl.rb
221
223
  - lib/nxt_http_client/config.rb
222
224
  - lib/nxt_http_client/error.rb