hydrafetch 0.5.0 → 0.7.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: a2db80b66abcc7cb6948142487ea814abe860920e18eeff4e0fe288b08d82490
4
- data.tar.gz: 02bd43273da1066e14cb2f36645d7d4e127028e068ff10f372d5c1ab59d77614
3
+ metadata.gz: bdf2b68d39f76d921042d0995c8686d58716b288884c919b6302943ee852f591
4
+ data.tar.gz: 61cfab340278a3bbae9c5af93bd7d3a492e28ca3d2ff8ccb6a40dcfd6d9f9c5b
5
5
  SHA512:
6
- metadata.gz: bdd7d850c628a7fe035026a462aa0355bf114dc02b8cbd5b61a4a248773a4fd9097558833ec6b11c7665bda288e9f4884c015df93851271133b7dc92f60cef57
7
- data.tar.gz: 0a524dbaaf0078234d73c532c1583401de9b4d3e83b2138a12de716dc92b0a73714564d572056559b5837749af742826488c44b989db8728ab8684dc6ddffe9b
6
+ metadata.gz: 4da1f0021b7f34e10f403ef305e560d0f6f0b2782fb280d2b96ef92fe70d8176402cc91fe5308ed799d03cbfcec8162942aec5dc05edbe553c8e6ecd470df411
7
+ data.tar.gz: 2d1a22c4113af9597730eeaa99470cd7e505b35d8985869e1ee9449778c56bf38b14737e7be8d011a80715d14e45704a9df6f8186fe7d0611812441ed20d78c9
data/README.md CHANGED
@@ -84,7 +84,7 @@ Pass `prompt:` instead of, or alongside, `schema:` to describe the fields in pla
84
84
 
85
85
  ```ruby
86
86
  links = hf.map("https://example.com", limit: 1000)["links"]
87
- docs = links.select { |url| url.include?("/docs/") }
87
+ docs = links.map { |link| link["url"] }.select { |url| url.include?("/docs/") }
88
88
  ```
89
89
 
90
90
  `batch` and `crawl` submit a job and poll until it finishes.
@@ -189,7 +189,7 @@ rescue Hydrafetch::Error => e
189
189
  return refresh_key if e.auth?
190
190
  return top_up if e.out_of_credits?
191
191
  return report(e.message) if e.invalid_request?
192
- return enqueue(url) if e.retryable?
192
+ return enqueue(url, e.retry_after) if e.retryable?
193
193
 
194
194
  warn [e.code, e.status, e.request_id].join(" ")
195
195
  raise
@@ -202,8 +202,10 @@ end
202
202
  | 401, 403 | invalid or missing key | no |
203
203
  | 402 | out of credits | no |
204
204
  | 404 | page does not exist | no |
205
- | 429 | rate limited | yes, twice with backoff |
206
- | 5xx | upstream failure | yes, twice with backoff |
205
+ | 429 | rate limited | yes, twice |
206
+ | 5xx | upstream failure | yes, twice |
207
+
208
+ Retries wait as long as the response asks through `Retry-After` or `retryAfterSeconds`, and otherwise back off with jitter. When the response asks for more than 30 seconds the client does not wait; it raises at once and `e.retry_after` gives the seconds to wait before asking again. A dropped connection is retried too, including while the client follows a job, and a job that fails is never started a second time.
207
209
 
208
210
  A 503 from `scrape` means the origin is unreachable, usually a dead domain or a broken certificate.
209
211
 
@@ -11,6 +11,14 @@ module Hydrafetch
11
11
  DEFAULT_TIMEOUT = 120
12
12
  DEFAULT_MAX_RETRIES = 2
13
13
  TERMINAL_STATUSES = %w[completed failed cancelled].freeze
14
+ ACCEPTED = 202
15
+ CALL_JOB_TIMEOUT = 330
16
+ CALL_JOB_POLL = 2
17
+ MAX_RETRY_WAIT = 30
18
+ BACKOFF_CAP = 8.0
19
+ TRANSPORT_ERRORS = [SocketError, IOError, SystemCallError].freeze
20
+
21
+ attr_writer :call_job_poll
14
22
 
15
23
  attr_reader :base_url, :last_request_id
16
24
 
@@ -188,18 +196,75 @@ module Hydrafetch
188
196
 
189
197
  res = http_for(uri).request(req)
190
198
  @last_request_id = res["x-request-id"] || @last_request_id
