async-http-faraday 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c35355716bc9483c9f0dcd3070e31cfa0570e49e20d25f96fa429c06d55fef5f
4
- data.tar.gz: 8781699d3bef67478a25bee464f7ccd9bb7cfcf39dae30879f7ad2c09b767e9b
3
+ metadata.gz: 91b8ac365195312b4906a8dd4f975cebdea675bda9316e97d7c0ed3186fdb9b0
4
+ data.tar.gz: 526ae0462c42d2be7e5362031a15b68388f6c723278dbcf74c0bde763d16a606
5
5
  SHA512:
6
- metadata.gz: 785d20de694024d75fd214bdfbf0680d7de1e0cb8fb695434186b51c829b2b04c9c957a6cab120ce64b75b7daa57c9fa26cd19940e22296015183793ec2bbac3
7
- data.tar.gz: 4f3cf8059b823e6db1b2be08eb76cd49d8383deb1685e8bafb31796d8c23a0f67672062c6739097a71dd944f48e346c74b9ca1c01a34ebbf0736c39fe049bebc
6
+ metadata.gz: 5bfad129a4104393375a077cd605aae4955e2adb8497a4a1655438593eb3aed8a56e966e182dc1094759617ecdb722dbe32c19de9e31211526ac705e94ef43d5
7
+ data.tar.gz: f2a3c8b0f1ad783b9ce4cfdbf57d707c5f27d953c7042504fbeaa2feb0fed18e9f6aea0070d162fb9c8301fd4c4a2b1037726fe6dbc8066be46d4bd5c1cff5fa
checksums.yaml.gz.sig CHANGED
Binary file
data/examples/topics.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  # Released under the MIT License.
5
- # Copyright, 2020-2024, by Samuel Williams.
5
+ # Copyright, 2020-2025, by Samuel Williams.
6
6
 
7
7
  $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
8
8
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2018-2024, by Samuel Williams.
4
+ # Copyright, 2018-2025, by Samuel Williams.
5
5
  # Copyright, 2018, by Andreas Garnaes.
6
6
  # Copyright, 2019, by Denis Talakevich.
7
7
  # Copyright, 2019-2020, by Igor Sidorov.
@@ -32,11 +32,16 @@ module Async
32
32
  #
33
33
  # @parameter body [Interface(:read)] The input body to wrap.
34
34
  # @parameter block_size [Integer] The size of the blocks to read from the body.
35
- def initialize(body, block_size: 4096)
35
+ # @parameter length [Integer | Nil] The length of the body, if known.
36
+ def initialize(body, length = nil, block_size: 4096)
36
37
  @body = body
38
+ @length = length
37
39
  @block_size = block_size
38
40
  end
39
41
 
42
+ # @attribute [Integer | Nil] The total length of the body, or `nil` if the length is unknown.
43
+ attr :length
44
+
40
45
  # Close the body if possible.
41
46
  def close(error = nil)
42
47
  @body.close if @body.respond_to?(:close)
@@ -111,7 +116,7 @@ module Async
111
116
  SocketError
112
117
  ].freeze
113
118
 
114
- # Create a Farady compatible adapter.
119
+ # Create a Faraday compatible adapter.
115
120
  #
116
121
  # @parameter timeout [Integer] The timeout for requests.
117
122
  # @parameter options [Hash] Additional options to pass to the underlying Async::HTTP::Client.
@@ -119,6 +124,7 @@ module Async
119
124
  super
120
125
 
121
126
  @timeout = @connection_options.delete(:timeout)
127
+ @read_timeout = @connection_options.delete(:read_timeout)
122
128
 
123
129
  if clients = @connection_options.delete(:clients)
124
130
  @clients = clients.call(**@connection_options, &@config_block)
@@ -127,6 +133,12 @@ module Async
127
133
  end
128
134
  end
129
135
 
136
+ # @attribute [Numeric | Nil] The maximum time to send a request and wait for a response.
137
+ attr :timeout
138
+
139
+ # @attribute [Numeric | Nil] The maximum time to wait for an individual IO operation.
140
+ attr :read_timeout
141
+
130
142
  # Close all clients.
131
143
  def close
132
144
  # The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
@@ -161,27 +173,32 @@ module Async
161
173
 
162
174
  def perform_request(env)
163
175
  with_client(env) do |endpoint, client|
176
+ if headers = env.request_headers
177
+ headers = ::Protocol::HTTP::Headers[headers]
178
+
179
+ # Use content-length to inform body length if given, but remove the header since it will be
180
+ # set for us later anyway, and not doing so could result in a duplicate content-length headers
181
+ # if capitalization differs
182
+ content_length = headers.delete("content-length")&.to_i
183
+ end
184
+
164
185
  if body = env.body
