internetdata 1.0.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +125 -0
- data/lib/internetdata/api/database_v2_api.rb +376 -0
- data/lib/internetdata/api_client.rb +397 -0
- data/lib/internetdata/api_error.rb +58 -0
- data/lib/internetdata/api_model_base.rb +88 -0
- data/lib/internetdata/client.rb +34 -0
- data/lib/internetdata/configuration.rb +315 -0
- data/lib/internetdata/database_api.rb +182 -0
- data/lib/internetdata/errors.rb +110 -0
- data/lib/internetdata/models/database.rb +349 -0
- data/lib/internetdata/models/database_checksums_response.rb +240 -0
- data/lib/internetdata/models/database_list.rb +166 -0
- data/lib/internetdata/models/database_metadata.rb +299 -0
- data/lib/internetdata/models/database_metadata_column.rb +199 -0
- data/lib/internetdata/models/database_version.rb +258 -0
- data/lib/internetdata/models/db_checksums.rb +246 -0
- data/lib/internetdata/models/download.rb +329 -0
- data/lib/internetdata/models/download_list.rb +166 -0
- data/lib/internetdata/models/error_envelope.rb +165 -0
- data/lib/internetdata/retries.rb +33 -0
- data/lib/internetdata/transport.rb +72 -0
- data/lib/internetdata/version.rb +5 -0
- data/lib/internetdata.rb +26 -0
- metadata +84 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
#InternetData API
|
|
3
|
+
|
|
4
|
+
#The InternetData API. Please see https://docs.internetdata.io/api for more details.
|
|
5
|
+
|
|
6
|
+
The version of the OpenAPI document: 2026.09.04
|
|
7
|
+
Contact: dev@internetdata.io
|
|
8
|
+
Generated by: https://openapi-generator.tech
|
|
9
|
+
Generator version: 7.25.0
|
|
10
|
+
|
|
11
|
+
=end
|
|
12
|
+
|
|
13
|
+
module InternetData
|
|
14
|
+
class Configuration
|
|
15
|
+
# Defines url scheme
|
|
16
|
+
attr_accessor :scheme
|
|
17
|
+
|
|
18
|
+
# Defines url host
|
|
19
|
+
attr_accessor :host
|
|
20
|
+
|
|
21
|
+
# Defines url base path
|
|
22
|
+
attr_accessor :base_path
|
|
23
|
+
|
|
24
|
+
# Define server configuration index
|
|
25
|
+
attr_accessor :server_index
|
|
26
|
+
|
|
27
|
+
# Define server operation configuration index
|
|
28
|
+
attr_accessor :server_operation_index
|
|
29
|
+
|
|
30
|
+
# Default server variables
|
|
31
|
+
attr_accessor :server_variables
|
|
32
|
+
|
|
33
|
+
# Default server operation variables
|
|
34
|
+
attr_accessor :server_operation_variables
|
|
35
|
+
|
|
36
|
+
# Defines API keys used with API Key authentications.
|
|
37
|
+
#
|
|
38
|
+
# @return [Hash] key: parameter name, value: parameter value (API key)
|
|
39
|
+
#
|
|
40
|
+
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
|
41
|
+
# config.api_key['api_key'] = 'xxx'
|
|
42
|
+
attr_accessor :api_key
|
|
43
|
+
|
|
44
|
+
# Defines API key prefixes used with API Key authentications.
|
|
45
|
+
#
|
|
46
|
+
# @return [Hash] key: parameter name, value: API key prefix
|
|
47
|
+
#
|
|
48
|
+
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
|
49
|
+
# config.api_key_prefix['api_key'] = 'Token'
|
|
50
|
+
attr_accessor :api_key_prefix
|
|
51
|
+
|
|
52
|
+
# Defines the username used with HTTP basic authentication.
|
|
53
|
+
#
|
|
54
|
+
# @return [String]
|
|
55
|
+
attr_accessor :username
|
|
56
|
+
|
|
57
|
+
# Defines the password used with HTTP basic authentication.
|
|
58
|
+
#
|
|
59
|
+
# @return [String]
|
|
60
|
+
attr_accessor :password
|
|
61
|
+
|
|
62
|
+
# Defines the access token (Bearer) used with OAuth2.
|
|
63
|
+
attr_accessor :access_token
|
|
64
|
+
|
|
65
|
+
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
|
|
66
|
+
# Overrides the access_token if set
|
|
67
|
+
# @return [Proc]
|
|
68
|
+
attr_accessor :access_token_getter
|
|
69
|
+
|
|
70
|
+
# Set this to return data as binary instead of downloading a temp file. When enabled (set to true)
|
|
71
|
+
# HTTP responses with return type `File` will be returned as a stream of binary data.
|
|
72
|
+
# Default to false.
|
|
73
|
+
attr_accessor :return_binary_data
|
|
74
|
+
|
|
75
|
+
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
|
76
|
+
# details will be logged with `logger.debug` (see the `logger` attribute).
|
|
77
|
+
# Default to false.
|
|
78
|
+
#
|
|
79
|
+
# @return [true, false]
|
|
80
|
+
attr_accessor :debugging
|
|
81
|
+
|
|
82
|
+
# Set this to ignore operation servers for the API client. This is useful when you need to
|
|
83
|
+
# send requests to a different server than the one specified in the OpenAPI document.
|
|
84
|
+
# Will default to the base url defined in the spec but can be overridden by setting
|
|
85
|
+
# `scheme`, `host`, `base_path` directly.
|
|
86
|
+
# Default to false.
|
|
87
|
+
# @return [true, false]
|
|
88
|
+
attr_accessor :ignore_operation_servers
|
|
89
|
+
|
|
90
|
+
# Defines the logger used for debugging.
|
|
91
|
+
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
|
92
|
+
#
|
|
93
|
+
# @return [#debug]
|
|
94
|
+
attr_accessor :logger
|
|
95
|
+
|
|
96
|
+
# Defines the temporary folder to store downloaded files
|
|
97
|
+
# (for API endpoints that have file response).
|
|
98
|
+
# Default to use `Tempfile`.
|
|
99
|
+
#
|
|
100
|
+
# @return [String]
|
|
101
|
+
attr_accessor :temp_folder_path
|
|
102
|
+
|
|
103
|
+
# The time limit for HTTP request in seconds.
|
|
104
|
+
# Default to 0 (never times out).
|
|
105
|
+
attr_accessor :timeout
|
|
106
|
+
|
|
107
|
+
# Set this to false to skip client side validation in the operation.
|
|
108
|
+
# Default to true.
|
|
109
|
+
# @return [true, false]
|
|
110
|
+
attr_accessor :client_side_validation
|
|
111
|
+
|
|
112
|
+
### TLS/SSL setting
|
|
113
|
+
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
|
114
|
+
# Default to true.
|
|
115
|
+
#
|
|
116
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
|
117
|
+
#
|
|
118
|
+
# @return [true, false]
|
|
119
|
+
attr_accessor :verify_ssl
|
|
120
|
+
|
|
121
|
+
### TLS/SSL setting
|
|
122
|
+
# Set this to false to skip verifying SSL host name
|
|
123
|
+
# Default to true.
|
|
124
|
+
#
|
|
125
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
|
126
|
+
#
|
|
127
|
+
# @return [true, false]
|
|
128
|
+
attr_accessor :verify_ssl_host
|
|
129
|
+
|
|
130
|
+
### TLS/SSL setting
|
|
131
|
+
# Set this to customize the certificate file to verify the peer.
|
|
132
|
+
#
|
|
133
|
+
# @return [String] the path to the certificate file
|
|
134
|
+
#
|
|
135
|
+
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
|
136
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
|
137
|
+
attr_accessor :ssl_ca_cert
|
|
138
|
+
|
|
139
|
+
### TLS/SSL setting
|
|
140
|
+
# Client certificate file (for client certificate)
|
|
141
|
+
attr_accessor :cert_file
|
|
142
|
+
|
|
143
|
+
### TLS/SSL setting
|
|
144
|
+
# Client private key file (for client certificate)
|
|
145
|
+
attr_accessor :key_file
|
|
146
|
+
|
|
147
|
+
# Set this to customize parameters encoding of array parameter with multi collectionFormat.
|
|
148
|
+
# Default to nil.
|
|
149
|
+
#
|
|
150
|
+
# @see The params_encoding option of Ethon. Related source code:
|
|
151
|
+
# https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96
|
|
152
|
+
attr_accessor :params_encoding
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
attr_accessor :inject_format
|
|
156
|
+
|
|
157
|
+
attr_accessor :force_ending_format
|
|
158
|
+
|
|
159
|
+
def initialize
|
|
160
|
+
@scheme = 'https'
|
|
161
|
+
@host = 'internetdata.io'
|
|
162
|
+
@base_path = ''
|
|
163
|
+
@server_index = nil
|
|
164
|
+
@server_operation_index = {}
|
|
165
|
+
@server_variables = {}
|
|
166
|
+
@server_operation_variables = {}
|
|
167
|
+
@api_key = {}
|
|
168
|
+
@api_key_prefix = {}
|
|
169
|
+
@client_side_validation = true
|
|
170
|
+
@verify_ssl = true
|
|
171
|
+
@verify_ssl_host = true
|
|
172
|
+
@cert_file = nil
|
|
173
|
+
@key_file = nil
|
|
174
|
+
@timeout = 0
|
|
175
|
+
@params_encoding = nil
|
|
176
|
+
@debugging = false
|
|
177
|
+
@ignore_operation_servers = false
|
|
178
|
+
@inject_format = false
|
|
179
|
+
@force_ending_format = false
|
|
180
|
+
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
|
181
|
+
|
|
182
|
+
yield(self) if block_given?
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The default Configuration object.
|
|
186
|
+
def self.default
|
|
187
|
+
@@default ||= Configuration.new
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def configure
|
|
191
|
+
yield(self) if block_given?
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def scheme=(scheme)
|
|
195
|
+
# remove :// from scheme
|
|
196
|
+
@scheme = scheme.sub(/:\/\//, '')
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def host=(host)
|
|
200
|
+
# remove http(s):// and anything after a slash
|
|
201
|
+
@host = host.sub(/https?:\/\//, '').split('/').first
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def base_path=(base_path)
|
|
205
|
+
# Add leading and trailing slashes to base_path
|
|
206
|
+
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
|
207
|
+
@base_path = '' if @base_path == '/'
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Returns base URL for specified operation based on server settings
|
|
211
|
+
def base_url(operation = nil)
|
|
212
|
+
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
|
|
213
|
+
if operation_server_settings.key?(operation) then
|
|
214
|
+
index = server_operation_index.fetch(operation, server_index)
|
|
215
|
+
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
|
|
216
|
+
else
|
|
217
|
+
server_index.nil? ? "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') : server_url(server_index, server_variables, nil)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Gets API key (with prefix if set).
|
|
222
|
+
# @param [String] param_name the parameter name of API key auth
|
|
223
|
+
def api_key_with_prefix(param_name, param_alias = nil)
|
|
224
|
+
key = @api_key[param_name]
|
|
225
|
+
key = @api_key.fetch(param_alias, key) unless param_alias.nil?
|
|
226
|
+
if @api_key_prefix[param_name]
|
|
227
|
+
"#{@api_key_prefix[param_name]} #{key}"
|
|
228
|
+
else
|
|
229
|
+
key
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Gets access_token using access_token_getter or uses the static access_token
|
|
234
|
+
def access_token_with_refresh
|
|
235
|
+
return access_token if access_token_getter.nil?
|
|
236
|
+
access_token_getter.call
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Gets Basic Auth token string
|
|
240
|
+
def basic_auth_token
|
|
241
|
+
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Returns Auth Settings hash for api client.
|
|
245
|
+
def auth_settings
|
|
246
|
+
{
|
|
247
|
+
'ApiKeyAuth' =>
|
|
248
|
+
{
|
|
249
|
+
type: 'api_key',
|
|
250
|
+
in: 'query',
|
|
251
|
+
key: 'apikey',
|
|
252
|
+
value: api_key_with_prefix('apikey')
|
|
253
|
+
},
|
|
254
|
+
'BearerAuth' =>
|
|
255
|
+
{
|
|
256
|
+
type: 'bearer',
|
|
257
|
+
in: 'header',
|
|
258
|
+
key: 'Authorization',
|
|
259
|
+
value: "Bearer #{access_token_with_refresh}"
|
|
260
|
+
},
|
|
261
|
+
}
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Returns an array of Server setting
|
|
265
|
+
def server_settings
|
|
266
|
+
[
|
|
267
|
+
{
|
|
268
|
+
url: "https://internetdata.io",
|
|
269
|
+
description: "No description provided",
|
|
270
|
+
}
|
|
271
|
+
]
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def operation_server_settings
|
|
275
|
+
{
|
|
276
|
+
}
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Returns URL based on server settings
|
|
280
|
+
#
|
|
281
|
+
# @param index array index of the server settings
|
|
282
|
+
# @param variables hash of variable and the corresponding value
|
|
283
|
+
def server_url(index, variables = {}, servers = nil)
|
|
284
|
+
servers = server_settings if servers == nil
|
|
285
|
+
|
|
286
|
+
# check array index out of bound
|
|
287
|
+
if (index.nil? || index < 0 || index >= servers.size)
|
|
288
|
+
fail ArgumentError, "Invalid index #{index} when selecting the server. Must not be nil and must be less than #{servers.size}"
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
server = servers[index]
|
|
292
|
+
url = server[:url]
|
|
293
|
+
|
|
294
|
+
return url unless server.key? :variables
|
|
295
|
+
|
|
296
|
+
# go through variable and assign a value
|
|
297
|
+
server[:variables].each do |name, variable|
|
|
298
|
+
if variables.key?(name)
|
|
299
|
+
if (!server[:variables][name].key?(:enum_values) || server[:variables][name][:enum_values].include?(variables[name]))
|
|
300
|
+
url.gsub! "{" + name.to_s + "}", variables[name]
|
|
301
|
+
else
|
|
302
|
+
fail ArgumentError, "The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}."
|
|
303
|
+
end
|
|
304
|
+
else
|
|
305
|
+
# use default value
|
|
306
|
+
url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value]
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
url
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
end
|
|
315
|
+
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module InternetData
|
|
4
|
+
# The licensed database downloads, reached as `client.database`.
|
|
5
|
+
#
|
|
6
|
+
# Not to be confused with {Database}, which is one entry in a {#list}, nor with
|
|
7
|
+
# the generated {DatabaseV2Api} underneath, which speaks the wire.
|
|
8
|
+
class DatabaseApi
|
|
9
|
+
def initialize(transport, retries:)
|
|
10
|
+
@transport = transport
|
|
11
|
+
@api = DatabaseV2Api.new(transport)
|
|
12
|
+
@retries = retries
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# The database FAMILIES your organization may see, with its license beside
|
|
16
|
+
# each one.
|
|
17
|
+
#
|
|
18
|
+
# A license is held against the family while a download names one version, so
|
|
19
|
+
# the ids {#download}, {#download_bytes}, {#download_url}, {#checksums} and
|
|
20
|
+
# {#metadata} take come from each family's `versions`, not from the family.
|
|
21
|
+
#
|
|
22
|
+
# `standing` is the discovery surface for the PUBLISHED catalog only. A
|
|
23
|
+
# database commissioned for a single customer is ABSENT from this listing for
|
|
24
|
+
# everyone else, rather than present with a standing of `unlicensed`, and the
|
|
25
|
+
# server decides that per key. So the catalog is not the same for every key,
|
|
26
|
+
# a listing fetched with one key says nothing about another, and there is no
|
|
27
|
+
# other source to reconstruct it from.
|
|
28
|
+
def list
|
|
29
|
+
call { @api.list_databases.databases }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# What is inside one database: schema, sample rows, row count and sizes.
|
|
33
|
+
#
|
|
34
|
+
# `updated` and `entries` answer whether today's build is worth fetching, and
|
|
35
|
+
# `size` is bytes per format, which is what a transfer should be budgeted
|
|
36
|
+
# against before it starts.
|
|
37
|
+
def metadata(id)
|
|
38
|
+
call { @api.database_metadata_v2(id) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The digests for one published file.
|
|
42
|
+
#
|
|
43
|
+
# Returns the whole set rather than one algorithm: which digests a database
|
|
44
|
+
# publishes is the API's choice, not ours, and the response nests them one
|
|
45
|
+
# level down under `checksums`.
|
|
46
|
+
def checksums(id, format)
|
|
47
|
+
call { @api.database_checksum_v2(id, format).checksums }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Your organization's recent download attempts, newest first, refusals
|
|
51
|
+
# included.
|
|
52
|
+
def downloads(limit: nil)
|
|
53
|
+
call { @api.list_downloads(limit.nil? ? {} : { limit: limit }).downloads }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The time-limited URL for one database file.
|
|
57
|
+
#
|
|
58
|
+
# Presigned, so it carries no credential of yours and can be handed to
|
|
59
|
+
# anything that speaks HTTP. The URL is returned rather than the bytes so the
|
|
60
|
+
# caller decides how to transfer a file that routinely runs to gigabytes; the
|
|
61
|
+
# link authorizes the START of a transfer, so one already running is not
|
|
62
|
+
# interrupted when it lapses.
|
|
63
|
+
def download_url(id, format)
|
|
64
|
+
call { redirect_location(id, format) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Download one database file to `path`, and return the bytes written.
|
|
68
|
+
#
|
|
69
|
+
# The bytes land in a neighboring `.part` file that is renamed on completion,
|
|
70
|
+
# so a transfer that dies half way leaves no truncated file that reads as a
|
|
71
|
+
# whole database, and a refresh that fails does not destroy the copy already
|
|
72
|
+
# there. Nothing beyond one chunk is ever held in memory, whatever the
|
|
73
|
+
# database weighs.
|
|
74
|
+
def download(id, format, path)
|
|
75
|
+
partial = "#{path}.part"
|
|
76
|
+
begin
|
|
77
|
+
url = download_url(id, format)
|
|
78
|
+
written = Retries.with_retries(@retries) do
|
|
79
|
+
# Reopened per attempt, so a retry restarts the file rather than
|
|
80
|
+
# appending a second copy of the body to a half-written one.
|
|
81
|
+
File.open(partial, 'wb') { |file| stream(url) { |chunk| file.write(chunk) } }
|
|
82
|
+
end
|
|
83
|
+
File.rename(partial, path)
|
|
84
|
+
rescue StandardError
|
|
85
|
+
File.delete(partial) if File.exist?(partial)
|
|
86
|
+
raise
|
|
87
|
+
end
|
|
88
|
+
written
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Download one database file and hand back its bytes.
|
|
92
|
+
#
|
|
93
|
+
# **This holds the entire file in memory**, and the catalog spans seven orders
|
|
94
|
+
# of magnitude: `bogon_asn_v1` is a few hundred bytes while the largest
|
|
95
|
+
# published database is over 5 GiB. Reach for it at the small end, where the
|
|
96
|
+
# bytes go straight into a parser, and use {#download} for anything you have
|
|
97
|
+
# not measured with {#metadata}.
|
|
98
|
+
def download_bytes(id, format)
|
|
99
|
+
url = download_url(id, format)
|
|
100
|
+
Retries.with_retries(@retries) do
|
|
101
|
+
bytes = String.new(encoding: Encoding::BINARY)
|
|
102
|
+
stream(url) { |chunk| bytes << chunk }
|
|
103
|
+
bytes
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
# Runs one transfer of a presigned link, handing each chunk to the block, and
|
|
110
|
+
# returns the bytes that reached it.
|
|
111
|
+
#
|
|
112
|
+
# Typhoeus only streams when a request carries an `on_body` callback: with
|
|
113
|
+
# one set, Ethon's write callback passes the chunk on INSTEAD of appending it
|
|
114
|
+
# to `response.body`, so the ceiling on a transfer of any size is one chunk.
|
|
115
|
+
# The callback must therefore never answer `:unyielded`, which is the value
|
|
116
|
+
# that means "nobody took this" and puts the chunk back in the buffer.
|
|
117
|
+
def stream(url)
|
|
118
|
+
written = 0
|
|
119
|
+
served = nil
|
|
120
|
+
request = @transport.storage_request(url)
|
|
121
|
+
request.on_headers { |response| served = response.code }
|
|
122
|
+
request.on_body do |chunk, _response|
|
|
123
|
+
# An error page has no bounded size, so a refusal is aborted here rather
|
|
124
|
+
# than read and then classified.
|
|
125
|
+
next :abort unless served == 200
|
|
126
|
+
|
|
127
|
+
yield chunk
|
|
128
|
+
written += chunk.bytesize
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
settle(request.run, written)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def settle(response, written)
|
|
135
|
+
status = response.code.to_i
|
|
136
|
+
# No status at all means the transfer never reached HTTP: DNS, connect,
|
|
137
|
+
# TLS or a timeout, and curl's own reason is the only thing that says
|
|
138
|
+
# which. It is also what a :partial_file arrives as once a status IS in
|
|
139
|
+
# hand, which is where a transfer that died mid-flight fails rather than
|
|
140
|
+
# leaving a short file that reads as a whole database. The declared-length
|
|
141
|
+
# check other bindings hand-roll is curl's job here.
|
|
142
|
+
raise Error.from_transport(response) if status.zero?
|
|
143
|
+
|
|
144
|
+
unless status == 200
|
|
145
|
+
raise Error.from_status(status, response.headers, nil,
|
|
146
|
+
message: "object storage refused the download link with status #{status}")
|
|
147
|
+
end
|
|
148
|
+
raise Error.from_transport(response) unless response.success?
|
|
149
|
+
|
|
150
|
+
written
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The 302 is this operation's SUCCESS case, but the generated client treats
|
|
154
|
+
# every non-2xx as a failure, so it arrives as an ApiError carrying the
|
|
155
|
+
# Location header.
|
|
156
|
+
def redirect_location(id, format)
|
|
157
|
+
@api.download_database_v2(id, format)
|
|
158
|
+
raise Error.new(:server_error, 'expected a redirect to object storage')
|
|
159
|
+
rescue ApiError => e
|
|
160
|
+
raise unless e.code == 302
|
|
161
|
+
|
|
162
|
+
location = e.response_headers && e.response_headers['Location']
|
|
163
|
+
raise Error.new(:server_error, 'the redirect carried no Location header', status: 302) if location.nil?
|
|
164
|
+
|
|
165
|
+
location.is_a?(Array) ? location.last : location
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def call(&block)
|
|
169
|
+
Retries.with_retries(@retries) do
|
|
170
|
+
block.call
|
|
171
|
+
rescue ApiError => e
|
|
172
|
+
raise error_for(e)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def error_for(api_error)
|
|
177
|
+
return Error.new(:network, api_error.message) if api_error.code.to_i.zero?
|
|
178
|
+
|
|
179
|
+
Error.from_status(api_error.code, api_error.response_headers, api_error.response_body)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'time'
|
|
5
|
+
|
|
6
|
+
module InternetData
|
|
7
|
+
# Everything this library raises.
|
|
8
|
+
#
|
|
9
|
+
# `:rate_limited` and `:quota_exceeded` both arrive as HTTP 429 and are NOT the
|
|
10
|
+
# same thing. A rate limit is the API protecting itself and carries
|
|
11
|
+
# `Retry-After`; retrying works. A spent quota carries no such header and
|
|
12
|
+
# retrying will not help until the window rolls over or the limit is raised.
|
|
13
|
+
# The header is the only thing that distinguishes them.
|
|
14
|
+
class Error < StandardError
|
|
15
|
+
KINDS = %i[
|
|
16
|
+
bad_request unauthorized forbidden rate_limited quota_exceeded server_error network
|
|
17
|
+
].freeze
|
|
18
|
+
|
|
19
|
+
RETRYABLE = %i[rate_limited server_error network].freeze
|
|
20
|
+
|
|
21
|
+
# The API's machine-readable result code, when the refusal carried one:
|
|
22
|
+
# `NOT_LICENSED`, `UNKNOWN_DATASET`, `NOT_AVAILABLE` and so on. Deliberately
|
|
23
|
+
# not an enum, so a code added later stays readable by a client built today.
|
|
24
|
+
attr_reader :kind, :status, :rc, :retry_after_seconds
|
|
25
|
+
|
|
26
|
+
def initialize(kind, message, status: nil, rc: nil, retry_after_seconds: nil)
|
|
27
|
+
super(message)
|
|
28
|
+
@kind = kind
|
|
29
|
+
@status = status
|
|
30
|
+
@rc = rc
|
|
31
|
+
@retry_after_seconds = retry_after_seconds
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Whether retrying this exact request could succeed.
|
|
35
|
+
def retryable?
|
|
36
|
+
RETRYABLE.include?(@kind)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# `message:` stands in for the response body, for a response whose body must
|
|
40
|
+
# not be read: an object storage error page has no bounded size, so a refusal
|
|
41
|
+
# from it is classified on the status alone.
|
|
42
|
+
def self.from_status(status, headers, body, message: nil)
|
|
43
|
+
rc = rc_of(body)
|
|
44
|
+
message ||= rc || "request failed with status #{status}"
|
|
45
|
+
retry_after = parse_retry_after(header(headers, 'retry-after'))
|
|
46
|
+
|
|
47
|
+
case status
|
|
48
|
+
when 429
|
|
49
|
+
# Present means transient, absent means an allowance is spent. Nothing
|
|
50
|
+
# else in the response separates the two.
|
|
51
|
+
if retry_after.nil?
|
|
52
|
+
new(:quota_exceeded, message, status: status, rc: rc)
|
|
53
|
+
else
|
|
54
|
+
new(:rate_limited, message, status: status, rc: rc, retry_after_seconds: retry_after)
|
|
55
|
+
end
|
|
56
|
+
when 400 then new(:bad_request, message, status: status, rc: rc)
|
|
57
|
+
when 401 then new(:unauthorized, message, status: status, rc: rc)
|
|
58
|
+
when 403 then new(:forbidden, message, status: status, rc: rc)
|
|
59
|
+
# Every other 4xx is a CLIENT error. Classifying on the RANGE rather than
|
|
60
|
+
# on an enumerated list is what keeps a 404 from an unknown database id
|
|
61
|
+
# falling through to the retryable server_error default and being retried
|
|
62
|
+
# twice before it fails.
|
|
63
|
+
when 400..499 then new(:bad_request, message, status: status, rc: rc)
|
|
64
|
+
else new(:server_error, message, status: status, rc: rc)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# A Typhoeus response that never carried an HTTP status: DNS failure,
|
|
69
|
+
# connection refused, TLS failure, or a timeout.
|
|
70
|
+
def self.from_transport(response)
|
|
71
|
+
message = response.return_message || response.return_code&.to_s || 'transport failure'
|
|
72
|
+
new(:network, message)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Case-insensitive, and works with a plain Hash as well as with the
|
|
76
|
+
# case-blind header object Typhoeus builds from a live response.
|
|
77
|
+
def self.header(headers, name)
|
|
78
|
+
return nil if headers.nil?
|
|
79
|
+
|
|
80
|
+
value = headers[name] || headers[name.split('-').map(&:capitalize).join('-')]
|
|
81
|
+
value = headers.find { |k, _| k.to_s.downcase == name }&.last if value.nil?
|
|
82
|
+
value.is_a?(Array) ? value.last : value
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Every refusal this API writes is a `{"rc": "..."}` envelope. An
|
|
86
|
+
# intermediary's HTML error page parses as neither JSON nor that, and leaves
|
|
87
|
+
# the status to speak for itself.
|
|
88
|
+
def self.rc_of(body)
|
|
89
|
+
parsed = body.is_a?(String) ? (JSON.parse(body) rescue nil) : body
|
|
90
|
+
return nil unless parsed.is_a?(Hash)
|
|
91
|
+
|
|
92
|
+
parsed['rc'].is_a?(String) && !parsed['rc'].empty? ? parsed['rc'] : nil
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.parse_retry_after(value)
|
|
96
|
+
return nil if value.nil? || value.to_s.strip.empty?
|
|
97
|
+
|
|
98
|
+
seconds = Float(value, exception: false)
|
|
99
|
+
return seconds if seconds && seconds >= 0
|
|
100
|
+
|
|
101
|
+
# The header also permits an HTTP date.
|
|
102
|
+
when_at = Time.httpdate(value) rescue nil
|
|
103
|
+
return nil if when_at.nil?
|
|
104
|
+
|
|
105
|
+
[0, (when_at - Time.now).ceil].max
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
private_class_method :header, :rc_of, :parse_retry_after
|
|
109
|
+
end
|
|
110
|
+
end
|