lhc 11.0.0 → 11.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: b98d689381652202d5204b16f76d5171c7650362ed047d733447f04de02dbac5
4
- data.tar.gz: cb43cb26d090cb3e387e9e1b17ece3c96a7aab2b07fb54592f1a594af0dd3d6a
3
+ metadata.gz: 19e2069105d325acde5b2d89abcb267604658239dfc8808acc58637aac94a97a
4
+ data.tar.gz: f1795b3999b8307cca582bfd55318a663a823b87057535e9652b1c611d952292
5
5
  SHA512:
6
- metadata.gz: 02d1689b6669a88dee07d03916d8d3972a5733abb577af695509bbe8c867c6357daefc94bb9008205378a0ccfc65f6de7006fcf66f3425e651ebc92b76e1d1bf
7
- data.tar.gz: 4149bfbcb67cd51d4c3743353357d663548a20812035b1d9e051ed9990ca4de73936dff7075db59193113009f8866ccce94a09816bd29942345e2e146cf6f2c9
6
+ metadata.gz: 2b0da63bf8c0ff0a66c37e7c3ca3b28f14e4940c2c16d432734b56ff957c4b305ca408660b5448c5f81e87cf19faf76578ddf52b0fbe826a2f63d1ad8c59468d
7
+ data.tar.gz: bd50aacfc4667b1d30cf9e1c1b2961d31b46e5603868a6d14ebcc2690deb4e9e3691e5ef65881f5b1744994d342eaf11be47b48cbd60bb33c1a9065033b32e5c
@@ -27,7 +27,7 @@ module LHC
27
27
  private
28
28
 
29
29
  def parallel_requests(options)
30
- hydra = Typhoeus::Hydra.hydra
30
+ hydra = Typhoeus::Hydra.new # do not use memoization !
31
31
  requests = []
32
32
  options.each do |option|
33
33
  request = LHC::Request.new(option, false)
@@ -5,6 +5,10 @@ module LHC
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  module ClassMethods
8
+ def form
9
+ LHC::Formats::Form
10
+ end
11
+
8
12
  def json
9
13
  LHC::Formats::JSON
10
14
  end
@@ -59,6 +59,10 @@ class LHC::Error < StandardError
59
59
  self.response = response
60
60
  end
61
61
 
62
+ def self.to_a
63
+ [self]
64
+ end
65
+
62
66
  def to_s
63
67
  return response if response.is_a?(String)
64
68
  request = response.request
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHC::Formats
4
+ autoload :Form, 'lhc/formats/form'
4
5
  autoload :JSON, 'lhc/formats/json'
5
6
  autoload :Multipart, 'lhc/formats/multipart'
6
7
  autoload :Plain, 'lhc/formats/plain'
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LHC::Formats
4
+ class Form < LHC::Format
5
+ include LHC::BasicMethodsConcern
6
+
7
+ def self.request(options)
8
+ options[:format] = new
9
+ super(options)
10
+ end
11
+
12
+ def format_options(options)
13
+ options[:headers] ||= {}
14
+ no_content_type_header!(options)
15
+ options[:headers]['Content-Type'] = 'application/x-www-form-urlencoded'
16
+ options
17
+ end
18
+
19
+ def as_json(input)
20
+ parse(input)
21
+ end
22
+
23
+ def as_open_struct(input)
24
+ parse(input)
25
+ end
26
+
27
+ def to_body(input)
28
+ input
29
+ end
30
+
31
+ def to_s
32
+ 'form'
33
+ end
34
+
35
+ def to_sym
36
+ to_s.to_sym
37
+ end
38
+
39
+ private
40
+
41
+ def parse(input)
42
+ input
43
+ end
44
+ end
45
+ end
@@ -66,7 +66,7 @@ class LHC::Auth < LHC::Interceptor
66
66
  end
67
67
 
68
68
  def bearer_header_present?
69
- @has_bearer_header ||= request.headers['Authorization'] =~ /^Bearer [0-9a-f-]+$/i
69
+ @has_bearer_header ||= request.headers['Authorization'] =~ /^Bearer .+$/i
70
70
  end
71
71
 
72
72
  def refresh_client_token_option
@@ -15,9 +15,15 @@ class LHC::Prometheus < LHC::Interceptor
15
15
  def initialize(request)
16
16
  super(request)
17
17
  return if LHC::Prometheus.registered || LHC::Prometheus.client.blank?
18
- LHC::Prometheus.client.registry.counter(LHC::Prometheus::REQUEST_COUNTER_KEY, 'Counter of all LHC requests.')
19
- LHC::Prometheus.client.registry.histogram(LHC::Prometheus::REQUEST_HISTOGRAM_KEY, 'Request timings for all LHC requests in seconds.')
20
- LHC::Prometheus.registered = true
18
+
19
+ begin
20
+ LHC::Prometheus.client.registry.counter(LHC::Prometheus::REQUEST_COUNTER_KEY, 'Counter of all LHC requests.')
21
+ LHC::Prometheus.client.registry.histogram(LHC::Prometheus::REQUEST_HISTOGRAM_KEY, 'Request timings for all LHC requests in seconds.')
22
+ rescue Prometheus::Client::Registry::AlreadyRegisteredError => e
23
+ Rails.logger.error(e) if defined?(Rails)
24
+ ensure
25
+ LHC::Prometheus.registered = true
26
+ end
21
27
  end
22
28
 
23
29
  def after_response
