twingly-http 0.5.0 → 0.7.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: 504e1df993b190cf3039e5b45b9586f2babaab2fe5e1a881a10dfc407fd21663
4
- data.tar.gz: 6081a1578382fa683f5ae71f52749988b52346dfef87cbcc9295d3ac33453bb8
3
+ metadata.gz: 62b69e5cac842eeefbaad91288707eb474458da8df5074521652fccd486edc89
4
+ data.tar.gz: 33c81395fbfd3f1588452fb93b66a067c40521654042f0eb6f04acd8a362b0c9
5
5
  SHA512:
6
- metadata.gz: 493b7190c970e227735c7ffa8e7377f243cec8c8e24778662b3f9e9f187f713e16f68404c0c9c6322ad002fd05f75e2088027471747b2d022fb204e5ca719729
7
- data.tar.gz: 68baaace97462c88bbbd84a082848264ee5515fde8505cf3e0e3524912d0b4f3387aa9c3057b577a683dbcd6f39ffec9dbaeae65c2b4bd3a35c8e5892f2684b0
6
+ metadata.gz: 3a95ad89c7bf1af645cb000bb739e8100048a268185e88d75ac307581f0832e85586f34fa1a4bc9f1f7291ca2147a2b9f61d032d562662a3f5a84aaca8750f3e
7
+ data.tar.gz: 4b79a8ea8a109fe5cbf0e57c0591e4a5382b10ba5bb5a05ada0ca96ed0197488fcf1e9bce47d0e1f61eb8d1ec016d5f1afec1338131a3b7b84388e85f35d2bf7
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module ResponseBodySizeLimit
5
+ class LimitExceededError < StandardError; end
6
+
7
+ class Middleware < Faraday::Middleware
8
+ def initialize(app, max_size_bytes:)
9
+ super(app)
10
+
11
+ @max_size_bytes = max_size_bytes
12
+ end
13
+
14
+ def call(env) # rubocop:disable Metrics/MethodLength
15
+ response_body_size = 0
16
+ accumulated_body = + ""
17
+
18
+ env.request.on_data = proc do |chunk, _|
19
+ response_body_size += chunk.bytesize
20
+ accumulated_body << chunk
21
+
22
+ if response_body_size > @max_size_bytes
23
+ raise LimitExceededError,
24
+ "Response body too large, exceeced the configured max size of #{@max_size_bytes} bytes."
25
+ end
26
+ end
27
+
28
+ @app.call(env).on_complete do |response_env|
29
+ response_env.body = accumulated_body
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Faraday::Request.register_middleware(
37
+ response_body_size_limit: Faraday::ResponseBodySizeLimit::Middleware
38
+ )
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "response_body_size_limit/middleware"
4
+
5
+ module Faraday
6
+ module ResponseBodySizeLimit
7
+ end
8
+ end
@@ -16,7 +16,7 @@ module Faraday
16
16
 
17
17
  if url_bytesize >= @max_size_bytes
18
18
  raise LimitExceededError,
19
- "Expected URL below #{@max_size_bytes} bytes, "\
19
+ "Expected URL below #{@max_size_bytes} bytes, " \
20
20
  "was #{url_bytesize} bytes"
21
21
  end
22
22
 
data/lib/twingly/http.rb CHANGED
@@ -7,6 +7,7 @@ require "faraday_middleware"
7
7
 
8
8
  require_relative "../faraday/logfmt_logger"
9
9
  require_relative "../faraday/url_size_limit"
10
+ require_relative "../faraday/response_body_size_limit"
10
11
  require_relative "heroku"
11
12
  require_relative "string_utilities"
12
13
 
@@ -15,6 +16,7 @@ module Twingly
15
16
  class ConnectionError < StandardError; end
16
17
  class UrlSizeLimitExceededError < StandardError; end
17
18
  class RedirectLimitReachedError < StandardError; end
19
+ class ResponseBodySizeLimitExceededError < StandardError; end
18
20
  class Client # rubocop:disable Metrics/ClassLength
