lhc 7.3.3 → 8.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/docs/interceptors.md +11 -6
- data/lib/lhc.rb +2 -3
- data/lib/lhc/interceptor.rb +15 -5
- data/lib/lhc/interceptors.rb +24 -0
- data/lib/lhc/interceptors/auth.rb +1 -1
- data/lib/lhc/interceptors/caching.rb +2 -2
- data/lib/lhc/interceptors/default_timeout.rb +1 -1
- data/lib/lhc/interceptors/monitoring.rb +3 -3
- data/lib/lhc/interceptors/prometheus.rb +3 -2
- data/lib/lhc/interceptors/retry.rb +1 -1
- data/lib/lhc/interceptors/rollbar.rb +1 -1
- data/lib/lhc/request.rb +7 -7
- data/lib/lhc/version.rb +1 -1
- data/spec/basic_methods/request_without_rails_spec.rb +2 -21
- data/spec/interceptors/after_request_spec.rb +1 -1
- data/spec/interceptors/after_response_spec.rb +1 -1
- data/spec/interceptors/before_request_spec.rb +1 -1
- data/spec/interceptors/before_response_spec.rb +1 -1
- data/spec/interceptors/caching/to_cache_spec.rb +1 -1
- data/spec/interceptors/response_competition_spec.rb +2 -2
- data/spec/interceptors/return_response_spec.rb +2 -2
- metadata +4 -4
- data/lib/lhc/interceptor_processor.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e896a6247a34efa8fb35b9ba8cdf51ad68b8a00
|
4
|
+
data.tar.gz: f5d30a0736bbb74ea0cb4b62e060c4edeb9cc48e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06f42cbc9182070e2d89c325a261095ea66665aa27ae3b61f6e3206158b1f960f9939997001b9f8db087c5ff439c89b059548d54eaceb44ae7f106d73ae931a3
|
7
|
+
data.tar.gz: 663cfcbe61d6e9b220dc6073c1c1c9e4c576981984c304f60945fd30d01ca7c2c14e6aa040270439e15134b2178d8deba39d4c9141d65d214979e9382a47d99b
|
data/README.md
CHANGED
data/docs/interceptors.md
CHANGED
@@ -6,7 +6,7 @@ Interceptors
|
|
6
6
|
```ruby
|
7
7
|
class TrackingIdInterceptor < LHC::Interceptor
|
8
8
|
|
9
|
-
def before_request
|
9
|
+
def before_request
|
10
10
|
request.params[:tid] = 123
|
11
11
|
end
|
12
12
|
end
|
@@ -27,15 +27,20 @@ like [Caching](/docs/interceptors/caching.md), [Monitoring](/docs/interceptors/m
|
|
27
27
|
|
28
28
|
## Callbacks
|
29
29
|
|
30
|
-
`before_raw_request
|
30
|
+
`before_raw_request` is called before the raw typhoeus request is prepared/created.
|
31
|
+
|
32
|
+
`before_request` is called when the request is prepared and about to be executed.
|
33
|
+
|
34
|
+
`after_request` is called after request was started.
|
35
|
+
|
36
|
+
`before_response` is called when response started to arrive.
|
31
37
|
|
32
|
-
`
|
38
|
+
`after_response` is called after the response arrived completely.
|
33
39
|
|
34
|
-
`after_request(request)` is called after request was started.
|
35
40
|
|
36
|
-
|
41
|
+
## Attributes: Request/Response
|
37
42
|
|
38
|
-
|
43
|
+
Every interceptor can directly access their instance `request` or `response`.
|
39
44
|
|
40
45
|
→ [Read more about the request object](request.md)
|
41
46
|
|
data/lib/lhc.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'typhoeus'
|
2
2
|
require 'active_support/core_ext/object/blank'
|
3
|
-
require 'active_support/core_ext/hash/keys'
|
4
3
|
|
5
4
|
module LHC
|
6
5
|
autoload :BasicMethodsConcern,
|
@@ -106,8 +105,8 @@ module LHC
|
|
106
105
|
|
107
106
|
autoload :Interceptor,
|
108
107
|
'lhc/interceptor'
|
109
|
-
autoload :
|
110
|
-
'lhc/
|
108
|
+
autoload :Interceptors,
|
109
|
+
'lhc/interceptors'
|
111
110
|
autoload :Formats,
|
112
111
|
'lhc/formats'
|
113
112
|
autoload :Monitoring,
|
data/lib/lhc/interceptor.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
1
|
class LHC::Interceptor
|
2
2
|
|
3
|
-
|
3
|
+
attr_reader :request
|
4
4
|
|
5
|
-
def
|
5
|
+
def initialize(request)
|
6
|
+
@request = request
|
7
|
+
end
|
8
|
+
|
9
|
+
def response
|
10
|
+
@request.response
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_raw_request; end
|
14
|
+
|
15
|
+
def before_request; end
|
6
16
|
|
7
|
-
def after_request
|
17
|
+
def after_request; end
|
8
18
|
|
9
|
-
def before_response
|
19
|
+
def before_response; end
|
10
20
|
|
11
|
-
def after_response
|
21
|
+
def after_response; end
|
12
22
|
|
13
23
|
# Prevent Interceptors from beeing duplicated!
|
14
24
|
# Their classes have flag-character.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Handles interceptions during the lifecycle of a request
|
2
|
+
# Represents all active interceptors for a request/response.
|
3
|
+
class LHC::Interceptors
|
4
|
+
|
5
|
+
attr_accessor :all
|
6
|
+
|
7
|
+
# Intitalizes and determines if global or local interceptors are used
|
8
|
+
def initialize(request)
|
9
|
+
self.all = (request.options[:interceptors] || LHC.config.interceptors).map do |interceptor|
|
10
|
+
interceptor.new(request)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Forwards messages to interceptors and handles provided responses.
|
15
|
+
def intercept(name)
|
16
|
+
all.each do |interceptor|
|
17
|
+
result = interceptor.send(name)
|
18
|
+
if result.is_a? LHC::Response
|
19
|
+
fail 'Response already set from another interceptor' if @response
|
20
|
+
@response = interceptor.request.response = result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -8,7 +8,7 @@ class LHC::Caching < LHC::Interceptor
|
|
8
8
|
# Options forwarded to the cache
|
9
9
|
FORWARDED_OPTIONS = [:expires_in, :race_condition_ttl]
|
10
10
|
|
11
|
-
def before_request
|
11
|
+
def before_request
|
12
12
|
return unless cache?(request)
|
13
13
|
deprecation_warning(request.options)
|
14
14
|
options = options(request.options)
|
@@ -19,7 +19,7 @@ class LHC::Caching < LHC::Interceptor
|
|
19
19
|
from_cache(request, response_data)
|
20
20
|
end
|
21
21
|
|
22
|
-
def after_response
|
22
|
+
def after_response
|
23
23
|
return unless response.success?
|
24
24
|
request = response.request
|
25
25
|
return unless cache?(request)
|
@@ -6,7 +6,7 @@ class LHC::DefaultTimeout < LHC::Interceptor
|
|
6
6
|
CONNECTTIMEOUT = 2 # seconds
|
7
7
|
TIMEOUT = 15 # seconds
|
8
8
|
|
9
|
-
def before_raw_request
|
9
|
+
def before_raw_request
|
10
10
|
request_options = (request.options || {})
|
11
11
|
request_options[:timeout] ||= timeout || TIMEOUT
|
12
12
|
request_options[:connecttimeout] ||= connecttimeout || CONNECTTIMEOUT
|
@@ -9,18 +9,18 @@ class LHC::Monitoring < LHC::Interceptor
|
|
9
9
|
|
10
10
|
config_accessor :statsd
|
11
11
|
|
12
|
-
def before_request
|
12
|
+
def before_request
|
13
13
|
return unless statsd
|
14
14
|
LHC::Monitoring.statsd.count("#{key(request)}.before_request", 1)
|
15
15
|
end
|
16
16
|
|
17
|
-
def after_request
|
17
|
+
def after_request
|
18
18
|
return unless statsd
|
19
19
|
LHC::Monitoring.statsd.count("#{key(request)}.count", 1)
|
20
20
|
LHC::Monitoring.statsd.count("#{key(request)}.after_request", 1)
|
21
21
|
end
|
22
22
|
|
23
|
-
def after_response
|
23
|
+
def after_response
|
24
24
|
return unless statsd
|
25
25
|
key = key(response)
|
26
26
|
LHC::Monitoring.statsd.timing("#{key}.time", response.time) if response.success?
|
@@ -15,14 +15,15 @@ class LHC::Prometheus < LHC::Interceptor
|
|
15
15
|
[LHC::Prometheus.namespace, 'lhc_times'].join('_').to_sym
|
16
16
|
end
|
17
17
|
|
18
|
-
def initialize
|
18
|
+
def initialize(request)
|
19
|
+
super(request)
|
19
20
|
return if LHC::Prometheus.registered || LHC::Prometheus.client.blank?
|
20
21
|
LHC::Prometheus.client.registry.counter(LHC::Prometheus.request_key, 'Counter of all LHC requests.')
|
21
22
|
LHC::Prometheus.client.registry.histogram(LHC::Prometheus.times_key, 'Times for all LHC requests.')
|
22
23
|
LHC::Prometheus.registered = true
|
23
24
|
end
|
24
25
|
|
25
|
-
def after_response
|
26
|
+
def after_response
|
26
27
|
return if !LHC::Prometheus.registered || LHC::Prometheus.client.blank?
|
27
28
|
LHC::Prometheus.client.registry
|
28
29
|
.get(LHC::Prometheus.request_key)
|
data/lib/lhc/request.rb
CHANGED
@@ -16,11 +16,11 @@ class LHC::Request
|
|
16
16
|
self.error_handler = options.delete :error_handler
|
17
17
|
use_configured_endpoint!
|
18
18
|
generate_url_from_template!
|
19
|
-
self.
|
20
|
-
|
19
|
+
self.interceptors = LHC::Interceptors.new(self)
|
20
|
+
interceptors.intercept(:before_raw_request)
|
21
21
|
self.raw = create_request
|
22
22
|
self.format = options.delete('format') || LHC::Formats::JSON.new
|
23
|
-
|
23
|
+
interceptors.intercept(:before_request)
|
24
24
|
run! if self_executing && !response
|
25
25
|
end
|
26
26
|
|
@@ -50,7 +50,7 @@ class LHC::Request
|
|
50
50
|
|
51
51
|
private
|
52
52
|
|
53
|
-
attr_accessor :
|
53
|
+
attr_accessor :interceptors
|
54
54
|
|
55
55
|
def optionally_encoded_url(options)
|
56
56
|
return options[:url] unless options.fetch(:url_encoding, true)
|
@@ -60,8 +60,8 @@ class LHC::Request
|
|
60
60
|
def create_request
|
61
61
|
request = Typhoeus::Request.new(optionally_encoded_url(options), typhoeusize(options))
|
62
62
|
request.on_headers do
|
63
|
-
|
64
|
-
|
63
|
+
interceptors.intercept(:after_request)
|
64
|
+
interceptors.intercept(:before_response)
|
65
65
|
end
|
66
66
|
request.on_complete { |response| on_complete(response) }
|
67
67
|
request
|
@@ -111,7 +111,7 @@ class LHC::Request
|
|
111
111
|
|
112
112
|
def on_complete(response)
|
113
113
|
self.response = LHC::Response.new(response, self)
|
114
|
-
|
114
|
+
interceptors.intercept(:after_response)
|
115
115
|
handle_error(self.response) unless self.response.success?
|
116
116
|
end
|
117
117
|
|
data/lib/lhc/version.rb
CHANGED
@@ -1,28 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe LHC do
|
4
|
-
context '
|
5
|
-
before do
|
6
|
-
stub_request(:get, "http://datastore/v2/feedbacks").to_return(status: 200, body: "{}")
|
7
|
-
end
|
4
|
+
context 'request' do
|
8
5
|
it "is able to call .request without LHC raising NoMethodError: undefined method `blank?' for nil:NilClass when calling it outside of the rails context" do
|
9
|
-
expect { LHC.request(url: "http://datastore/v2/feedbacks", method: :get) }.not_to raise_error
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context 'POST' do
|
14
|
-
before do
|
15
|
-
stub_request(:post, "http://datastore/v2/feedbacks").to_return(status: 200, body: "{}")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "is able to call .request without LHC raising NoMethodError: undefined method `deep_symbolize_keys' for {}:Hash" do
|
19
|
-
options = {
|
20
|
-
url: "http://datastore/v2/feedbacks",
|
21
|
-
method: :post,
|
22
|
-
body: "{}",
|
23
|
-
headers: { 'Content-Type' => 'application/json' }
|
24
|
-
}
|
25
|
-
expect { LHC.request(options) }.not_to raise_error
|
6
|
+
expect { LHC.request(url: "http://datastore/v2/feedbacks", method: :get) }.not_to raise_error(NoMethodError)
|
26
7
|
end
|
27
8
|
end
|
28
9
|
end
|
@@ -6,7 +6,7 @@ describe LHC::Caching do
|
|
6
6
|
expect do
|
7
7
|
response = Typhoeus::Response.new(headers: { 'Accept' => 'application/json' })
|
8
8
|
Marshal.dump(
|
9
|
-
LHC::Caching.new.send(:to_cache, response)
|
9
|
+
LHC::Caching.new(response).send(:to_cache, response)
|
10
10
|
)
|
11
11
|
end.not_to raise_error
|
12
12
|
end
|
@@ -8,7 +8,7 @@ describe LHC do
|
|
8
8
|
@@cached = false
|
9
9
|
cattr_accessor :cached
|
10
10
|
|
11
|
-
def before_request
|
11
|
+
def before_request
|
12
12
|
if @@cached
|
13
13
|
return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from local cache'), nil)
|
14
14
|
end
|
@@ -18,7 +18,7 @@ describe LHC do
|
|
18
18
|
|
19
19
|
class RemoteCacheInterceptor < LHC::Interceptor
|
20
20
|
|
21
|
-
def before_request
|
21
|
+
def before_request
|
22
22
|
if request.response.nil?
|
23
23
|
return LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from remote cache'), nil)
|
24
24
|
end
|
@@ -5,7 +5,7 @@ describe LHC do
|
|
5
5
|
before(:each) do
|
6
6
|
class CacheInterceptor < LHC::Interceptor
|
7
7
|
|
8
|
-
def before_request
|
8
|
+
def before_request
|
9
9
|
LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from cache'), nil)
|
10
10
|
end
|
11
11
|
end
|
@@ -20,7 +20,7 @@ describe LHC do
|
|
20
20
|
context 'misusage' do
|
21
21
|
before(:each) do
|
22
22
|
class AnotherInterceptor < LHC::Interceptor
|
23
|
-
def before_request
|
23
|
+
def before_request
|
24
24
|
LHC::Response.new(Typhoeus::Response.new({}), nil)
|
25
25
|
end
|
26
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 8.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhc/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|
@@ -217,7 +217,7 @@ files:
|
|
217
217
|
- lib/lhc/formats.rb
|
218
218
|
- lib/lhc/formats/json.rb
|
219
219
|
- lib/lhc/interceptor.rb
|
220
|
-
- lib/lhc/
|
220
|
+
- lib/lhc/interceptors.rb
|
221
221
|
- lib/lhc/interceptors/auth.rb
|
222
222
|
- lib/lhc/interceptors/caching.rb
|
223
223
|
- lib/lhc/interceptors/default_timeout.rb
|
@@ -364,7 +364,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
364
364
|
requirements:
|
365
365
|
- Ruby >= 2.0.0
|
366
366
|
rubyforge_project:
|
367
|
-
rubygems_version: 2.6.
|
367
|
+
rubygems_version: 2.6.12
|
368
368
|
signing_key:
|
369
369
|
specification_version: 4
|
370
370
|
summary: LocalHttpClient
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# Handles interceptions during the lifecycle of a request
|
2
|
-
class LHC::InterceptorProcessor
|
3
|
-
|
4
|
-
attr_accessor :interceptors
|
5
|
-
|
6
|
-
# Intitalizes the processor and determines if global or local interceptors are used
|
7
|
-
def initialize(target)
|
8
|
-
options = target.options if target.is_a? LHC::Request
|
9
|
-
options ||= target.request.options if target.is_a? LHC::Response
|
10
|
-
self.interceptors = (options[:interceptors] || LHC.config.interceptors).map { |i| i.new }
|
11
|
-
end
|
12
|
-
|
13
|
-
# Forwards messages to interceptors and handles provided responses.
|
14
|
-
def intercept(name, target)
|
15
|
-
interceptors.each do |interceptor|
|
16
|
-
result = interceptor.send(name, target)
|
17
|
-
if result.is_a? LHC::Response
|
18
|
-
fail 'Response already set from another interceptor' if @response
|
19
|
-
request = target.is_a?(LHC::Request) ? target : target.request
|
20
|
-
@response = request.response = result
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|