165
186
  # We need to ensure the body is wrapped in a Readable object so that it can be read in chunks:
166
187
  # Faraday's body only responds to `#read`.
167
188
  if body.is_a?(::Protocol::HTTP::Body::Readable)
168
189
  # Good to go
169
190
  elsif body.respond_to?(:read)
170
- body = BodyReadWrapper.new(body)
191
+ body = BodyReadWrapper.new(body, content_length)
171
192
  else
172
193
  body = ::Protocol::HTTP::Body::Buffered.wrap(body)
173
194
  end
174
195
  end
175
196
 
176
- if headers = env.request_headers
177
- headers = ::Protocol::HTTP::Headers[headers]
178
- end
179
-
180
197
  method = env.method.to_s.upcase
181
198
 
182
199
  request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
183
200
 
184
- with_timeout do
201
+ with_timeout(env.request.timeout || @timeout) do
185
202
  if env.stream_response?
186
203
  response = env.stream_response do |&on_data|
187
204
  response = client.call(request)
@@ -203,17 +220,17 @@ module Async
203
220
  end
204
221
 
205
222
  return @app.call(env)
206
- rescue Errno::ETIMEDOUT, Async::TimeoutError => e
207
- raise ::Faraday::TimeoutError, e
208
- rescue OpenSSL::SSL::SSLError => e
209
- raise ::Faraday::SSLError, e
210
- rescue *CONNECTION_EXCEPTIONS => e
211
- raise ::Faraday::ConnectionFailed, e
223
+ rescue Errno::ETIMEDOUT, Async::TimeoutError => error
224
+ raise ::Faraday::TimeoutError, error
225
+ rescue OpenSSL::SSL::SSLError => error
226
+ raise ::Faraday::SSLError, error
227
+ rescue *CONNECTION_EXCEPTIONS => error
228
+ raise ::Faraday::ConnectionFailed, error
212
229
  end
213
230
 
214
231
  def with_client(env)
215
232
  Sync do
216
- endpoint = Endpoint.new(env.url)
233
+ endpoint = Endpoint.new(env.url, timeout: @read_timeout)
217
234
 
218
235
  if proxy = env.request.proxy
219
236
  proxy_endpoint = Endpoint.new(proxy.uri)
@@ -229,9 +246,9 @@ module Async
229
246
  end
230
247
  end
231
248
 
232
- def with_timeout(task: Async::Task.current)
233
- if @timeout
234
- task.with_timeout(@timeout, ::Faraday::TimeoutError) do
249
+ def with_timeout(timeout = @timeout, task: Async::Task.current)
250
+ if timeout
251
+ task.with_timeout(timeout, ::Faraday::TimeoutError) do
235
252
  yield
236
253
  end
237
254
  else
@@ -241,7 +258,7 @@ module Async
241
258
 
242
259
  def encoded_body(response)
243
260
  body = response.read
244
- return +"" if body.nil?
261
+ return "" if body.nil?
245
262
  content_type = response.headers["content-type"]
246
263
  return body unless content_type
247
264
  params = extract_type_parameters(content_type)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2024, by Samuel Williams.
4
+ # Copyright, 2024-2025, by Samuel Williams.
5
5
 
6
6
  require "faraday"
7
7
  require "faraday/adapter"
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2020-2024, by Samuel Williams.
4
+ # Copyright, 2020-2025, by Samuel Williams.
5
5
 
6
- require_relative "adapter"
6
+ require_relative "register"
7
7
 
8
8
  # Set the default adapter to use Async::HTTP.
9
9
  ::Faraday.default_adapter = :async_http
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2025, by Samuel Williams.
5
+
6
+ require_relative "adapter"
7
+
8
+ Faraday::Adapter.register_middleware :async_http => Async::HTTP::Faraday::Adapter
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2018-2024, by Samuel Williams.
4
+ # Copyright, 2018-2025, by Samuel Williams.
5
5
 
6
6
  module Async
7
7
  module HTTP
8
8
  module Faraday
9
- VERSION = "0.20.0"
9
+ VERSION = "0.22.0"
10
10
  end
11
11
  end
12
12
  end
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2018-2024, by Samuel Williams.
4
+ # Copyright, 2018-2025, by Samuel Williams.
5
5
 
6
6
  require_relative "faraday/version"
7
- require_relative "faraday/adapter"
8
-
9
- Faraday::Adapter.register_middleware :async_http => Async::HTTP::Faraday::Adapter
7
+ require_relative "faraday/register"
10
8
 
11
9
  # @namespace