@@ -15,7 +15,7 @@ class LHC::Request
15
15
  attr_accessor :response, :options, :raw, :format, :error_handler, :errors_ignored, :source
16
16
 
17
17
  def initialize(options, self_executing = true)
18
- self.errors_ignored = (options.fetch(:ignored_errors, []) || []).compact
18
+ self.errors_ignored = (options.fetch(:ignored_errors, []) || []).to_a.compact
19
19
  self.source = options&.dig(:source)
20
20
  self.options = format!(options.deep_dup || {})
21
21
  self.error_handler = options.delete :error_handler
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHC
4
- VERSION ||= '11.0.0'
4
+ VERSION ||= '11.2.0'
5
5
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC do
6
+ include ActionDispatch::TestProcess
7
+
8
+ context 'form' do
9
+ it 'formats requests to be application/x-www-form-urlencoded' do
10
+ stub = stub_request(:post, 'http://local.ch/')
11
+ .with(body: 'client_id=1234&client_secret=4567&grant_type=client_credentials')
12
+ .with(headers: { 'Content-Type': 'application/x-www-form-urlencoded' })
13
+ .to_return(status: 200)
14
+
15
+ LHC.form.post(
16
+ 'http://local.ch',
17
+ body: {
18
+ client_id: '1234',
19
+ client_secret: '4567',
20
+ grant_type: 'client_credentials'
21
+ }
22
+ )
23
+
24
+ expect(stub).to have_been_requested
25
+ end
26
+ end
27
+ end
@@ -10,7 +10,7 @@ describe LHC do
10
10
  let(:body) { { size: 2231 }.to_json }
11
11
  let(:location) { 'http://local.ch/uploads/image.jpg' }
12
12
 
13
- it 'leaves plains requests unformatted' do
13
+ it 'formats requests to be multipart/form-data' do
14
14
  stub_request(:post, 'http://local.ch/') do |request|
15
15
  raise 'Content-Type header wrong' unless request.headers['Content-Type'] == 'multipart/form-data'
16
16
  raise 'Body wrongly formatted' unless request.body.match(/file=%23%3CActionDispatch%3A%3AHttp%3A%3AUploadedFile%3A.*%3E&type=Image/)
@@ -31,4 +31,14 @@ describe LHC::Auth do
31
31
  LHC.config.endpoint(:local, 'http://local.ch', auth: options.merge(reauthenticated: true))
32
32
  expect { LHC.get(:local) }.to raise_error(LHC::Unauthorized)
33
33
  end
34
+
35
+ context 'token format' do
36
+ let(:initial_token) { 'BAsZ-98-ZZZ' }
37
+
38
+ it 'refreshes tokens with various formats' do
39
+ LHC.config.endpoint(:local, 'http://local.ch', auth: options)
40
+ LHC.get(:local)
41
+ expect(auth_suceeding_after_recovery).to have_been_made.once
42
+ end
43
+ end
34
44
  end
@@ -62,4 +62,12 @@ describe LHC::Request do
62
62
  }.to raise_error(LHC::NotFound)
63
63
  end
64
64
  end
65
+
66
+ context 'passing keys instead of arrays' do
67
+ before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
68
+
69
+ it "does not raise an error when ignored errors is a key instead of an array" do
70
+ LHC.get('http://local.ch', ignored_errors: LHC::NotFound)
71
+ end
72
+ end
65
73
  end
@@ -37,4 +37,23 @@ describe LHC::Request do
37
37
  expect(@called).to eq 2
38
38
  end
39
39
  end
40
+
41
+ context 'webmock disabled' do
42
+ before do
43
+ WebMock.disable!
44
+ end
45
+
46
+ after do
47
+ WebMock.enable!
48
+ end
49
+
50
+ it 'does not memorize parallelization handlers in typhoeus (hydra) in case one request of the parallization fails' do
51
+ begin
52
+ LHC.request([{ url: 'https://www.google.com/' }, { url: 'https://nonexisting123' }, { url: 'https://www.google.com/' }, { url: 'https://nonexisting123' }])
53
+ rescue LHC::UnknownError
54
+ end
55
+
56
+ LHC.request([{ url: 'https://www.google.com' }])
57
+ end
58
+ end
40
59
  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: 11.0.0
4
+ version: 11.2.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: 2019-12-03 00:00:00.000000000 Z
11
+ date: 2020-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -230,6 +230,7 @@ files:
230
230
  - lib/lhc/errors/unknown_error.rb
231
231
  - lib/lhc/format.rb
232
232
  - lib/lhc/formats.rb
233
+ - lib/lhc/formats/form.rb
233
234
  - lib/lhc/formats/json.rb
234
235
  - lib/lhc/formats/multipart.rb
235
236
  - lib/lhc/formats/plain.rb
@@ -316,6 +317,7 @@ files:
316
317
  - spec/error/response_spec.rb
317
318
  - spec/error/timeout_spec.rb
318
319
  - spec/error/to_s_spec.rb
320
+ - spec/formats/form_spec.rb
319
321
  - spec/formats/json_spec.rb
320
322
  - spec/formats/multipart_spec.rb
321
323
  - spec/formats/plain_spec.rb
@@ -465,6 +467,7 @@ test_files:
465
467
  - spec/error/response_spec.rb
466
468
  - spec/error/timeout_spec.rb
467
469
  - spec/error/to_s_spec.rb
470
+ - spec/formats/form_spec.rb
468
471
  - spec/formats/json_spec.rb
469
472
  - spec/formats/multipart_spec.rb
470
473
  - spec/formats/plain_spec.rb