internetdata 2.9.1 → 2.10.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.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +146 -9
  3. data/lib/internetdata/api/database_v2_api.rb +2 -2
  4. data/lib/internetdata/api_client.rb +2 -2
  5. data/lib/internetdata/api_error.rb +2 -2
  6. data/lib/internetdata/api_model_base.rb +2 -2
  7. data/lib/internetdata/asn_result.rb +107 -0
  8. data/lib/internetdata/bogon.rb +87 -0
  9. data/lib/internetdata/bogons.rb +93 -0
  10. data/lib/internetdata/cache.rb +35 -0
  11. data/lib/internetdata/client.rb +388 -16
  12. data/lib/internetdata/configuration.rb +23 -2
  13. data/lib/internetdata/domain_result.rb +124 -0
  14. data/lib/internetdata/errors.rb +29 -6
  15. data/lib/internetdata/models/abuse_contact.rb +236 -0
  16. data/lib/internetdata/models/asn.rb +294 -0
  17. data/lib/internetdata/models/asn_lookup_response.rb +405 -0
  18. data/lib/internetdata/models/asn_prefix.rb +329 -0
  19. data/lib/internetdata/models/batch_lookup_error.rb +192 -0
  20. data/lib/internetdata/models/bit_torrent_detail.rb +410 -0
  21. data/lib/internetdata/models/carrier_detail.rb +273 -0
  22. data/lib/internetdata/models/class_detail.rb +246 -0
  23. data/lib/internetdata/models/client_registration.rb +2 -2
  24. data/lib/internetdata/models/company.rb +213 -0
  25. data/lib/internetdata/models/database.rb +2 -2
  26. data/lib/internetdata/models/database_checksums_response.rb +2 -2
  27. data/lib/internetdata/models/database_format.rb +2 -2
  28. data/lib/internetdata/models/database_list.rb +2 -2
  29. data/lib/internetdata/models/database_metadata.rb +2 -2
  30. data/lib/internetdata/models/database_metadata_column.rb +2 -2
  31. data/lib/internetdata/models/database_version.rb +2 -2
  32. data/lib/internetdata/models/db_checksums.rb +2 -2
  33. data/lib/internetdata/models/device_authorization.rb +2 -2
  34. data/lib/internetdata/models/disposable_detail.rb +248 -0
  35. data/lib/internetdata/models/dns_address_record.rb +192 -0
  36. data/lib/internetdata/models/dns_cname_record.rb +192 -0
  37. data/lib/internetdata/models/dns_mx_record.rb +219 -0
  38. data/lib/internetdata/models/dns_ns_record.rb +192 -0
  39. data/lib/internetdata/models/dns_resolver_detail.rb +300 -0
  40. data/lib/internetdata/models/domain_dns.rb +279 -0
  41. data/lib/internetdata/models/domain_ip_range.rb +219 -0
  42. data/lib/internetdata/models/domain_ip_ranges.rb +276 -0
  43. data/lib/internetdata/models/domain_lookup_response.rb +376 -0
  44. data/lib/internetdata/models/download.rb +2 -2
  45. data/lib/internetdata/models/download_list.rb +2 -2
  46. data/lib/internetdata/models/entitlement.rb +243 -0
  47. data/lib/internetdata/models/entitlement_apikey.rb +207 -0
  48. data/lib/internetdata/models/entitlement_plan.rb +216 -0
  49. data/lib/internetdata/models/entitlement_usage.rb +259 -0
  50. data/lib/internetdata/models/error_envelope.rb +2 -2
  51. data/lib/internetdata/models/hosted_domains.rb +166 -0
  52. data/lib/internetdata/models/location.rb +509 -0
  53. data/lib/internetdata/models/lookup_response.rb +897 -0
  54. data/lib/internetdata/models/oauth_metadata.rb +2 -2
  55. data/lib/internetdata/models/proxy_detail.rb +327 -0
  56. data/lib/internetdata/models/pubproxy_detail.rb +329 -0
  57. data/lib/internetdata/models/rwhois.rb +301 -0
  58. data/lib/internetdata/models/satellite_detail.rb +327 -0
  59. data/lib/internetdata/models/smtp_detail.rb +273 -0
  60. data/lib/internetdata/models/ssh_detail.rb +300 -0
  61. data/lib/internetdata/models/standing.rb +2 -2
  62. data/lib/internetdata/models/token_response.rb +2 -2
  63. data/lib/internetdata/models/web_detail.rb +516 -0
  64. data/lib/internetdata/models/whois.rb +382 -0
  65. data/lib/internetdata/oauth_api.rb +12 -5
  66. data/lib/internetdata/result.rb +431 -0
  67. data/lib/internetdata/transport.rb +215 -27
  68. data/lib/internetdata/version.rb +1 -1
  69. data/lib/internetdata.rb +17 -2
  70. metadata +58 -3
