nxt_http_client 0.2.9 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +57 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile.lock +48 -46
- data/README.md +53 -14
- data/lib/nxt_http_client.rb +0 -1
- data/lib/nxt_http_client/client.rb +70 -50
- data/lib/nxt_http_client/client_dsl.rb +5 -12
- data/lib/nxt_http_client/default_config.rb +1 -1
- data/lib/nxt_http_client/error.rb +11 -4
- data/lib/nxt_http_client/response_handler.rb +17 -15
- data/lib/nxt_http_client/version.rb +1 -1
- data/nxt_http_client.gemspec +3 -2
- metadata +23 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89185b814fea114ee2d74dd8e05afd3cf4773cdef8fb908cdf5e954775b4e885
|
4
|
+
data.tar.gz: 2d940e5c42051c09ca776d57901535a7a3d18bf8facb116b742f04a46b91e2ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dea66682adbebf9800a68cbb3f9962d4b17623bc022163b7765fd1b4a889a78fc610dfc12719d137605e9e3b7c98b254e6f8f5ec80eca947318795b66dd48a2
|
7
|
+
data.tar.gz: 8f04ddb779773c29db1a6f1af0cc73065b4da4e19e7e1a0b43eb5e1c6ec09d94acefb47ca0bdff0880377e2366354a631bc6751adad2ab73c31161108d2fa540
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
#
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
4
|
+
#
|
5
|
+
version: 2
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
docker:
|
9
|
+
- image: circleci/ruby:2.7.0-node
|
10
|
+
- image: circleci/redis:5.0.4
|
11
|
+
environment:
|
12
|
+
BUNDLER_VERSION: 2.1.4
|
13
|
+
|
14
|
+
working_directory: ~/repo
|
15
|
+
|
16
|
+
steps:
|
17
|
+
- checkout
|
18
|
+
|
19
|
+
# Download and cache dependencies
|
20
|
+
- restore_cache:
|
21
|
+
keys:
|
22
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
23
|
+
|
24
|
+
- run: gem install bundler --version $BUNDLER_VERSION
|
25
|
+
|
26
|
+
- run:
|
27
|
+
name: install dependencies
|
28
|
+
command: |
|
29
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
30
|
+
|
31
|
+
- save_cache:
|
32
|
+
paths:
|
33
|
+
- ./vendor/bundle
|
34
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
35
|
+
|
36
|
+
# run tests!
|
37
|
+
- run:
|
38
|
+
name: run tests
|
39
|
+
command: |
|
40
|
+
mkdir /tmp/test-results
|
41
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
|
42
|
+
circleci tests split --split-by=timings)"
|
43
|
+
|
44
|
+
bundle exec rspec \
|
45
|
+
--format progress \
|
46
|
+
--format RspecJunitFormatter \
|
47
|
+
--out /tmp/test-results/rspec.xml \
|
48
|
+
--format progress \
|
49
|
+
$TEST_FILES
|
50
|
+
|
51
|
+
# collect reports
|
52
|
+
- store_artifacts:
|
53
|
+
path: /tmp/rspec/
|
54
|
+
destination: rspec
|
55
|
+
|
56
|
+
- store_test_results:
|
57
|
+
path: /tmp/rspec/
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# v0.3.4 2021-01-05
|
2
|
+
|
3
|
+
### Updated
|
4
|
+
|
5
|
+
- Loosen ActiveSupport version requirement to allow 6.1
|
6
|
+
|
7
|
+
# v0.3.3 2020-09-30
|
8
|
+
|
9
|
+
### Updated NxtRegistry
|
10
|
+
|
11
|
+
[Compare v0.3.2...v0.3.3](https://github.com/nxt-insurance/nxt_http_client/compare/v0.3.2...v0.3.3)
|
12
|
+
|
13
|
+
# v0.2.10 2020-03-10
|
14
|
+
|
15
|
+
### Refactored
|
16
|
+
|
17
|
+
- [internal] Added CHANGELOG.MD
|
18
|
+
- Refactored a bit
|
19
|
+
|
20
|
+
[Compare v0.2.9...v0.3.0](https://github.com/nxt-insurance/nxt_http_client/compare/v0.2.9...v0.2.10)
|
data/Gemfile.lock
CHANGED
@@ -1,71 +1,72 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_http_client (0.
|
5
|
-
activesupport (~> 6.0
|
4
|
+
nxt_http_client (0.3.4)
|
5
|
+
activesupport (~> 6.0)
|
6
6
|
nxt_registry
|
7
7
|
typhoeus
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activesupport (6.0
|
12
|
+
activesupport (6.1.0)
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
-
i18n (>=
|
15
|
-
minitest (
|
16
|
-
tzinfo (~>
|
17
|
-
zeitwerk (~> 2.
|
14
|
+
i18n (>= 1.6, < 2)
|
15
|
+
minitest (>= 5.1)
|
16
|
+
tzinfo (~> 2.0)
|
17
|
+
zeitwerk (~> 2.3)
|
18
18
|
addressable (2.7.0)
|
19
19
|
public_suffix (>= 2.0.2, < 5.0)
|
20
|
-
coderay (1.1.
|
21
|
-
concurrent-ruby (1.1.
|
22
|
-
crack (0.4.
|
23
|
-
|
24
|
-
diff-lcs (1.
|
20
|
+
coderay (1.1.3)
|
21
|
+
concurrent-ruby (1.1.7)
|
22
|
+
crack (0.4.5)
|
23
|
+
rexml
|
24
|
+
diff-lcs (1.4.4)
|
25
25
|
ethon (0.12.0)
|
26
26
|
ffi (>= 1.3.0)
|
27
|
-
ffi (1.
|
28
|
-
hashdiff (1.0.
|
29
|
-
i18n (1.8.
|
27
|
+
ffi (1.13.1)
|
28
|
+
hashdiff (1.0.1)
|
29
|
+
i18n (1.8.7)
|
30
30
|
concurrent-ruby (~> 1.0)
|
31
|
-
method_source (0.
|
32
|
-
minitest (5.14.
|
33
|
-
nxt_registry (0.
|
31
|
+
method_source (1.0.0)
|
32
|
+
minitest (5.14.2)
|
33
|
+
nxt_registry (0.3.6)
|
34
34
|
activesupport
|
35
|
-
nxt_vcr_harness (0.1.
|
35
|
+
nxt_vcr_harness (0.1.4)
|
36
36
|
rspec (~> 3.0)
|
37
|
-
vcr (~>
|
38
|
-
pry (0.
|
39
|
-
coderay (~> 1.1
|
40
|
-
method_source (~>
|
41
|
-
public_suffix (4.0.
|
42
|
-
rake (
|
43
|
-
redis (4.
|
44
|
-
|
45
|
-
|
46
|
-
rspec-
|
47
|
-
rspec-
|
48
|
-
|
49
|
-
|
50
|
-
|
37
|
+
vcr (~> 6.0)
|
38
|
+
pry (0.13.1)
|
39
|
+
coderay (~> 1.1)
|
40
|
+
method_source (~> 1.0)
|
41
|
+
public_suffix (4.0.6)
|
42
|
+
rake (13.0.3)
|
43
|
+
redis (4.2.5)
|
44
|
+
rexml (3.2.4)
|
45
|
+
rspec (3.10.0)
|
46
|
+
rspec-core (~> 3.10.0)
|
47
|
+
rspec-expectations (~> 3.10.0)
|
48
|
+
rspec-mocks (~> 3.10.0)
|
49
|
+
rspec-core (3.10.1)
|
50
|
+
rspec-support (~> 3.10.0)
|
51
|
+
rspec-expectations (3.10.1)
|
51
52
|
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
-
rspec-support (~> 3.
|
53
|
-
rspec-mocks (3.
|
53
|
+
rspec-support (~> 3.10.0)
|
54
|
+
rspec-mocks (3.10.1)
|
54
55
|
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
-
rspec-support (~> 3.
|
56
|
-
rspec-support (3.
|
57
|
-
|
58
|
-
|
59
|
-
typhoeus (1.
|
56
|
+
rspec-support (~> 3.10.0)
|
57
|
+
rspec-support (3.10.1)
|
58
|
+
rspec_junit_formatter (0.4.1)
|
59
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
60
|
+
typhoeus (1.4.0)
|
60
61
|
ethon (>= 0.9.0)
|
61
|
-
tzinfo (
|
62
|
-
|
63
|
-
vcr (
|
64
|
-
webmock (3.
|
62
|
+
tzinfo (2.0.4)
|
63
|
+
concurrent-ruby (~> 1.0)
|
64
|
+
vcr (6.0.0)
|
65
|
+
webmock (3.11.0)
|
65
66
|
addressable (>= 2.3.6)
|
66
67
|
crack (>= 0.3.2)
|
67
68
|
hashdiff (>= 0.4.0, < 2.0.0)
|
68
|
-
zeitwerk (2.
|
69
|
+
zeitwerk (2.4.2)
|
69
70
|
|
70
71
|
PLATFORMS
|
71
72
|
ruby
|
@@ -75,9 +76,10 @@ DEPENDENCIES
|
|
75
76
|
nxt_http_client!
|
76
77
|
nxt_vcr_harness
|
77
78
|
pry
|
78
|
-
rake (~>
|
79
|
+
rake (~> 13.0)
|
79
80
|
redis
|
80
81
|
rspec (~> 3.0)
|
82
|
+
rspec_junit_formatter
|
81
83
|
vcr
|
82
84
|
webmock
|
83
85
|
|
data/README.md
CHANGED
@@ -26,40 +26,43 @@ Or install it yourself as:
|
|
26
26
|
```ruby
|
27
27
|
class MyClient < NxtHttpClient
|
28
28
|
|
29
|
+
# In your subclasses you probably want to deep_merge options in order to not overwrite options inherited
|
30
|
+
# from the parent class. Of course this will not influence the parent class and you can also reset them
|
31
|
+
# to a new hash here.
|
32
|
+
|
33
|
+
# Also be aware that the result of x_request_id_proc will be hashed into the cache key and thus might cause
|
34
|
+
# your request not to be cached if not used properly
|
35
|
+
|
29
36
|
configure do |config|
|
30
37
|
config.base_url = 'www.example.com'
|
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
38
|
config.request_options.deep_merge!(
|
35
39
|
headers: { API_KEY: '1993' },
|
36
40
|
method: :get,
|
37
41
|
followlocation: true
|
38
42
|
)
|
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
43
|
config.x_request_id_proc = -> { ('a'..'z').to_a.shuffle.take(10).join }
|
42
44
|
end
|
43
45
|
|
44
46
|
register_response_handler do |handler|
|
45
47
|
handler.on(:error) do |response|
|
48
|
+
Raven.extra_context(error_details: error.to_h) # call error.to_h to inspect request and response
|
46
49
|
raise StandardError, "I can't handle this: #{response.code}"
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
53
|
+
# Will be called before fire so you can reconfigure your handler before fire
|
50
54
|
before_fire do |client, request, handler|
|
51
|
-
# Will be called before fire
|
52
55
|
handler.on!(200) do |response|
|
53
|
-
#
|
56
|
+
# ...
|
54
57
|
end
|
55
58
|
end
|
56
59
|
|
60
|
+
# Will be called after fire. You probably want to return the result here in order for your code
|
61
|
+
# to be able to access the result from the response handler from before.
|
62
|
+
# In case one of the response handler callbacks raises an error
|
63
|
+
# after fire will has access to it and you may want to reraise the error in that case.
|
64
|
+
|
57
65
|
after_fire do |client, request, response, result, error|
|
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 has access to it and you may want to reraise the error in that case.
|
63
66
|
if error
|
64
67
|
raise error
|
65
68
|
else
|
@@ -84,15 +87,51 @@ class MyClient < NxtHttpClient
|
|
84
87
|
end
|
85
88
|
end
|
86
89
|
|
90
|
+
# there are also convenience methods for all http verbs (get post patch put delete head)
|
87
91
|
def update
|
88
|
-
# there are also convenience methods for all http verbs (get post patch put delete head)
|
89
92
|
post(params: { my: 'payload' }) do |handler|
|
90
|
-
|
93
|
+
# ...
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
### HTTP Methods
|
100
|
+
|
101
|
+
Instead of fire you can simply use the http verbs as methods
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class MyClient < NxtHttpClient
|
105
|
+
|
106
|
+
def initialize(url)
|
107
|
+
@url = url
|
108
|
+
end
|
109
|
+
|
110
|
+
attr_reader :url
|
111
|
+
|
112
|
+
def fetch
|
113
|
+
get(url) do
|
114
|
+
handler.on(:success) { |response| response.body }
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def create(params)
|
119
|
+
post(url, params: params) do
|
120
|
+
handler.on(:success) { |response| response.body }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def update(params)
|
125
|
+
put(url, params: params) do
|
126
|
+
handler.on(:success) { |response| response.body }
|
91
127
|
end
|
92
128
|
end
|
129
|
+
|
130
|
+
# ... there are others as you know ...
|
93
131
|
end
|
94
132
|
```
|
95
133
|
|
134
|
+
|
96
135
|
### configure
|
97
136
|
|
98
137
|
Register default request options on the class level. Available options are `request_options` that are passed directly to
|
data/lib/nxt_http_client.rb
CHANGED
@@ -1,80 +1,48 @@
|
|
1
1
|
module NxtHttpClient
|
2
2
|
class Client
|
3
3
|
extend ClientDsl
|
4
|
-
CACHE_STRATEGIES = %w[global thread]
|
4
|
+
CACHE_STRATEGIES = %w[global thread].freeze
|
5
|
+
HTTP_METHODS = %w[get post patch put delete head].freeze
|
5
6
|
|
6
7
|
def build_request(url, **opts)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
duplicated_slashes = url.match(/([^:]\/{2,})/)
|
11
|
-
duplicated_slashes && duplicated_slashes.captures.each do |capture|
|
12
|
-
url.gsub!(capture, "#{capture[0]}/")
|
13
|
-
end
|
14
|
-
|
15
|
-
opts = default_config.request_options.with_indifferent_access.deep_merge(opts.with_indifferent_access)
|
16
|
-
opts[:headers] ||= {}
|
17
|
-
|
18
|
-
if default_config.x_request_id_proc
|
19
|
-
opts[:headers]['X-Request-ID'] ||= default_config.x_request_id_proc.call
|
20
|
-
end
|
21
|
-
|
22
|
-
if opts[:cache] ||= false
|
23
|
-
strategy = opts.delete(:cache)
|
24
|
-
|
25
|
-
case strategy.to_s
|
26
|
-
when 'thread'
|
27
|
-
cache_key = Thread.current[:nxt_http_client_cache_key] ||= "#{SecureRandom.base58}::#{DateTime.current}"
|
28
|
-
opts[:headers].reverse_merge!(cache_key: cache_key)
|
29
|
-
when 'global'
|
30
|
-
opts[:headers].delete(:cache_key)
|
31
|
-
else
|
32
|
-
raise ArgumentError, "Cache strategy unknown: #{strategy}. Options are #{CACHE_STRATEGIES}"
|
33
|
-
end
|
34
|
-
end
|
8
|
+
url = build_url(opts, url)
|
9
|
+
opts = build_headers(opts)
|
35
10
|
|
36
11
|
Typhoeus::Request.new(url, **opts.symbolize_keys)
|
37
12
|
end
|
38
13
|
|
39
|
-
|
40
|
-
# calling_method = caller_locations(1,1)[0].label
|
41
|
-
response_handler = opts.fetch(:response_handler) do
|
42
|
-
dup_handler_from_class || NxtHttpClient::ResponseHandler.new
|
43
|
-
end
|
14
|
+
delegate :before_fire_callback, :after_fire_callback, to: :class
|
44
15
|
|
16
|
+
def fire(url = '', **opts, &block)
|
17
|
+
response_handler = opts.fetch(:response_handler) { dup_handler_from_class || NxtHttpClient::ResponseHandler.new }
|
45
18
|
response_handler.configure(&block) if block_given?
|
46
|
-
request = build_request(url, opts.except(:response_handler))
|
47
|
-
|
48
|
-
before_fire_callback = self.class.before_fire_callback
|
19
|
+
request = build_request(url, **opts.except(:response_handler))
|
49
20
|
before_fire_callback && instance_exec(self, request, response_handler, &before_fire_callback)
|
50
21
|
|
51
|
-
if response_handler.callbacks
|
22
|
+
if response_handler.callbacks.resolve('headers')
|
52
23
|
request.on_headers do |response|
|
53
24
|
response_handler.eval_callback(self, 'headers', response)
|
54
25
|
end
|
55
26
|
end
|
56
27
|
|
57
|
-
if response_handler.callbacks
|
28
|
+
if response_handler.callbacks.resolve('body')
|
58
29
|
request.on_body do |response|
|
59
30
|
response_handler.eval_callback(self, 'body', response)
|
60
31
|
end
|
61
32
|
end
|
62
33
|
|
63
34
|
result = nil
|
64
|
-
|
35
|
+
current_error = nil
|
65
36
|
|
66
37
|
request.on_complete do |response|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
error = e
|
38
|
+
result = callback_or_response(response, response_handler)
|
39
|
+
rescue StandardError => error
|
40
|
+
current_error = error
|
71
41
|
ensure
|
72
|
-
after_fire_callback = self.class.after_fire_callback
|
73
|
-
|
74
42
|
if after_fire_callback
|
75
|
-
result = instance_exec(self, request, response, result,
|
43
|
+
result = instance_exec(self, request, response, result, current_error, &after_fire_callback)
|
76
44
|
else
|
77
|
-
result || (raise
|
45
|
+
result || (raise current_error)
|
78
46
|
end
|
79
47
|
end
|
80
48
|
|
@@ -83,10 +51,32 @@ module NxtHttpClient
|
|
83
51
|
result
|
84
52
|
end
|
85
53
|
|
86
|
-
|
54
|
+
HTTP_METHODS.each do |method|
|
87
55
|
define_method method do |url = '', **opts, &block|
|
88
|
-
fire(url, opts.reverse_merge(method: method), &block)
|
56
|
+
fire(url, **opts.reverse_merge(method: method), &block)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def build_url(opts, url)
|
63
|
+
base_url = opts.delete(:base_url) || default_config.base_url
|
64
|
+
url = [base_url, url].reject(&:blank?).join('/')
|
65
|
+
|
66
|
+
url_without_duplicated_hashes(url)
|
67
|
+
url
|
68
|
+
end
|
69
|
+
|
70
|
+
def build_headers(opts)
|
71
|
+
opts = default_config.request_options.with_indifferent_access.deep_merge(opts.with_indifferent_access)
|
72
|
+
opts[:headers] ||= {}
|
73
|
+
|
74
|
+
if default_config.x_request_id_proc
|
75
|
+
opts[:headers]['X-Request-ID'] ||= default_config.x_request_id_proc.call
|
89
76
|
end
|
77
|
+
|
78
|
+
build_cache_header(opts)
|
79
|
+
opts
|
90
80
|
end
|
91
81
|
|
92
82
|
def dup_handler_from_class
|
@@ -96,5 +86,35 @@ module NxtHttpClient
|
|
96
86
|
def default_config
|
97
87
|
self.class.default_config
|
98
88
|
end
|
89
|
+
|
90
|
+
def build_cache_header(opts)
|
91
|
+
if opts[:cache] ||= false
|
92
|
+
strategy = opts.delete(:cache)
|
93
|
+
|
94
|
+
case strategy.to_s
|
95
|
+
when 'thread'
|
96
|
+
cache_key = Thread.current[:nxt_http_client_cache_key] ||= "#{SecureRandom.base58}::#{DateTime.current}"
|
97
|
+
opts[:headers].reverse_merge!(cache_key: cache_key)
|
98
|
+
when 'global'
|
99
|
+
opts[:headers].delete(:cache_key)
|
100
|
+
else
|
101
|
+
raise ArgumentError, "Cache strategy unknown: #{strategy}. Options are #{CACHE_STRATEGIES}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def url_without_duplicated_hashes(url)
|
107
|
+
duplicated_slashes = url.match(/([^:]\/{2,})/)
|
108
|
+
duplicated_slashes && duplicated_slashes.captures.each do |capture|
|
109
|
+
url.gsub!(capture, "#{capture[0]}/")
|
110
|
+
end
|
111
|
+
|
112
|
+
url
|
113
|
+
end
|
114
|
+
|
115
|
+
def callback_or_response(response, response_handler)
|
116
|
+
callback = response_handler.callback_for_response(response)
|
117
|
+
callback && instance_exec(response, &callback) || response
|
118
|
+
end
|
99
119
|
end
|
100
120
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
module NxtHttpClient
|
2
2
|
module ClientDsl
|
3
3
|
def configure(opts = {}, &block)
|
4
|
-
opts.each
|
5
|
-
default_config.send(k, v)
|
6
|
-
end
|
4
|
+
opts.each { |k, v| default_config.send(k, v) }
|
7
5
|
default_config.tap { |d| block.call(d) }
|
8
6
|
default_config
|
9
7
|
end
|
@@ -13,7 +11,7 @@ module NxtHttpClient
|
|
13
11
|
end
|
14
12
|
|
15
13
|
def before_fire_callback
|
16
|
-
@
|
14
|
+
@before_fire_callback ||= dup_instance_variable_from_ancestor_chain(:@before_fire_callback)
|
17
15
|
end
|
18
16
|
|
19
17
|
def after_fire(&block)
|
@@ -44,21 +42,16 @@ module NxtHttpClient
|
|
44
42
|
end
|
45
43
|
|
46
44
|
def instance_variable_from_ancestor_chain(instance_variable_name)
|
47
|
-
client = client_ancestors.find
|
48
|
-
client.instance_variable_get(instance_variable_name)
|
49
|
-
end
|
45
|
+
client = client_ancestors.find { |c| c.instance_variable_get(instance_variable_name) }
|
50
46
|
|
51
47
|
client.instance_variable_get(instance_variable_name)
|
52
48
|
end
|
53
49
|
|
54
50
|
def dup_instance_variable_from_ancestor_chain(instance_variable_name)
|
55
51
|
result = instance_variable_from_ancestor_chain(instance_variable_name).dup
|
52
|
+
return result unless block_given?
|
56
53
|
|
57
|
-
|
58
|
-
result || yield
|
59
|
-
else
|
60
|
-
result
|
61
|
-
end
|
54
|
+
result || yield
|
62
55
|
end
|
63
56
|
end
|
64
57
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module NxtHttpClient
|
2
|
-
CONFIGURABLE_OPTIONS = %i[request_options base_url x_request_id_proc]
|
2
|
+
CONFIGURABLE_OPTIONS = %i[request_options base_url x_request_id_proc].freeze
|
3
3
|
|
4
4
|
DefaultConfig = Struct.new('DefaultConfig', *CONFIGURABLE_OPTIONS) do
|
5
5
|
def initialize(request_options: ActiveSupport::HashWithIndifferentAccess.new, base_url: '', x_request_id_proc: nil)
|
@@ -1,17 +1,24 @@
|
|
1
1
|
module NxtHttpClient
|
2
2
|
class Error < StandardError
|
3
|
-
def initialize(response)
|
3
|
+
def initialize(response, message = nil)
|
4
4
|
@response = response.blank? ? Typhoeus::Response.new : response
|
5
|
+
@id = SecureRandom.uuid
|
6
|
+
@message = message || default_message
|
7
|
+
|
8
|
+
super(@message)
|
5
9
|
end
|
6
10
|
|
7
|
-
attr_reader :response
|
11
|
+
attr_reader :response, :id, :message
|
12
|
+
|
13
|
+
alias_method :to_s, :message
|
8
14
|
|
9
|
-
def
|
15
|
+
def default_message
|
10
16
|
"NxtHttpClient::Error::#{response_code}"
|
11
17
|
end
|
12
18
|
|
13
19
|
def to_h
|
14
20
|
{
|
21
|
+
id: id,
|
15
22
|
url: url,
|
16
23
|
response_code: response_code,
|
17
24
|
request_options: request_options,
|
@@ -21,7 +28,7 @@ module NxtHttpClient
|
|
21
28
|
end
|
22
29
|
|
23
30
|
def body
|
24
|
-
if response_content_type
|
31
|
+
if response_content_type&.starts_with?('application/json')
|
25
32
|
JSON.parse(response.body)
|
26
33
|
else
|
27
34
|
response.body
|
@@ -4,20 +4,14 @@ module NxtHttpClient
|
|
4
4
|
include NxtRegistry
|
5
5
|
|
6
6
|
def initialize
|
7
|
-
@callbacks = registry(
|
8
|
-
:callbacks,
|
9
|
-
call: false,
|
10
|
-
on_key_already_registered: ->(key) { raise_callback_already_registered(key) }
|
11
|
-
)
|
12
|
-
|
13
7
|
@result = nil
|
14
8
|
end
|
15
9
|
|
16
10
|
attr_accessor :result
|
17
|
-
attr_reader :callbacks
|
18
11
|
|
19
12
|
def eval_callback(target, key, response)
|
20
13
|
return unless callbacks.resolve!(key)
|
14
|
+
|
21
15
|
target.instance_exec(response, &callbacks.resolve(key))
|
22
16
|
end
|
23
17
|
|
@@ -37,23 +31,31 @@ module NxtHttpClient
|
|
37
31
|
register_callback(code, overwrite: true, &block)
|
38
32
|
end
|
39
33
|
|
40
|
-
|
41
|
-
|
34
|
+
alias on register_callback
|
35
|
+
alias on! register_callback!
|
42
36
|
|
43
37
|
def callback_for_response(response)
|
44
38
|
key_from_response = response.code.to_s
|
45
|
-
return callbacks
|
39
|
+
return callbacks.resolve('any') if callbacks.resolve('any').present?
|
46
40
|
|
47
41
|
first_matching_key = callbacks.keys.sort.reverse.find do |key|
|
48
42
|
regex_key = key.gsub('*', '[0-9]{1}')
|
49
43
|
key_from_response =~ /\A#{regex_key}\z/
|
50
44
|
end
|
51
45
|
|
52
|
-
first_matching_key && callbacks
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
46
|
+
first_matching_key && callbacks.resolve(first_matching_key) ||
|
47
|
+
response.success? && callbacks.resolve('success') ||
|
48
|
+
response.timed_out? && callbacks.resolve('timed_out') ||
|
49
|
+
!response.success? && callbacks.resolve('error') ||
|
50
|
+
callbacks.resolve('others')
|
51
|
+
end
|
52
|
+
|
53
|
+
def callbacks
|
54
|
+
@callbacks ||= NxtRegistry::Registry.new(
|
55
|
+
:callbacks,
|
56
|
+
call: false,
|
57
|
+
on_key_already_registered: ->(key) { raise_callback_already_registered(key) }
|
58
|
+
)
|
57
59
|
end
|
58
60
|
|
59
61
|
private
|
data/nxt_http_client.gemspec
CHANGED
@@ -36,15 +36,16 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.require_paths = ['lib']
|
37
37
|
|
38
38
|
spec.add_dependency 'typhoeus'
|
39
|
-
spec.add_dependency 'activesupport', '~> 6.0
|
39
|
+
spec.add_dependency 'activesupport', '~> 6.0'
|
40
40
|
spec.add_dependency 'nxt_registry'
|
41
41
|
|
42
42
|
spec.add_development_dependency 'bundler', '~> 1.17'
|
43
|
-
spec.add_development_dependency 'rake', '~>
|
43
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
44
44
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
45
45
|
spec.add_development_dependency 'pry'
|
46
46
|
spec.add_development_dependency 'vcr'
|
47
47
|
spec.add_development_dependency 'webmock'
|
48
48
|
spec.add_development_dependency 'nxt_vcr_harness'
|
49
49
|
spec.add_development_dependency 'redis'
|
50
|
+
spec.add_development_dependency 'rspec_junit_formatter'
|
50
51
|
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.
|
4
|
+
version: 0.3.4
|
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:
|
14
|
+
date: 2021-01-05 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: typhoeus
|
@@ -33,14 +33,14 @@ dependencies:
|
|
33
33
|
requirements:
|
34
34
|
- - "~>"
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 6.0
|
36
|
+
version: '6.0'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - "~>"
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 6.0
|
43
|
+
version: '6.0'
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
45
|
name: nxt_registry
|
46
46
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,14 +75,14 @@ dependencies:
|
|
75
75
|
requirements:
|
76
76
|
- - "~>"
|
77
77
|
- !ruby/object:Gem::Version
|
78
|
-
version: '
|
78
|
+
version: '13.0'
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
81
|
version_requirements: !ruby/object:Gem::Requirement
|
82
82
|
requirements:
|
83
83
|
- - "~>"
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version: '
|
85
|
+
version: '13.0'
|
86
86
|
- !ruby/object:Gem::Dependency
|
87
87
|
name: rspec
|
88
88
|
requirement: !ruby/object:Gem::Requirement
|
@@ -167,6 +167,20 @@ dependencies:
|
|
167
167
|
- - ">="
|
168
168
|
- !ruby/object:Gem::Version
|
169
169
|
version: '0'
|
170
|
+
- !ruby/object:Gem::Dependency
|
171
|
+
name: rspec_junit_formatter
|
172
|
+
requirement: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
type: :development
|
178
|
+
prerelease: false
|
179
|
+
version_requirements: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
170
184
|
description: NxtHttpClinet allows you to easily create and configure http clients.
|
171
185
|
email:
|
172
186
|
- a.robecke@getsafe.de
|
@@ -174,9 +188,11 @@ executables: []
|
|
174
188
|
extensions: []
|
175
189
|
extra_rdoc_files: []
|
176
190
|
files:
|
191
|
+
- ".circleci/config.yml"
|
177
192
|
- ".gitignore"
|
178
193
|
- ".rspec"
|
179
194
|
- ".travis.yml"
|
195
|
+
- CHANGELOG.md
|
180
196
|
- Gemfile
|
181
197
|
- Gemfile.lock
|
182
198
|
- LICENSE.txt
|
@@ -214,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
230
|
- !ruby/object:Gem::Version
|
215
231
|
version: '0'
|
216
232
|
requirements: []
|
217
|
-
rubygems_version: 3.0.
|
233
|
+
rubygems_version: 3.0.3
|
218
234
|
signing_key:
|
219
235
|
specification_version: 4
|
220
236
|
summary: NxtHttpClinet is a simple DSL on top the typhoeus http gem
|