lhc 11.2.0 → 12.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 +73 -73
- data/lib/lhc/request.rb +2 -2
- data/lib/lhc/version.rb +1 -1
- data/spec/interceptors/retry/main_spec.rb +1 -1
- data/spec/request/error_handling_spec.rb +3 -3
- data/spec/request/ignore_errors_spec.rb +6 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cde56134f9f363649066cc6a6764534c56887fbd9ea2234beaaac0babe6a56c
|
4
|
+
data.tar.gz: 4daf357293a3539de5764670def7e682d6d6dfb98cc209980fcd968a187bad31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc0deecc6746fb5c192fdc4648af66992cf21fe1fc340665cb7ec03e98a6f9aaabfbb0f5d59769fa8312c8448cf64c4236f8bcb807915b8055270af709163b75
|
7
|
+
data.tar.gz: fbbda42ca1d2d7976b41c982a2b5135865af46340f4e5f6539f21c79115144bd9c8ff0039b5ee910e581fbe6d075d6776f564476cccded2e41fd330ab48e952f
|
data/README.md
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
LHC
|
2
|
-
===
|
3
|
-
|
4
1
|
LHC is an extended/advanced HTTP client. Implementing basic http-communication enhancements like interceptors, exception handling, format handling, accessing response data, configuring endpoints and placeholders and fully compatible, RFC-compliant URL-template support.
|
5
2
|
|
6
3
|
LHC uses [typhoeus](https://github.com/typhoeus/typhoeus) for low level http communication.
|
@@ -30,69 +27,70 @@ use it like:
|
|
30
27
|
```
|
31
28
|
|
32
29
|
## Table of contents
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
30
|
+
* [Quick start guide](#quick-start-guide)
|
31
|
+
* [Basic methods](#basic-methods)
|
32
|
+
* [Request](#request)
|
33
|
+
* [Formats](#formats)
|
34
|
+
* [Default format](#default-format)
|
35
|
+
* [Unformatted requests](#unformatted-requests)
|
36
|
+
* [Upload with LHC](#upload-with-lhc)
|
37
|
+
* [Parallel requests](#parallel-requests)
|
38
|
+
* [Follow redirects](#follow-redirects)
|
39
|
+
* [Transfer data through the request body](#transfer-data-through-the-request-body)
|
40
|
+
* [Request parameters](#request-parameters)
|
41
|
+
* [Array Parameter Encoding](#array-parameter-encoding)
|
42
|
+
* [Request URL encoding](#request-url-encoding)
|
43
|
+
* [Request URL-Templates](#request-url-templates)
|
44
|
+
* [Request timeout](#request-timeout)
|
45
|
+
* [Request Agent](#request-agent)
|
46
|
+
* [Response](#response)
|
47
|
+
* [Accessing response data](#accessing-response-data)
|
48
|
+
* [Exceptions](#exceptions)
|
49
|
+
* [Custom error handling (rescue)](#custom-error-handling-rescue)
|
50
|
+
* [Ignore certain errors](#ignore-certain-errors)
|
51
|
+
* [Configuration](#configuration)
|
52
|
+
* [Configuring endpoints](#configuring-endpoints)
|
53
|
+
* [Configuring placeholders](#configuring-placeholders)
|
54
|
+
* [Interceptors](#interceptors)
|
55
|
+
* [Quick start: Configure/Enable Interceptors](#quick-start-configureenable-interceptors)
|
56
|
+
* [Interceptors on local request level](#interceptors-on-local-request-level)
|
57
|
+
* [Core Interceptors](#core-interceptors)
|
58
|
+
* [Authentication Interceptor](#authentication-interceptor)
|
59
|
+
* [Bearer Authentication](#bearer-authentication)
|
60
|
+
* [Basic Authentication](#basic-authentication)
|
61
|
+
* [Reauthenticate](#reauthenticate)
|
62
|
+
* [Bearer Authentication with client access token](#bearer-authentication-with-client-access-token)
|
63
|
+
* [Caching Interceptor](#caching-interceptor)
|
64
|
+
* [Options](#options)
|
65
|
+
* [Default Timeout Interceptor](#default-timeout-interceptor)
|
66
|
+
* [Overwrite defaults](#overwrite-defaults)
|
67
|
+
* [Logging Interceptor](#logging-interceptor)
|
68
|
+
* [Installation](#installation)
|
69
|
+
* [What and how it logs](#what-and-how-it-logs)
|
70
|
+
* [Configure](#configure)
|
71
|
+
* [Monitoring Interceptor](#monitoring-interceptor)
|
72
|
+
* [Installation](#installation-1)
|
73
|
+
* [Environment](#environment)
|
74
|
+
* [What it tracks](#what-it-tracks)
|
75
|
+
* [Configure](#configure-1)
|
76
|
+
* [Prometheus Interceptor](#prometheus-interceptor)
|
77
|
+
* [Retry Interceptor](#retry-interceptor)
|
78
|
+
* [Limit the amount of retries while making the request](#limit-the-amount-of-retries-while-making-the-request)
|
79
|
+
* [Change the default maximum of retries of the retry interceptor](#change-the-default-maximum-of-retries-of-the-retry-interceptor)
|
80
|
+
* [Retry all requests](#retry-all-requests)
|
81
|
+
* [Do not retry certain response codes](#do-not-retry-certain-response-codes)
|
82
|
+
* [Rollbar Interceptor](#rollbar-interceptor)
|
83
|
+
* [Forward additional parameters](#forward-additional-parameters)
|
84
|
+
* [Throttle](#throttle)
|
85
|
+
* [Zipkin](#zipkin)
|
86
|
+
* [Create an interceptor from scratch](#create-an-interceptor-from-scratch)
|
87
|
+
* [Interceptor callbacks](#interceptor-callbacks)
|
88
|
+
* [Interceptor request/response](#interceptor-requestresponse)
|
89
|
+
* [Provide a response replacement through an interceptor](#provide-a-response-replacement-through-an-interceptor)
|
90
|
+
* [Testing](#testing)
|
91
|
+
* [License](#license)
|
92
|
+
|
93
|
+
|
96
94
|
|
97
95
|
## Basic methods
|
98
96
|
|
@@ -403,7 +401,7 @@ timeout? => LHC::Timeout
|
|
403
401
|
anything_else => LHC::UnknownError
|
404
402
|
```
|
405
403
|
|
406
|
-
### Custom error handling
|
404
|
+
### Custom error handling (rescue)
|
407
405
|
|
408
406
|
You can provide custom error handlers to handle errors happening during the request.
|
409
407
|
|
@@ -413,19 +411,21 @@ If your error handler returns anything else but `nil` it replaces the response b
|
|
413
411
|
|
414
412
|
```ruby
|
415
413
|
handler = ->(response){ do_something_with_response; return {name: 'unknown'} }
|
416
|
-
response = LHC.get('http://something',
|
414
|
+
response = LHC.get('http://something', rescue: handler)
|
417
415
|
response.data.name # 'unknown'
|
418
416
|
```
|
419
417
|
|
420
418
|
### Ignore certain errors
|
421
419
|
|
422
420
|
As it's discouraged to rescue errors and then don't handle them (ruby styleguide)[https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions],
|
423
|
-
but you often want to continue working with `nil`, LHC provides the `
|
421
|
+
but you often want to continue working with `nil`, LHC provides the `ignore` option.
|
424
422
|
|
425
423
|
Errors listed in this option will not be raised and will leave the `response.body` and `response.data` to stay `nil`.
|
426
424
|
|
425
|
+
You can either pass the LHC error class you want to be ignored or an array of LHC error classes.
|
426
|
+
|
427
427
|
```ruby
|
428
|
-
response = LHC.get('http://something',
|
428
|
+
response = LHC.get('http://something', ignore: LHC::NotFound)
|
429
429
|
|
430
430
|
response.body # nil
|
431
431
|
response.data # nil
|
@@ -817,16 +817,16 @@ If you want to retry all requests made from your application, you just need to c
|
|
817
817
|
|
818
818
|
##### Do not retry certain response codes
|
819
819
|
|
820
|
-
If you do not want to retry based on certain response codes, use retry in combination with explicit `
|
820
|
+
If you do not want to retry based on certain response codes, use retry in combination with explicit `ignore`:
|
821
821
|
|
822
822
|
```ruby
|
823
|
-
LHC.get('http://local.ch',
|
823
|
+
LHC.get('http://local.ch', ignore: LHC::NotFound, retry: { max: 1 })
|
824
824
|
```
|
825
825
|
|
826
826
|
Or if you use `LHC::Retry.all`:
|
827
827
|
|
828
828
|
```ruby
|
829
|
-
LHC.get('http://local.ch',
|
829
|
+
LHC.get('http://local.ch', ignore: LHC::NotFound)
|
830
830
|
```
|
831
831
|
|
832
832
|
#### Rollbar Interceptor
|
data/lib/lhc/request.rb
CHANGED
@@ -15,10 +15,10 @@ 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(:
|
18
|
+
self.errors_ignored = (options.fetch(:ignore, []) || []).to_a.compact
|
19
19
|
self.source = options&.dig(:source)
|
20
20
|
self.options = format!(options.deep_dup || {})
|
21
|
-
self.error_handler = options.delete :
|
21
|
+
self.error_handler = options.delete :rescue
|
22
22
|
use_configured_endpoint!
|
23
23
|
generate_url_from_template!
|
24
24
|
self.interceptors = LHC::Interceptors.new(self)
|
data/lib/lhc/version.rb
CHANGED
@@ -66,7 +66,7 @@ describe LHC::Rollbar do
|
|
66
66
|
|
67
67
|
it 'does not retry if the error is explicitly ignored' do
|
68
68
|
request_stub
|
69
|
-
LHC.get('http://local.ch', retry: { max: 1 },
|
69
|
+
LHC.get('http://local.ch', retry: { max: 1 }, ignore: [LHC::NotFound])
|
70
70
|
expect(request_stub).to have_been_requested.times(1)
|
71
71
|
end
|
72
72
|
end
|
@@ -67,21 +67,21 @@ describe LHC::Request do
|
|
67
67
|
it 'handles errors with the provided handler and does not raise them' do
|
68
68
|
stub_request(:get, "http://something").to_return(status: 400)
|
69
69
|
handler = double('handler', call: -> {})
|
70
|
-
LHC::Request.new(url: "http://something",
|
70
|
+
LHC::Request.new(url: "http://something", rescue: handler)
|
71
71
|
expect(handler).to have_received(:call)
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'exchanges body with handlers return if the handler returns something' do
|
75
75
|
stub_request(:get, "http://something").to_return(status: 400)
|
76
76
|
handler = ->(_response) { { name: 'unknown' }.to_json }
|
77
|
-
request = LHC::Request.new(url: "http://something",
|
77
|
+
request = LHC::Request.new(url: "http://something", rescue: handler)
|
78
78
|
expect(request.response.data.name).to eq 'unknown'
|
79
79
|
end
|
80
80
|
|
81
81
|
it 'does not exchange body with handlers return if the handler returns nil' do
|
82
82
|
stub_request(:get, "http://something").to_return(status: 400, body: { message: 'an error occurred' }.to_json)
|
83
83
|
handler = ->(_response) { nil }
|
84
|
-
request = LHC::Request.new(url: "http://something",
|
84
|
+
request = LHC::Request.new(url: "http://something", rescue: handler)
|
85
85
|
expect(request.response.data.message).to eq 'an error occurred'
|
86
86
|
end
|
87
87
|
end
|
@@ -4,7 +4,7 @@ require 'rails_helper'
|
|
4
4
|
|
5
5
|
describe LHC::Request do
|
6
6
|
context 'ignoring LHC::NotFound' do
|
7
|
-
let(:response) { LHC.get('http://local.ch',
|
7
|
+
let(:response) { LHC.get('http://local.ch', ignore: [LHC::NotFound]) }
|
8
8
|
|
9
9
|
before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
|
10
10
|
|
@@ -36,13 +36,13 @@ describe LHC::Request do
|
|
36
36
|
|
37
37
|
it "does not raise an error when it's a subclass of the ignored error" do
|
38
38
|
expect {
|
39
|
-
LHC.get('http://local.ch',
|
39
|
+
LHC.get('http://local.ch', ignore: [LHC::Error])
|
40
40
|
}.not_to raise_error
|
41
41
|
end
|
42
42
|
|
43
43
|
it "does raise an error if it's not a subclass of the ignored error" do
|
44
44
|
expect {
|
45
|
-
LHC.get('http://local.ch',
|
45
|
+
LHC.get('http://local.ch', ignore: [ArgumentError])
|
46
46
|
}.to raise_error(LHC::NotFound)
|
47
47
|
end
|
48
48
|
end
|
@@ -52,13 +52,13 @@ describe LHC::Request do
|
|
52
52
|
|
53
53
|
it "does not raise an error when ignored errors is set to array with nil" do
|
54
54
|
expect {
|
55
|
-
LHC.get('http://local.ch',
|
55
|
+
LHC.get('http://local.ch', ignore: [nil])
|
56
56
|
}.to raise_error(LHC::NotFound)
|
57
57
|
end
|
58
58
|
|
59
59
|
it "does not raise an error when ignored errors is set to nil" do
|
60
60
|
expect {
|
61
|
-
LHC.get('http://local.ch',
|
61
|
+
LHC.get('http://local.ch', ignore: nil)
|
62
62
|
}.to raise_error(LHC::NotFound)
|
63
63
|
end
|
64
64
|
end
|
@@ -67,7 +67,7 @@ describe LHC::Request do
|
|
67
67
|
before { stub_request(:get, 'http://local.ch').to_return(status: 404) }
|
68
68
|
|
69
69
|
it "does not raise an error when ignored errors is a key instead of an array" do
|
70
|
-
LHC.get('http://local.ch',
|
70
|
+
LHC.get('http://local.ch', ignore: LHC::NotFound)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|