@@ -3,43 +3,415 @@
3
3
  require 'typhoeus'
4
4
 
5
5
  module InternetData
6
- DEFAULT_BASE_URL = 'https://internetdata.io'
6
+ DEFAULT_BASE_URL = 'https://api.internetdata.io'
7
+ DEFAULT_CACHE_MAX_SIZE = 10_000
8
+ DEFAULT_CACHE_TTL = 3600
9
+ DEFAULT_CONCURRENCY = 8
7
10
  DEFAULT_RETRIES = 2
8
11
  DEFAULT_TIMEOUT = 30
12
+ # The most addresses POST /batch takes in one call; a larger batch is sent in
13
+ # chunks of this size.
14
+ BATCH_MAX = 1000
9
15
 
10
16
  # A client for the InternetData API.
11
17
  #
12
- # Every database published today needs an API key carrying the `db.download`
13
- # scope, granted by contract one family at a time. The key is still OPTIONAL:
14
- # a client built without one sends no `Authorization` header at all, which is
15
- # what a database served without a license would need, and `oauth` needs none.
18
+ # The cache is per instance, so an answer is never shared between two clients
19
+ # holding different API keys and therefore entitled to different fields.
16
20
  class Client
17
- # The licensed database downloads, and everything about them.
21
+ # The licensed database downloads, for keys that carry the `db.download` scope.
18
22
  attr_reader :database
19
23
  # Signing a person in with OAuth, which needs no API key at all.
20
24
  attr_reader :oauth
21
25
 
22
- # @param api_key [String, nil] a key carrying the `db.download` scope. Omit
23
- # it and no credential is sent; every database published today answers 401.
24
- # The blanks around it are dropped, and blanks alone are no key.
26
+ # @param api_key [String, nil] without one a lookup is on Unlimited, the free
27
+ # plan: an address's location, network, company, abuse contact, reverse
28
+ # DNS and every flag but the paid ones, an AS's registration and a
29
+ # domain's, limited per address per minute. A batch needs a key on a paid
30
+ # plan, and the databases a key carrying the `db.download` scope. It is
31
+ # trimmed, and a key of blanks alone is no key.
32
+ # @param base_url [String] the API; {#oauth} signs in at its apex, `api.`
33
+ # dropped and `api-<env>.` becoming `<env>.`, and at any other URL as given.
34
+ # @param cache [Boolean] pass false to disable caching.
35
+ # @param cache_ttl [Numeric] how long an answer stays fresh, in seconds.
36
+ # @param concurrency [Integer] batch requests - chunks of up to 1000 addresses -
37
+ # in flight during a batch, from 1.
25
38
  # @param retries [Integer] extra attempts for a transient failure, from 0. Each
26
39
  # waits the server's `Retry-After` when it sent one of at most about 24.8
27
40
  # days, and otherwise a backoff of 250 ms that doubles per retry.
28
- # @param timeout [Numeric] seconds allowed for one API call, 0 for no bound. It
29
- # also bounds the CONNECT phase of a transfer, which is otherwise unlimited.
30
- # @raise [ArgumentError] for a `timeout` that is negative, not a finite number,
31
- # or past 2147483.647 seconds (2**31 - 1 ms), the longest curl holds; for a
32
- # `retries` that is not an Integer from 0; and for an `api_key` holding a
33
- # control character, without the key in the message.
41
+ # @param timeout [Numeric] seconds one request may take before it is
42
+ # abandoned, 0 for no bound. Applies per ATTEMPT, so a retried call may take
43
+ # longer in total, and every call that takes `retries:` also takes
44
+ # `timeout:` to override it. A database transfer bounds only its connect
45
+ # phase with it.
46
+ # @raise [ArgumentError] for an `api_key` with a control character in it.
47
+ # @raise [ArgumentError] for a `timeout`, here or on any call, that is
48
+ # negative, not a finite number, or past 2147483.647 seconds (2**31 - 1 ms),
49
+ # the longest curl holds, for a `retries`, here or on any call, that is
50
+ # not an Integer from 0, and for a `concurrency` that is not an Integer
51
+ # from 1.
34
52
  # @param transport [Transport, nil] override the HTTP layer, mostly for tests.