12
10
  module Async
data/readme.md CHANGED
@@ -17,6 +17,10 @@ Please see the [project documentation](https://socketry.github.io/async-http-far
17
17
 
18
18
  Please see the [project releases](https://socketry.github.io/async-http-faraday/releases/index) for all releases.
19
19
 
20
+ ### v0.21.0
21
+
22
+ - [Improved support for `timeout` and `read_timeout`.](https://socketry.github.io/async-http-faraday/releases/index#improved-support-for-timeout-and-read_timeout.)
23
+
20
24
  ### v0.20.0
21
25
 
22
26
  - Implement the new response streaming interface, which provides the initial response status code and headers before streaming the response body.
@@ -24,15 +28,15 @@ Please see the [project releases](https://socketry.github.io/async-http-faraday/
24
28
 
25
29
  ### v0.19.0
26
30
 
27
- - [Support `in_parallel`](https://socketry.github.io/async-http-faraday/releases/index#support-in_parallel)
31
+ - [Support `in_parallel`.](https://socketry.github.io/async-http-faraday/releases/index#support-in_parallel.)
28
32
 
29
33
  ### v0.18.0
30
34
 
31
- - [Config Block](https://socketry.github.io/async-http-faraday/releases/index#config-block)
35
+ - [Support for `config_block` returning a middleware wrapper.](https://socketry.github.io/async-http-faraday/releases/index#support-for-config_block-returning-a-middleware-wrapper.)
32
36
 
33
37
  ### v0.17.0
34
38
 
35
- - [Per-thread Client Cache](https://socketry.github.io/async-http-faraday/releases/index#per-thread-client-cache)
39
+ - [Introduced a per-thread `Client` cache.](https://socketry.github.io/async-http-faraday/releases/index#introduced-a-per-thread-client-cache.)
36
40
 
37
41
  ## Contributing
38
42
 
data/releases.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Releases
2
2
 
3
+ ## v0.21.0
4
+
5
+ ### Improved support for `timeout` and `read_timeout`.
6
+
7
+ Previously, only a per-connection `timeout` was supported, but now:
8
+
9
+ 1. `timeout` can be set per request too.
10
+ 2. `read_timeout` can be set per adapter and is assigned to `IO#timeout` if available.
11
+
12
+ This improves compatibility with existing code that uses `timeout` and `read_timeout`.
13
+
3
14
  ## v0.20.0
4
15
 
5
16
  - Implement the new response streaming interface, which provides the initial response status code and headers before streaming the response body.
@@ -7,7 +18,7 @@
7
18
 
8
19
  ## v0.19.0
9
20
 
10
- ### Support `in_parallel`
21
+ ### Support `in_parallel`.
11
22
 
12
23
  The adapter now supports the `in_parallel` method, which allows multiple requests to be made concurrently.
13
24
 
@@ -45,7 +56,7 @@ end
45
56
 
46
57
  ## v0.18.0
47
58
 
48
- ### Config Block
59
+ ### Support for `config_block` returning a middleware wrapper.
49
60
 
50
61
  The `config_block` provided to the adapter must now return `nil`, `client` or a middleware wrapper around `client`.
51
62
 
@@ -63,7 +74,7 @@ end
63
74
 
64
75
  ## v0.17.0
65
76
 
66
- ### Per-thread Client Cache
77
+ ### Introduced a per-thread `Client` cache.
67
78
 
68
79
  The default adapter now uses a per-thread client cache internally, to improve compatibility with existing code that shares a single `Faraday::Connection` instance across multiple threads.
69
80
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-http-faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -45,7 +45,7 @@ cert_chain:
45
45
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
46
46
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
47
47
  -----END CERTIFICATE-----
48
- date: 2025-01-23 00:00:00.000000000 Z
48
+ date: 1980-01-02 00:00:00.000000000 Z
49
49
  dependencies:
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: async-http
@@ -84,6 +84,7 @@ files:
84
84
  - lib/async/http/faraday/adapter.rb
85
85
  - lib/async/http/faraday/clients.rb
86
86
  - lib/async/http/faraday/default.rb
87
+ - lib/async/http/faraday/register.rb
87
88
  - lib/async/http/faraday/version.rb
88
89
  - license.md
89
90
  - readme.md
@@ -109,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  - !ruby/object:Gem::Version
110
111
  version: '0'
111
112
  requirements: []
112
- rubygems_version: 3.6.2
113
+ rubygems_version: 3.6.7
113
114
  specification_version: 4
114
115
  summary: Provides an adaptor between async-http and faraday.
115
116
  test_files: []
metadata.gz.sig CHANGED
Binary file