19
21
  DEFAULT_RETRYABLE_EXCEPTIONS = [
20
22
  Faraday::ConnectionFailed,
@@ -31,6 +33,7 @@ module Twingly
31
33
  DEFAULT_NUMBER_OF_RETRIES = 0
32
34
  DEFAULT_RETRY_INTERVAL = 1
33
35
  DEFAULT_MAX_URL_SIZE_BYTES = Float::INFINITY
36
+ DEFAULT_MAX_RESPONSE_BODY_SIZE_BYTES = Float::INFINITY
34
37
  DEFAULT_FOLLOW_REDIRECTS_LIMIT = 3
35
38
 
36
39
  attr_writer :http_timeout
@@ -39,6 +42,7 @@ module Twingly
39
42
  attr_writer :retry_interval
40
43
  attr_writer :on_retry_callback
41
44
  attr_writer :max_url_size_bytes
45
+ attr_writer :max_response_body_size_bytes
42
46
  attr_writer :request_id
43
47
  attr_writer :follow_redirects
44
48
 
@@ -46,9 +50,10 @@ module Twingly
46
50
  attr_accessor :logger
47
51
  attr_accessor :retryable_exceptions
48
52
 
49
- def initialize(base_user_agent:, logger: default_logger)
53
+ def initialize(base_user_agent:, logger: default_logger, user_agent: nil)
50
54
  @base_user_agent = base_user_agent
51
55
  @logger = logger
56
+ @user_agent = user_agent
52
57
 
53
58
  initialize_defaults
54
59
  end
@@ -80,19 +85,20 @@ module Twingly
80
85
  end
81
86
 
82
87
  def initialize_defaults
83
- @request_id = nil
84
- @http_timeout = DEFAULT_HTTP_TIMEOUT
85
- @http_open_timeout = DEFAULT_HTTP_OPEN_TIMEOUT
86
- @retryable_exceptions = DEFAULT_RETRYABLE_EXCEPTIONS
87
- @number_of_retries = DEFAULT_NUMBER_OF_RETRIES
88
- @retry_interval = DEFAULT_RETRY_INTERVAL
89
- @on_retry_callback = nil
90
- @follow_redirects = false
91
- @follow_redirects_limit = DEFAULT_FOLLOW_REDIRECTS_LIMIT
92
- @max_url_size_bytes = DEFAULT_MAX_URL_SIZE_BYTES
88
+ @request_id = nil
89
+ @http_timeout = DEFAULT_HTTP_TIMEOUT
90
+ @http_open_timeout = DEFAULT_HTTP_OPEN_TIMEOUT
91
+ @retryable_exceptions = DEFAULT_RETRYABLE_EXCEPTIONS
92
+ @number_of_retries = DEFAULT_NUMBER_OF_RETRIES
93
+ @retry_interval = DEFAULT_RETRY_INTERVAL
94
+ @on_retry_callback = nil
95
+ @follow_redirects = false
96
+ @follow_redirects_limit = DEFAULT_FOLLOW_REDIRECTS_LIMIT
97
+ @max_url_size_bytes = DEFAULT_MAX_URL_SIZE_BYTES
98
+ @max_response_body_size_bytes = DEFAULT_MAX_RESPONSE_BODY_SIZE_BYTES
93
99
  end
94
100
 
95
- def http_response_for(method, **args)
101
+ def http_response_for(method, **args) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
96
102
  response = send("http_#{method}_response", **args)
97
103
 
98
104
  Response.new(headers: response.headers.to_h,
@@ -105,6 +111,8 @@ module Twingly
105
111
  raise UrlSizeLimitExceededError, error.message
106
112
  rescue FaradayMiddleware::RedirectLimitReached => error
107
113
  raise RedirectLimitReachedError, error.message
114
+ rescue Faraday::ResponseBodySizeLimit::LimitExceededError => error
115
+ raise ResponseBodySizeLimitExceededError, error.message
108
116
  end
109
117
 
110
118
  def http_get_response(url:, params:, headers:)
@@ -192,6 +200,8 @@ module Twingly
192
200
  exceptions: @retryable_exceptions,
193
201
  methods: [], # empty [] forces Faraday to run retry_if
194
202
  retry_if: retry_if
203
+ faraday.request :response_body_size_limit,
204
+ max_size_bytes: @max_response_body_size_bytes
195
205
  faraday.response :logfmt_logger, @logger.dup,
196
206
  headers: true,
197
207
  bodies: true,
@@ -232,7 +242,7 @@ module Twingly
232
242
  end
233
243
 
234
244
  def user_agent
235
- format(
245
+ @user_agent || format(
236
246
  "%<base>s (Release/%<release>s; Commit/%<commit>s)",
237
247
  base: @base_user_agent,
238
248
  release: Heroku.release_version,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Twingly
4
4
  module HTTP
5
- VERSION = "0.5.0"
5
+ VERSION = "0.7.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twingly-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Twingly AB
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-24 00:00:00.000000000 Z
11
+ date: 2024-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -44,118 +44,6 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: 1.0.0
47
- - !ruby/object:Gem::Dependency
48
- name: climate_control
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '0.1'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '0.1'
61
- - !ruby/object:Gem::Dependency
62
- name: rake
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '12'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '12'
75
- - !ruby/object:Gem::Dependency
76
- name: rspec
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '3'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '3'
89
- - !ruby/object:Gem::Dependency
90
- name: rubocop
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: 1.14.0
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: 1.14.0
103
- - !ruby/object:Gem::Dependency
104
- name: rubocop-rspec
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: 2.4.0
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: 2.4.0
117
- - !ruby/object:Gem::Dependency
118
- name: toxiproxy
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: '1.0'
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '1.0'
131
- - !ruby/object:Gem::Dependency
132
- name: vcr
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '5.0'
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '5.0'
145
- - !ruby/object:Gem::Dependency
146
- name: webmock
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: '3.7'
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- requirements:
156
- - - "~>"
157
- - !ruby/object:Gem::Version
158
- version: '3.7'
159
47
  description: Robust HTTP client tailored by Twingly
160
48
  email:
161
49
  - support@twingly.com
@@ -168,6 +56,8 @@ files:
168
56
  - lib/faraday/logfmt_logger.rb
169
57
  - lib/faraday/logfmt_logger/LICENSE.md
170
58
  - lib/faraday/logfmt_logger/middleware.rb
59
+ - lib/faraday/response_body_size_limit.rb
60
+ - lib/faraday/response_body_size_limit/middleware.rb
171
61
  - lib/faraday/url_size_limit.rb
172
62
  - lib/faraday/url_size_limit/middleware.rb
173
63
  - lib/twingly/heroku.rb
@@ -177,7 +67,8 @@ files:
177
67
  homepage: http://github.com/twingly/twingly-http
178
68
  licenses:
179
69
  - MIT
180
- metadata: {}
70
+ metadata:
71
+ rubygems_mfa_required: 'true'
181
72
  post_install_message:
182
73
  rdoc_options: []
183
74
  require_paths:
@@ -193,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
84
  - !ruby/object:Gem::Version
194
85
  version: '0'
195
86
  requirements: []
196
- rubygems_version: 3.3.7
87
+ rubygems_version: 3.5.4
197
88
  signing_key:
198
89
  specification_version: 4
199
90
  summary: Robust HTTP client