35
- def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, retries: DEFAULT_RETRIES,
53
+ def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, cache: true,
54
+ cache_max_size: DEFAULT_CACHE_MAX_SIZE, cache_ttl: DEFAULT_CACHE_TTL,
55
+ concurrency: DEFAULT_CONCURRENCY, retries: DEFAULT_RETRIES,
36
56
  timeout: DEFAULT_TIMEOUT, transport: nil)
37
57
  retries = Retries.checked_count(retries)
58
+ unless concurrency?(concurrency)
59
+ raise ArgumentError, "concurrency must be an Integer from 1, not #{concurrency.inspect}"
60
+ end
38
61
  @transport = transport || Transport.new(
39
62
  Transport::Config.new(api_key: api_key, base_url: base_url, timeout: timeout),
40
63
  )
64
+ @cache = cache ? Cache.new(max_size: cache_max_size, ttl: cache_ttl) : nil
65
+ # The addresses with a request in flight, each a Flight its waiters block on.
66
+ @flights = {}
67
+ @flights_lock = Mutex.new
68
+ @concurrency = concurrency
69
+ @retries = retries
41
70
  @database = DatabaseApi.new(@transport, retries: retries)
42
71
  @oauth = OauthApi.new(@transport, retries: retries)
43
72
  end
73
+
74
+ # Whether an address is private, loopback, link-local, documentation,
75
+ # multicast or otherwise not routable, including the IPv6 equivalents and
76
+ # the 6to4 and Teredo ranges.
77
+ #
78
+ # These are the addresses {#lookup} answers locally. Exposed here so the
79
+ # check is reachable from the client you already hold; the same predicate is
80
+ # also on the module itself, for code with no client to hand.
81
+ def bogon?(ip)
82
+ Bogon.bogon?(ip)
83
+ end
84
+
85
+ # What every database says about one address.
86
+ #
87
+ # A bogon is answered locally and never reaches the network. Everything else
88
+ # is served, then cached for this instance.
89
+ #
90
+ # @param retries [Integer, nil] extra attempts, for THIS call only.
91
+ # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
92
+ # @return [Result]
93
+ def lookup(ip, retries: nil, timeout: nil)
94
+ # Here as well as in the transport: a bogon or a cached answer returns before any request.
95
+ Transport.checked_timeout(timeout) unless timeout.nil?
96
+ Retries.checked_count(retries) unless retries.nil?
97
+ # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
98
+ ip = Bogon.unmapped(ip)
99
+ return Bogon.result(ip) if Bogon.bogon?(ip)
100
+
101
+ loop do
102
+ hit = @cache&.get(ip)
103
+ return hit unless hit.nil?
104
+
105
+ flight, leader = board(ip)
106
+ return fetch(ip, retries, timeout) if flight.nil?
107
+
108
+ if leader
109
+ # Read once more now the address is boarded: a leader that landed
110
+ # between the miss above and boarding has already cached its answer.
111
+ hit = @cache.get(ip)
112
+ return unboard(ip, flight, :served, hit) unless hit.nil?
113
+
114
+ return lead(ip, flight) { fetch(ip, retries, timeout) }
115
+ end
116
+ state, value = flight.wait
117
+ return value if state == :served
118
+ raise value if state == :failed
119
+ # :abandoned - its leader never finished, so this caller asks again.
120
+ end
121
+ end
122
+
123
+ # What every database says about one AS. Sent as given, so `9009` and
124
+ # `"AS9009"` ask the same; one the API cannot read is a `:bad_request`. Not
125
+ # cached.
126
+ #
127
+ # @param retries [Integer, nil] extra attempts, for THIS call only.
128
+ # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
129
+ # @return [AsnResult]
130
+ def lookup_asn(asn, retries: nil, timeout: nil)
131
+ Retries.checked_count(retries) unless retries.nil?
132
+ Retries.with_retries(retries || @retries) do
133
+ Transport.asn_result(@transport.asn_request(asn, timeout: timeout).run)
134
+ end
135
+ end
136
+
137
+ # What every database says about one domain. Sent as given: the API reads it
138
+ # in any case and answers an internationalized name under its punycode
139
+ # spelling, and one it cannot read is a `:bad_request`. Not cached.
140
+ #
141
+ # @param retries [Integer, nil] extra attempts, for THIS call only.
142
+ # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
143
+ # @return [DomainResult]
144
+ def lookup_domain(domain, retries: nil, timeout: nil)
145
+ Retries.checked_count(retries) unless retries.nil?
146
+ Retries.with_retries(retries || @retries) do
147
+ Transport.domain_result(@transport.domain_request(domain, timeout: timeout).run)
148
+ end
149
+ end
150
+
151
+ # What every database says about the address this client is calling from.
152
+ #
153
+ # The same answer {#lookup} would give for that address, at the same cost
154
+ # against your allowance. The address is the one our edge observed, so a
155
+ # call made through a proxy or a VPN reports the exit it left through -
156
+ # usually the point of asking.
157
+ #
158
+ # Deliberately NOT cached. The cache is keyed by address, and which address
159
+ # this is IS the question: a machine that moves between networks would
160
+ # otherwise be told where it used to be.
161
+ #
162
+ # @return [Result]
163
+ def my_ip(retries: nil, timeout: nil)
164
+ Retries.checked_count(retries) unless retries.nil?
165
+ Retries.with_retries(retries || @retries) do
166
+ Transport.lookup_result(@transport.myip_request(timeout: timeout).run)
167
+ end
168
+ end
169
+
170
+ # What this client's key is entitled to, and how much of it has been used.
171
+ #
172
+ # Named for what it answers rather than `me`, which sits one letter from
173
+ # {#my_ip} and means something quite different: one is which address you are
174
+ # calling FROM, the other is what the key you are calling WITH may spend.
175
+ #
176
+ # Unlike a lookup there is no useful unauthenticated answer, so a client
177
+ # built without an API key gets an unauthorized error rather than a partial
178
+ # one.
179
+ #
180
+ # Usage counts against the ALLOWANCE WINDOW - on a paid plan the
181
+ # anniversary of the subscription, not the calendar month and not the
182
+ # billing period, and on the free plan the UTC day - and it is the same
183
+ # number a lookup is gated on. It can lag by a few seconds, because
184
+ # requests are counted in memory and flushed in aggregate.
185
+ #
186
+ # Deliberately NOT cached: the whole point is what has been spent, and a
187
+ # cached answer is a wrong one within seconds of the next request.
188
+ #
189
+ # @return [Entitlement]
190
+ def my_entitlement(retries: nil, timeout: nil)
191
+ Retries.checked_count(retries) unless retries.nil?
192
+ Retries.with_retries(retries || @retries) do
193
+ Transport.entitlement_result(@transport.entitlement_request(timeout: timeout).run)
194
+ end
195
+ end
196
+
197
+ # Look many addresses up in as few requests as possible.
198
+ #
199
+ # Bogons are answered locally and cached answers are reused; everything else
200
+ # goes to the batch endpoint in chunks of up to 1000 addresses, with at most
201
+ # `concurrency` chunks in flight. Keyed by address rather than positional, so
202
+ # duplicates in the input collapse to a single entry and the caller never has
203
+ # to line two lists up. An address that fails carries its error as its value,
204
+ # so one bad entry cannot lose the rest of the answers: the API reports a
205
+ # per-entry failure with the status the single lookup would have answered,
206
+ # and a chunk that fails as a whole marks every address in it. The batch
207
+ # endpoint needs a paid plan: without a key every address in a request of
208
+ # two or more carries an `:unauthorized` error, and a free-plan key's
209
+ # request of two or more a `:bad_request` (HTTP 402). A request of one
210
+ # address is answered as its lookup would be.
211
+ #
212
+ # @param concurrency [Integer, nil] chunks in flight, for THIS batch only.
213
+ # Anything but an Integer from 1 is refused as `:bad_request` before any
214
+ # request, and before a bogon or a cached answer is returned.
215
+ # @param retries [Integer, nil] extra attempts for a failed chunk, for THIS batch only.
216
+ # @param timeout [Numeric, nil] seconds each chunk's attempt may take, for THIS batch only.
217
+ # @return [Hash{String => Result, Error}] in the order the addresses were given
218
+ def lookup_batch(ips, concurrency: nil, retries: nil, timeout: nil)
219
+ limit = concurrency || @concurrency
220
+ unless concurrency?(limit)
221
+ raise Error.new(:bad_request, "concurrency must be an Integer from 1, not #{limit.inspect}")
222
+ end
223
+ Transport.checked_timeout(timeout) unless timeout.nil?
224
+ Retries.checked_count(retries) unless retries.nil?
225
+
226
+ # An IPv4-mapped address is sent as the address it carries, once however
227
+ # many of its spellings were asked, and answered under each one asked.
228
+ asked = ips.to_a.uniq
229
+ addresses = asked.map { |ip| Bogon.unmapped(ip) }.uniq
230
+ answers = {}
231
+ pending = []
232
+
233
+ addresses.each do |ip|
234
+ hit = Bogon.bogon?(ip) ? Bogon.result(ip) : @cache&.get(ip)
235
+ hit.nil? ? pending << ip : answers[ip] = hit
236
+ end
237
+ # Boarded before any chunk is built, so a lookup arriving meanwhile waits
238
+ # for this batch; an address already in flight is not sent again.
239
+ boarded = {}
240
+ joined = {}
241
+ pending.each do |ip|
242
+ flight, leader = board(ip)
243
+ if leader == false
244
+ joined[ip] = flight
245
+ else
246
+ boarded[ip] = flight
247
+ end
248
+ end
249
+ begin
250
+ unless boarded.empty?
251
+ chunks = boarded.keys.each_slice(BATCH_MAX).to_a
252
+ run_batch(chunks, answers, limit, retries || @retries, timeout, boarded)
253
+ end
254
+ ensure
255
+ boarded.each { |ip, flight| unboard(ip, flight, :abandoned) unless flight.nil? }
256
+ end
257
+ joined.each { |ip, flight| answers[ip] = joined_answer(ip, flight, retries, timeout) }
258
+
259
+ # Reinstated in input order: a hydra settles in completion order, and a
260
+ # caller iterating the hash should see what they passed in.
261
+ asked.to_h { |ip| [ip, answers[Bogon.unmapped(ip)]] }
262
+ end
263
+
264
+ private
265
+
266
+ # Whether `value` can be a batch's concurrency: chunks in flight are counted
267
+ # whole, and a hydra allowed fewer than one runs nothing.
268
+ def concurrency?(value)
269
+ value.is_a?(Integer) && value >= 1
270
+ end
271
+
272
+ # Concurrent misses for one address share ONE request. The first miss boards
273
+ # the address and every miss after it, a batch's included, waits on its
274
+ # Flight.
275
+ class Flight
276
+ def initialize
277
+ @lock = Mutex.new
278
+ @landed = ConditionVariable.new
279
+ @state = :pending
280
+ end
281
+
282
+ # Settles the flight once, for every waiter: :served with a Result,
283
+ # :failed with an Error, or :abandoned when its leader never finished.
284
+ def land(state, value = nil)
285
+ @lock.synchronize do
286
+ next unless @state == :pending
287
+
288
+ @state = state
289
+ @value = value
290
+ @landed.broadcast
291
+ end
292
+ end
293
+
294
+ # Blocks until the flight lands, then [state, value].
295
+ def wait
296
+ @lock.synchronize do
297
+ @landed.wait(@lock) while @state == :pending
298
+ [@state, @value]
299
+ end
300
+ end
301
+ end
302
+
303
+ # [flight, true] when this caller leads the request for `ip`, [flight,
304
+ # false] when one is already in flight, and nil without a cache: every
305
+ # lookup is then served, so nothing is shared.
306
+ def board(ip)
307
+ return nil if @cache.nil?
308
+
309
+ @flights_lock.synchronize do
310
+ existing = @flights[ip]
311
+ existing ? [existing, false] : [@flights[ip] = Flight.new, true]
312
+ end
313
+ end
314
+
315
+ # Takes `ip` off the board and lands its flight. Returns `value`.
316
+ def unboard(ip, flight, state, value = nil)
317
+ @flights_lock.synchronize { @flights.delete(ip) if @flights[ip].equal?(flight) }
318
+ flight.land(state, value)
319
+ value
320
+ end
321
+
322
+ # Runs the request a flight is waiting on, under the options of the call
323
+ # that leads it. A served answer is cached before it lands; a failure
324
+ # reaches every waiter and is cached for none; and a leader that never
325
+ # finishes - a killed thread, a Timeout.timeout - abandons the flight, so
326
+ # its waiters ask again rather than failing with it.
327
+ def lead(ip, flight)
328
+ unboard(ip, flight, :served, yield)
329
+ rescue Error => e
330
+ unboard(ip, flight, :failed, e)
331
+ raise
332
+ ensure
333
+ unboard(ip, flight, :abandoned)
334
+ end
335
+
336
+ def fetch(ip, retries, timeout)
337
+ result = Retries.with_retries(retries || @retries) do
338
+ Transport.lookup_result(@transport.lookup_request(ip, timeout: timeout).run)
339
+ end
340
+ @cache&.set(ip, result)
341
+ result
342
+ end
343
+
344
+ # One hydra per call, sized for THIS call. Reusing an instance-level hydra
345
+ # would silently cap a per-call concurrency at the client's setting, and
346
+ # would not be safe to drive from two threads either. Each request is one
347
+ # chunk of up to 1000 addresses.
348
+ def run_batch(chunks, answers, concurrency, retries, timeout, boarded)
349
+ hydra = Typhoeus::Hydra.new(max_concurrency: concurrency)
350
+ attempts = Hash.new(0)
351
+
352
+ enqueue = lambda do |chunk|
353
+ request = @transport.batch_request(chunk, timeout: timeout)
354
+ request.on_complete do |response|
355
+ outcome = settle(chunk, response, attempts, retries, enqueue)
356
+ outcome&.each do |ip, answer|
357
+ answers[ip] = answer
358
+ flight = boarded[ip]
359
+ unboard(ip, flight, answer.is_a?(Result) ? :served : :failed, answer) unless flight.nil?
360
+ end
361
+ end
362
+ hydra.queue(request)
363
+ end
364
+
365
+ chunks.each { |chunk| enqueue.call(chunk) }
366
+ hydra.run
367
+ end
368
+
369
+ # One POST /batch, mapped back onto the addresses it was asked about. A
370
+ # chunk-level failure - the call refused, the transport failing, the retries
371
+ # exhausted - becomes every address's error, exactly as it would have been
372
+ # had each been looked up alone.
373
+ def settle(chunk, response, attempts, retries, enqueue)
374
+ body = Transport.batch_body(response)
375
+ chunk.to_h do |ip|
376
+ answer = batch_answer(ip, body)
377
+ @cache&.set(ip, answer) if answer.is_a?(Result)
378
+ [ip, answer]
379
+ end
380
+ rescue Error => e
381
+ return chunk.to_h { |ip| [ip, e] } unless e.retryable? && attempts[chunk] < retries
382
+
383
+ attempts[chunk] += 1
384
+ # Sleeping here stalls the whole hydra, which is what a server-supplied
385
+ # delay asks for: it is telling every request to this host to back off.
386
+ sleep(Retries.delay_for(e, attempts[chunk]))
387
+ enqueue.call(chunk)
388
+ nil
389
+ end
390
+
391
+ # What a batch takes for an address another call was fetching: that call's
392
+ # answer or error, or, when its leader never finished, its own lookup.
393
+ def joined_answer(ip, flight, retries, timeout)
394
+ state, value = flight.wait
395
+ return value unless state == :abandoned
396
+
397
+ lookup(ip, retries: retries, timeout: timeout)
398
+ rescue Error => e
399
+ e
400
+ end
401
+
402
+ # Every address lands in exactly one of `results` and `errors`; an address in
403
+ # neither, or with an entry that is not an answer, is the server breaking its
404
+ # own contract, and is reported as that address's error rather than lost.
405
+ def batch_answer(ip, body)
406
+ served = body['results'][ip]
407
+ failed = body['errors'][ip]
408
+ if Transport.conforms?(served, 'LookupResponse')
409
+ Result.new(served)
410
+ elsif Transport.conforms?(failed, 'BatchLookupError')
411
+ Error.from_entry(failed['status'], failed['error'])
412
+ else
413
+ Error.new(:server_error, "the batch answer carried no readable entry for #{ip}", status: 200)
414
+ end
415
+ end
44
416
  end