191
- unwrap(res)
199
+ result = unwrap(res)
192
200
  rescue Error => e
193
- raise unless e.retryable? && attempt < @max_retries
201
+ wait = retry_wait(e, attempt)
202
+ raise unless e.retryable? && attempt < @max_retries && wait
194
203
 
195
204
  attempt += 1
196
- sleep((2**attempt) * 0.25)
205
+ sleep wait
197
206
  retry
198
207
  rescue Net::OpenTimeout, Net::ReadTimeout => e
199
208
  raise TimeoutError.new(
200
209
  "Request to #{path} timed out after #{@timeout}s. Raise timeout:, " \
201
210
  "or use the crawl and batch job endpoints for long work. (#{e.class})"
202
211
  )
212
+ rescue *TRANSPORT_ERRORS
213
+ raise unless attempt < @max_retries
214
+
215
+ sleep backoff(attempt)
216
+ attempt += 1
217
+ retry
218
+ end
219
+ poll = res.code.to_i == ACCEPTED && result.is_a?(Hash) ? result["poll"] : nil
220
+ poll ? follow_call_job(poll) : result
221
+ end
222
+
223
+ def retry_wait(error, attempt)
224
+ return backoff(attempt) if error.retry_after.nil?
225
+
226
+ error.retry_after > MAX_RETRY_WAIT ? nil : error.retry_after
227
+ end
228
+
229
+ def backoff(attempt)
230
+ rand * [BACKOFF_CAP, 0.5 * (2**attempt)].min
231
+ end
232
+
233
+ def retry_after(res, details)
234
+ header = res["retry-after"].to_s.strip
235
+ return header.to_i if header.match?(/\A\d+\z/)
236
+ return nil unless details.is_a?(Hash)
237
+
238
+ %w[retryAfterSeconds retryAfter].each do |key|
239
+ value = details[key]
240
+ return value if value.is_a?(Integer) && value >= 0
241
+ end
242
+ nil
243
+ end
244
+
245
+ def follow_call_job(poll)
246
+ interval = @call_job_poll || CALL_JOB_POLL
247
+ deadline = monotonic + CALL_JOB_TIMEOUT
248
+ loop do
249
+ sleep interval
250
+ job = request(Net::HTTP::Get, poll)
251
+ case job["status"]
252
+ when "completed"
253
+ result = job["result"]
254
+ return result.is_a?(Hash) && result.key?("data") ? result["data"] : result
255
+ when "failed"
256
+ error = job["error"] || {}
257
+ raise Error.new(
258
+ error["message"] || "The job failed.",
259
+ code: error["code"] || "JOB_FAILED",
260
+ status: error["status"] || 500,
261
+ request_id: job["jobId"],
262
+ details: error["details"]
263
+ )
264
+ end
265
+ if monotonic + interval > deadline
266
+ raise TimeoutError.new("The job did not finish in time; poll #{poll} for its result.")
267
+ end
203
268
  end
204
269
  end
205
270
 
@@ -228,7 +293,8 @@ module Hydrafetch
228
293
  code: (error && error["code"]) || "HTTP_#{status}",
229
294
  status: status,
230
295
  request_id: meta && meta["requestId"],
231
- details: error && error["details"]
296
+ details: error && error["details"],
297
+ retry_after: retry_after(res, error && error["details"])
232
298
  )
233
299
  end
234
300
 
@@ -1,13 +1,14 @@
1
1
  module Hydrafetch
2
2
  class Error < StandardError
3
- attr_reader :code, :status, :request_id, :details
3
+ attr_reader :code, :status, :request_id, :details, :retry_after
4
4
 
5
- def initialize(message, code: "UNKNOWN", status: 500, request_id: nil, details: nil)
5
+ def initialize(message, code: "UNKNOWN", status: 500, request_id: nil, details: nil, retry_after: nil)
6
6
  super(message)
7
7
  @code = code
8
8
  @status = status
9
9
  @request_id = request_id
10
10
  @details = details
11
+ @retry_after = retry_after
11
12
  end
12
13
 
13
14
  def to_s
@@ -1,3 +1,3 @@
1
1
  module Hydrafetch
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "0.7.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydrafetch
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
  - Hydrafetch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-05 00:00:00.000000000 Z
11
+ date: 2026-11-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Turn any URL into clean Markdown and structured data. Scrape, map, search,
14
14
  extract, brand and bulk crawl through one API.