45
417
  end
@@ -1,9 +1,9 @@
1
1
  =begin
2
2
  #InternetData API
3
3
 
4
- #The InternetData API: download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
4
+ #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.08
6
+ The version of the OpenAPI document: 2026.11.13
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -272,6 +272,27 @@ module InternetData
272
272
  key: 'apikey',
273
273
  value: api_key_with_prefix('apikey')
274
274
  },
275
+ 'bearerAuth' =>
276
+ {
277
+ type: 'bearer',
278
+ in: 'header',
279
+ key: 'Authorization',
280
+ value: "Bearer #{access_token_with_refresh}"
281
+ },
282
+ 'apiKeyHeader' =>
283
+ {
284
+ type: 'api_key',
285
+ in: 'header',
286
+ key: 'X-Api-Key',
287
+ value: api_key_with_prefix('X-Api-Key')
288
+ },
289
+ 'apiKeyQuery' =>
290
+ {
291
+ type: 'api_key',
292
+ in: 'query',
293
+ key: 'apikey',
294
+ value: api_key_with_prefix('apikey')
295
+ },
275
296
  }
276
297
  end
277
298
 
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InternetData
4
+ # What a lookup of a domain answers.
5
+ #
6
+ # As with {Result}, an absent member is one your plan does not include, and
7
+ # reads as `nil`, never as false: Unlimited answers whether the domain is
8
+ # registered and which record types it has, and paid plans and the trial add
9
+ # the records themselves, its organization's IP ranges and Disposable Domain.
10
+ # Each flag has the three readers {Result}'s have: `is_disposable`,
11
+ # `disposable?` and `included?(:is_disposable)`.
12
+ class DomainResult
13
+ include Answer
14
+
15
+ # The domain, lowercase and in punycode.
16
+ def domain
17
+ @raw['domain']
18
+ end
19
+
20
+ # Whether it is a registered domain (Domain List). Every answer.
21
+ def is_registered
22
+ @raw['is_registered']
23
+ end
24
+
25
+ # `is_registered` with an absent member read as false.
26
+ def registered?
27
+ @raw['is_registered'] == true
28
+ end
29
+
30
+ # Whether it is a disposable email domain (Disposable Domain), or nil when
31
+ # your plan does not include it. Paid plans and the trial.
32
+ def is_disposable
33
+ @raw['is_disposable']
34
+ end
35
+
36
+ # `is_disposable` with an absent member read as false.
37
+ def disposable?
38
+ @raw['is_disposable'] == true
39
+ end
40
+
41
+ # Whether it has A records. Every answer.
42
+ def has_a
43
+ @raw['has_a']
44
+ end
45
+
46
+ # `has_a` with an absent member read as false.
47
+ def has_a?
48
+ @raw['has_a'] == true
49
+ end
50
+
51
+ # Whether it has AAAA records. Every answer.
52
+ def has_aaaa
53
+ @raw['has_aaaa']
54
+ end
55
+
56
+ # `has_aaaa` with an absent member read as false.
57
+ def has_aaaa?
58
+ @raw['has_aaaa'] == true
59
+ end
60
+
61
+ # Whether it has MX records. Every answer.
62
+ def has_mx
63
+ @raw['has_mx']
64
+ end
65
+
66
+ # `has_mx` with an absent member read as false.
67
+ def has_mx?
68
+ @raw['has_mx'] == true
69
+ end
70
+
71
+ # Whether it has NS records. Every answer.
72
+ def has_ns
73
+ @raw['has_ns']
74
+ end
75
+
76
+ # `has_ns` with an absent member read as false.
77
+ def has_ns?
78
+ @raw['has_ns'] == true
79
+ end
80
+
81
+ # Whether it has a CNAME record. Every answer.
82
+ def has_cname
83
+ @raw['has_cname']
84
+ end
85
+
86
+ # `has_cname` with an absent member read as false.
87
+ def has_cname?
88
+ @raw['has_cname'] == true
89
+ end
90
+
91
+ # Its records in Domain DNS, up to 100 a type, as a wire hash
92
+ # (`result.dns['mx']`). Paid plans and the trial.
93
+ def dns
94
+ @raw['dns']
95
+ end
96
+
97
+ # Its organization's IP ranges, from Domain IP Ranges. Paid plans and the
98
+ # trial.
99
+ def ip_ranges
100
+ @raw['ip_ranges']
101
+ end
102
+
103
+ # Detail for `is_disposable`, `{}` when it is false. Paid plans and the
104
+ # trial.
105
+ def disposable
106
+ @raw['disposable']
107
+ end
108
+
109
+ # On answers to a key on the free plan only: false while its organization's
110
+ # daily trial lasts, true once the answer has Unlimited's; nil on any other.
111
+ def trial_expired
112
+ @raw['trial_expired']
113
+ end
114
+
115
+ # `trial_expired` with an absent member read as false.
116
+ def trial_expired?
117
+ @raw['trial_expired'] == true
118
+ end
119
+
120
+ def inspect
121
+ "#<#{self.class} domain=#{domain.inspect} is_registered=#{is_registered.inspect}>"
122
+ end
123
+ end
124
+ end
@@ -21,6 +21,8 @@ module InternetData
21
21
  # The API's machine-readable result code, when the refusal carried one:
22
22
  # `NOT_LICENSED`, `UNKNOWN_DATASET`, `NOT_AVAILABLE` and so on. Deliberately
23
23
  # not an enum, so a code added later stays readable by a client built today.
24
+ # A database call's refusal carries one; a lookup's carries none, and its
25
+ # message is the API's own words.
24
26
  attr_reader :kind, :status, :rc, :retry_after_seconds
25
27
 
26
28
  def initialize(kind, message, status: nil, rc: nil, retry_after_seconds: nil)
@@ -41,7 +43,7 @@ module InternetData
41
43
  # from it is classified on the status alone.
42
44
  def self.from_status(status, headers, body, message: nil)
43
45
  rc = rc_of(body)
44
- message ||= rc || "request failed with status #{status}"
46
+ message ||= message_of(body) || "request failed with status #{status}"
45
47
  retry_after = parse_retry_after(header(headers, 'retry-after'))
46
48
  kind = kind_for(status, headers)
47
49
 
@@ -73,6 +75,13 @@ module InternetData
73
75
  new(:network, message)
74
76
  end
75
77
 
78
+ # A per-entry failure inside a successful batch: the status the single lookup
79
+ # would have answered, and its message, with no headers at all - so a 429
80
+ # here is a spent allowance, which is the only kind the API puts in an entry.
81
+ def self.from_entry(status, message)
82
+ from_status(status.to_i, {}, nil, message: message.to_s)
83
+ end
84
+
76
85
  # Case-insensitive, and works with a plain Hash as well as with the
77
86
  # case-blind header object Typhoeus builds from a live response.
78
87
  def self.header(headers, name)
@@ -83,16 +92,30 @@ module InternetData
83
92
  value.is_a?(Array) ? value.last : value
84
93
  end
85
94
 
86
- # Every refusal this API writes is a `{"rc": "..."}` envelope. An
87
- # intermediary's HTML error page parses as neither JSON nor that, and leaves
88
- # the status to speak for itself.
95
+ # The two APIs behind this host answer with different envelopes: the lookup
96
+ # endpoints use `error`, the database endpoints `rc`. Both are read here so a
97
+ # caller never has to know which one they hit. An intermediary's HTML error
98
+ # page parses as neither and leaves the status to speak for itself.
99
+ def self.message_of(body)
100
+ parsed = parse_body(body)
101
+ return nil unless parsed.is_a?(Hash)
102
+
103
+ message = parsed['error'] || parsed['rc']
104
+ message.is_a?(String) && !message.empty? ? message : nil
105
+ end
106
+
107
+ # A database refusal's `{"rc": "..."}` code; a lookup refusal carries none.
89
108
  def self.rc_of(body)
90
- parsed = body.is_a?(String) ? (JSON.parse(body) rescue nil) : body
109
+ parsed = parse_body(body)
91
110
  return nil unless parsed.is_a?(Hash)
92
111
 
93
112
  parsed['rc'].is_a?(String) && !parsed['rc'].empty? ? parsed['rc'] : nil
94
113
  end
95
114
 
115
+ def self.parse_body(body)
116
+ body.is_a?(String) ? (JSON.parse(body) rescue nil) : body
117
+ end
118
+
96
119
  # Seconds as digits, or an HTTP date, and nothing else: `Float()` would also
97
120
  # read `0x10`, `1_0` and `1e3` as a wait.
98
121
  def self.parse_retry_after(value)
@@ -109,7 +132,7 @@ module InternetData
109
132
  [0, (when_at - Time.now).ceil].max
110
133
  end
111
134
 
112
- private_class_method :header, :rc_of, :parse_retry_after
135
+ private_class_method :header, :message_of, :rc_of, :parse_body, :parse_retry_after
113
136
  end
114
137
 
115
138
  # The authorization server refusing an OAuth request: a 4xx whose body names an