galaxy-ruby 0.5.0 → 0.6.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 +4 -4
- data/CHANGELOG.md +27 -0
- data/README.md +26 -5
- data/SKILL.md +6 -4
- data/api.md +8 -8
- data/lib/galaxy-ruby/client.rb +43 -12
- data/lib/galaxy-ruby/errors.rb +40 -0
- data/lib/galaxy-ruby/file_part.rb +13 -1
- data/lib/galaxy-ruby/internal/transport/base_client.rb +278 -42
- data/lib/galaxy-ruby/internal/transport/connection_pool.rb +160 -0
- data/lib/galaxy-ruby/internal/transport/pooled_net_requester.rb +77 -10
- data/lib/galaxy-ruby/internal/type/base_model.rb +62 -17
- data/lib/galaxy-ruby/internal/type/boolean.rb +3 -1
- data/lib/galaxy-ruby/internal/type/converter.rb +13 -3
- data/lib/galaxy-ruby/internal/type/enum.rb +9 -4
- data/lib/galaxy-ruby/internal/type/file_input.rb +8 -2
- data/lib/galaxy-ruby/internal/type/hash_of.rb +3 -1
- data/lib/galaxy-ruby/internal/type/union.rb +17 -6
- data/lib/galaxy-ruby/internal/type/unknown.rb +3 -1
- data/lib/galaxy-ruby/internal/util.rb +273 -15
- data/lib/galaxy-ruby/internal/webhook_verifier.rb +240 -0
- data/lib/galaxy-ruby/internal.rb +4 -1
- data/lib/galaxy-ruby/models/authentication_create_token_params.rb +7 -1
- data/lib/galaxy-ruby/models/authentication_create_user_params.rb +2 -0
- data/lib/galaxy-ruby/models/celestial_body_create_params.rb +3 -3
- data/lib/galaxy-ruby/models/celestial_body_param.rb +72 -0
- data/lib/galaxy-ruby/models/credentials.rb +1 -9
- data/lib/galaxy-ruby/models/planet_create_params.rb +11 -1
- data/lib/galaxy-ruby/models/{planet_list_all_data_params.rb → planet_list_params.rb} +2 -2
- data/lib/galaxy-ruby/models/{planet_list_all_data_response.rb → planet_list_response.rb} +9 -9
- data/lib/galaxy-ruby/models/planet_param.rb +204 -0
- data/lib/galaxy-ruby/models/planet_update_params.rb +12 -1
- data/lib/galaxy-ruby/models/{planet_delte_image_params.rb → planet_upload_image_params.rb} +2 -2
- data/lib/galaxy-ruby/models/{planet_delte_image_response.rb → planet_upload_image_response.rb} +2 -2
- data/lib/galaxy-ruby/models/user_param.rb +19 -0
- data/lib/galaxy-ruby/models/webhook_unwrap_params.rb +2 -0
- data/lib/galaxy-ruby/models.rb +38 -33
- data/lib/galaxy-ruby/request_options.rb +13 -5
- data/lib/galaxy-ruby/resources/celestial_bodies.rb +1 -1
- data/lib/galaxy-ruby/resources/planets.rb +37 -47
- data/lib/galaxy-ruby/resources/webhooks.rb +63 -6
- data/lib/galaxy-ruby/version.rb +1 -1
- data/lib/galaxy-ruby.rb +12 -10
- data/rbi/galaxy-ruby/client.rbi +23 -0
- data/rbi/galaxy-ruby/errors.rbi +25 -27
- data/rbi/galaxy-ruby/internal/transport/base_client.rbi +88 -68
- data/rbi/galaxy-ruby/internal/transport/connection_pool.rbi +44 -0
- data/rbi/galaxy-ruby/internal/transport/pooled_net_requester.rbi +18 -19
- data/rbi/galaxy-ruby/internal/type/base_model.rbi +27 -49
- data/rbi/galaxy-ruby/internal/type/base_page.rbi +1 -2
- data/rbi/galaxy-ruby/internal/type/converter.rbi +12 -51
- data/rbi/galaxy-ruby/internal/type/enum.rbi +1 -5
- data/rbi/galaxy-ruby/internal/type/request_parameters.rbi +2 -6
- data/rbi/galaxy-ruby/internal/type/union.rbi +2 -11
- data/rbi/galaxy-ruby/internal/util.rbi +101 -115
- data/rbi/galaxy-ruby/internal/webhook_verifier.rbi +86 -0
- data/rbi/galaxy-ruby/internal.rbi +1 -4
- data/rbi/galaxy-ruby/models/authentication_create_token_params.rbi +11 -5
- data/rbi/galaxy-ruby/models/celestial_body_create_params.rbi +8 -5
- data/rbi/galaxy-ruby/models/celestial_body_param.rbi +138 -0
- data/rbi/galaxy-ruby/models/credentials.rbi +2 -5
- data/rbi/galaxy-ruby/models/planet_create_params.rbi +22 -5
- data/rbi/galaxy-ruby/models/{planet_list_all_data_params.rbi → planet_list_params.rbi} +2 -5
- data/rbi/galaxy-ruby/models/{planet_list_all_data_response.rbi → planet_list_response.rbi} +11 -15
- data/rbi/galaxy-ruby/models/planet_param.rbi +346 -0
- data/rbi/galaxy-ruby/models/planet_update_params.rbi +19 -2
- data/rbi/galaxy-ruby/models/{planet_delte_image_params.rbi → planet_upload_image_params.rbi} +2 -2
- data/rbi/galaxy-ruby/models/{planet_delte_image_response.rbi → planet_upload_image_response.rbi} +2 -2
- data/rbi/galaxy-ruby/models/unwrap_webhook_event.rbi +1 -1
- data/rbi/galaxy-ruby/models/user_param.rbi +27 -0
- data/rbi/galaxy-ruby/models/webhook_unwrap_params.rbi +2 -0
- data/rbi/galaxy-ruby/models.rbi +10 -2
- data/rbi/galaxy-ruby/request_options.rbi +4 -10
- data/rbi/galaxy-ruby/resources/celestial_bodies.rbi +2 -2
- data/rbi/galaxy-ruby/resources/planets.rbi +22 -30
- data/rbi/galaxy-ruby/resources/webhooks.rbi +64 -3
- data/sig/galaxy-ruby/client.rbs +8 -0
- data/sig/galaxy-ruby/errors.rbs +13 -0
- data/sig/galaxy-ruby/internal/transport/base_client.rbs +38 -2
- data/sig/galaxy-ruby/internal/transport/connection_pool.rbs +19 -0
- data/sig/galaxy-ruby/internal/transport/pooled_net_requester.rbs +13 -2
- data/sig/galaxy-ruby/internal/type/base_model.rbs +4 -0
- data/sig/galaxy-ruby/internal/util.rbs +29 -1
- data/sig/galaxy-ruby/internal/webhook_verifier.rbs +53 -0
- data/sig/galaxy-ruby/models/authentication_create_token_params.rbs +13 -3
- data/sig/galaxy-ruby/models/celestial_body_create_params.rbs +4 -4
- data/sig/galaxy-ruby/models/celestial_body_param.rbs +69 -0
- data/sig/galaxy-ruby/models/credentials.rbs +1 -3
- data/sig/galaxy-ruby/models/planet_create_params.rbs +14 -3
- data/sig/galaxy-ruby/models/{planet_list_all_data_params.rbs → planet_list_params.rbs} +2 -2
- data/sig/galaxy-ruby/models/{planet_list_all_data_response.rbs → planet_list_response.rbs} +15 -13
- data/sig/galaxy-ruby/models/planet_param.rbs +199 -0
- data/sig/galaxy-ruby/models/planet_update_params.rbs +8 -1
- data/sig/galaxy-ruby/models/{planet_delte_image_params.rbs → planet_upload_image_params.rbs} +2 -2
- data/sig/galaxy-ruby/models/{planet_delte_image_response.rbs → planet_upload_image_response.rbs} +2 -2
- data/sig/galaxy-ruby/models/user_param.rbs +15 -0
- data/sig/galaxy-ruby/models/webhook_unwrap_params.rbs +4 -0
- data/sig/galaxy-ruby/models.rbs +10 -2
- data/sig/galaxy-ruby/resources/celestial_bodies.rbs +1 -1
- data/sig/galaxy-ruby/resources/planets.rbs +10 -14
- data/sig/galaxy-ruby/resources/webhooks.rbs +17 -1
- metadata +32 -71
|
@@ -12,7 +12,11 @@ module ScalarGalaxy
|
|
|
12
12
|
# from whatwg fetch spec
|
|
13
13
|
MAX_REDIRECTS = 20
|
|
14
14
|
|
|
15
|
-
#
|
|
15
|
+
# Longest wait, in seconds, a `retry-after-ms` or `Retry-After` header may ask for before the
|
|
16
|
+
# client's own backoff is used instead.
|
|
17
|
+
MAX_RETRY_AFTER = 60
|
|
18
|
+
|
|
19
|
+
# rubocop:disable-next Style/MutableConstant
|
|
16
20
|
PLATFORM_HEADERS = {
|
|
17
21
|
"x-scalar-arch" => ScalarGalaxy::Internal::Util.arch,
|
|
18
22
|
"x-scalar-lang" => "ruby",
|
|
@@ -21,7 +25,6 @@ module ScalarGalaxy
|
|
|
21
25
|
"x-scalar-runtime" => ::RUBY_ENGINE,
|
|
22
26
|
"x-scalar-runtime-version" => ::RUBY_ENGINE_VERSION
|
|
23
27
|
}
|
|
24
|
-
# rubocop:enable Style/MutableConstant
|
|
25
28
|
|
|
26
29
|
class << self
|
|
27
30
|
# @api private
|
|
@@ -30,7 +33,7 @@ module ScalarGalaxy
|
|
|
30
33
|
#
|
|
31
34
|
# @raise [ArgumentError]
|
|
32
35
|
def validate!(req)
|
|
33
|
-
keys = %i[method path query headers body unwrap page stream model options]
|
|
36
|
+
keys = %i[method path query headers body unwrap page stream model security options]
|
|
34
37
|
case req
|
|
35
38
|
in Hash
|
|
36
39
|
req.each_key do |k|
|
|
@@ -66,6 +69,61 @@ module ScalarGalaxy
|
|
|
66
69
|
end
|
|
67
70
|
end
|
|
68
71
|
|
|
72
|
+
# @api private
|
|
73
|
+
#
|
|
74
|
+
# The wait in seconds a `retry-after-ms` or `Retry-After` header asks for, when it is a
|
|
75
|
+
# finite value over zero and at most {MAX_RETRY_AFTER}; otherwise `nil`, and the caller backs
|
|
76
|
+
# off on its own schedule.
|
|
77
|
+
#
|
|
78
|
+
# Both headers come from the server, so neither is trusted: a negative or past value would
|
|
79
|
+
# make `sleep` raise, and an hour or `1e400` would park the call. `Retry-After` is seconds
|
|
80
|
+
# or an HTTP date; a date that does not parse is ignored rather than raised.
|
|
81
|
+
#
|
|
82
|
+
# @param headers [Hash{String=>String}, Net::HTTPResponse]
|
|
83
|
+
#
|
|
84
|
+
# @return [Float, nil]
|
|
85
|
+
def retry_after(headers)
|
|
86
|
+
millis = Float(headers["retry-after-ms"], exception: false)
|
|
87
|
+
if millis&.finite? && millis.positive? && millis <= self::MAX_RETRY_AFTER * 1000
|
|
88
|
+
return millis / 1000
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
header = headers["retry-after"]
|
|
92
|
+
return nil if header.nil?
|
|
93
|
+
|
|
94
|
+
seconds = Float(header, exception: false)
|
|
95
|
+
seconds ||=
|
|
96
|
+
begin
|
|
97
|
+
Time.httpdate(header.to_s) - Time.now
|
|
98
|
+
rescue ArgumentError, RangeError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
seconds.to_f if seconds&.finite? && seconds.positive? && seconds <= self::MAX_RETRY_AFTER
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# @api private
|
|
105
|
+
#
|
|
106
|
+
# Whether a response is a redirect the client follows: a 301, 302, 303, 307 or 308 that
|
|
107
|
+
# names a `location`, the statuses the fetch spec follows. A 300 only offers choices, a 304
|
|
108
|
+
# tells a conditional request its copy is current, and a redirect with no `location` names
|
|
109
|
+
# no target, so `send_request` treats each as the server's answer and raises it as a status
|
|
110
|
+
# error, as it does a 4xx.
|
|
111
|
+
#
|
|
112
|
+
# @param status [Integer]
|
|
113
|
+
# @param headers [Hash{String=>String}]
|
|
114
|
+
#
|
|
115
|
+
# @return [Boolean]
|
|
116
|
+
def redirect?(status, headers:)
|
|
117
|
+
case status
|
|
118
|
+
in 301 | 302 | 303 | 307 | 308
|
|
119
|
+
# Net::HTTP strips a blank `location` to "", which `URI.join` resolves to the request's
|
|
120
|
+
# own URL, so it would be re-requested until the redirect limit.
|
|
121
|
+
!headers["location"].to_s.strip.empty?
|
|
122
|
+
else
|
|
123
|
+
false
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
69
127
|
# @api private
|
|
70
128
|
#
|
|
71
129
|
# @param request [Hash{Symbol=>Object}] .
|
|
@@ -86,13 +144,23 @@ module ScalarGalaxy
|
|
|
86
144
|
#
|
|
87
145
|
# @param response_headers [Hash{String=>String}]
|
|
88
146
|
#
|
|
147
|
+
# @param base_url [URI::Generic] the client's base URL, whose origin is the only one its
|
|
148
|
+
# credentials belong to
|
|
149
|
+
#
|
|
150
|
+
# @param credential_headers [Array<String>] lowercase names of the client's header
|
|
151
|
+
# credentials, its `auth_header_keys`
|
|
152
|
+
#
|
|
89
153
|
# @return [Hash{Symbol=>Object}]
|
|
90
|
-
def follow_redirect(request, status:, response_headers:)
|
|
154
|
+
def follow_redirect(request, status:, response_headers:, base_url:, credential_headers:)
|
|
91
155
|
method, url, headers = request.fetch_values(:method, :url, :headers)
|
|
156
|
+
# `send_request` only passes a response `redirect?` accepted. The rescue covers a call
|
|
157
|
+
# handed a 3xx with no `location` (`ArgumentError`), and a `location` that is not a valid
|
|
158
|
+
# URI reference (`URI::Error`), such as one with a space or a bad `%` escape, or two
|
|
159
|
+
# `Location` headers Net::HTTP joined into "/a, /b".
|
|
92
160
|
location =
|
|
93
161
|
Kernel.then do
|
|
94
162
|
URI.join(url, response_headers["location"])
|
|
95
|
-
rescue ArgumentError
|
|
163
|
+
rescue ArgumentError, URI::Error
|
|
96
164
|
message = "Server responded with status #{status} but no valid location header."
|
|
97
165
|
raise ScalarGalaxy::Errors::APIConnectionError.new(
|
|
98
166
|
url: url,
|
|
@@ -135,6 +203,16 @@ module ScalarGalaxy
|
|
|
135
203
|
request = {**request, headers: request.fetch(:headers).except(*drop)}
|
|
136
204
|
end
|
|
137
205
|
|
|
206
|
+
# The list above only knows the standard credential headers, so an API key sent in a
|
|
207
|
+
# header of its own (`x-api-key`) would otherwise follow any redirect to a foreign host,
|
|
208
|
+
# and a 307/308 would carry the method and body with it. The drop is sticky: nothing
|
|
209
|
+
# re-adds a header once it is gone, so a chain that returns to the client's origin after
|
|
210
|
+
# a foreign hop still arrives without it.
|
|
211
|
+
if ScalarGalaxy::Internal::Util.uri_origin(base_url) !=
|
|
212
|
+
ScalarGalaxy::Internal::Util.uri_origin(location)
|
|
213
|
+
request = {**request, headers: request.fetch(:headers).except(*credential_headers)}
|
|
214
|
+
end
|
|
215
|
+
|
|
138
216
|
request
|
|
139
217
|
end
|
|
140
218
|
|
|
@@ -223,8 +301,120 @@ module ScalarGalaxy
|
|
|
223
301
|
|
|
224
302
|
# @api private
|
|
225
303
|
#
|
|
304
|
+
# The credential headers one request is sent with, given the security requirement groups its
|
|
305
|
+
# operation passes as `security:` (`nil` for the client's default). A client that scopes its
|
|
306
|
+
# credentials per operation overrides this; every other one sends all of `auth_headers`.
|
|
307
|
+
#
|
|
308
|
+
# @param _security [Array<Array<String>>, nil]
|
|
309
|
+
#
|
|
310
|
+
# @return [Hash{String=>String}]
|
|
311
|
+
private def auth_headers_for(_security) = auth_headers
|
|
312
|
+
|
|
313
|
+
# @api private
|
|
314
|
+
#
|
|
315
|
+
# Lowercased names of every header credential `auth_headers` can write, whether or not the
|
|
316
|
+
# client holds a value for it. A redirect that leaves the base URL's origin drops these, so a
|
|
317
|
+
# key passed per call through `extra_headers` is dropped the same as one the client holds.
|
|
318
|
+
#
|
|
319
|
+
# @return [Array<String>]
|
|
320
|
+
private def auth_header_keys = []
|
|
321
|
+
|
|
322
|
+
# @api private
|
|
323
|
+
#
|
|
324
|
+
# Credentials the client sends as query parameters, merged under each request's own query
|
|
325
|
+
# so a per-request or `extra_query` value still wins.
|
|
326
|
+
#
|
|
327
|
+
# @return [Hash{String=>String}]
|
|
328
|
+
private def auth_query = {}
|
|
329
|
+
|
|
330
|
+
# @api private
|
|
331
|
+
#
|
|
332
|
+
# The query credentials one request is sent with, given the security requirement groups its
|
|
333
|
+
# operation passes as `security:`; a client that scopes its credentials per operation
|
|
334
|
+
# overrides this, every other one sends all of `auth_query`.
|
|
335
|
+
#
|
|
336
|
+
# @param _security [Array<Array<String>>, nil]
|
|
337
|
+
#
|
|
338
|
+
# @return [Hash{String=>String}]
|
|
339
|
+
private def auth_query_for(_security) = auth_query
|
|
340
|
+
|
|
341
|
+
# @api private
|
|
342
|
+
#
|
|
343
|
+
# Wire names of every query-carried credential, whether or not the client holds a value for
|
|
344
|
+
# it. Redaction and redirects key off these rather than off `auth_query`, which drops unset
|
|
345
|
+
# values: a key passed per call through `extra_query` travels under the same name.
|
|
346
|
+
#
|
|
347
|
+
# @return [Array<String>]
|
|
348
|
+
private def auth_query_keys = []
|
|
349
|
+
|
|
350
|
+
# @api private
|
|
351
|
+
#
|
|
352
|
+
# The URL with every `auth_query_keys` credential's value replaced, for anything that outlives the
|
|
353
|
+
# request: error messages and `#url`, `#inspect`, and the stream that raises from a frame. Those
|
|
354
|
+
# end up in logs and error trackers, and a query-carried key must not travel with them.
|
|
355
|
+
#
|
|
356
|
+
# @param url [URI::Generic, nil]
|
|
357
|
+
#
|
|
358
|
+
# @return [URI::Generic, nil]
|
|
359
|
+
private def redact_url(url)
|
|
360
|
+
secrets = auth_query_keys
|
|
361
|
+
return url if secrets.empty? || url.nil? || url.query.nil?
|
|
362
|
+
|
|
363
|
+
query = ScalarGalaxy::Internal::Util.decode_query(url.query)
|
|
364
|
+
secrets.each { query[_1] = ["REDACTED"] if query.key?(_1) }
|
|
365
|
+
redacted = url.dup
|
|
366
|
+
redacted.query = ScalarGalaxy::Internal::Util.encode_query(query)
|
|
367
|
+
redacted
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# @api private
|
|
371
|
+
#
|
|
372
|
+
# A redirect rebuilds the URL from the `Location` header, which drops the query credential.
|
|
373
|
+
# It is merged back only while the whole chain has stayed on the client's own origin —
|
|
374
|
+
# compared with the base URL, never the previous hop. Once any hop leaves it (`left_origin`),
|
|
375
|
+
# the key is never re-sent, even to a later hop back on the API, as `follow_redirect` drops
|
|
376
|
+
# the header credentials (`Authorization`, `Cookie` and every `auth_header_keys` name) once
|
|
377
|
+
# a hop leaves it, for good; a 307/308 from a foreign host would otherwise replay the
|
|
378
|
+
# original method and body, with the key, at a path it picks.
|
|
379
|
+
# Both origins go through `Util.uri_origin`, so an explicit default port on either side
|
|
380
|
+
# (`https://api.x.com:443`) compares equal to none. The values re-sent are the ones the
|
|
381
|
+
# original request carried (a per-call `extra_query` key included), and a value the server
|
|
382
|
+
# already put in `Location` is left alone: missing credentials are appended to its query
|
|
383
|
+
# string as-is, never decoded and re-encoded, so flags, `;`, key order and signed values
|
|
384
|
+
# survive byte for byte. The error URL is refreshed either way.
|
|
385
|
+
#
|
|
386
|
+
# @param previous [Hash{Symbol=>Object}]
|
|
387
|
+
# @param redirected [Hash{Symbol=>Object}]
|
|
388
|
+
#
|
|
389
|
+
# @return [Hash{Symbol=>Object}]
|
|
390
|
+
private def reauthorize_redirect(previous, redirected)
|
|
391
|
+
sent =
|
|
392
|
+
previous.fetch(:auth_query_sent) do
|
|
393
|
+
carried = ScalarGalaxy::Internal::Util.decode_query(previous.fetch(:url).query)
|
|
394
|
+
auth_query_keys.to_h { [_1, carried[_1]] }.reject { |_, values| values.empty? }
|
|
395
|
+
end
|
|
396
|
+
to = redirected.fetch(:url)
|
|
397
|
+
left =
|
|
398
|
+
previous.fetch(:left_origin, false) ||
|
|
399
|
+
ScalarGalaxy::Internal::Util.uri_origin(to) !=
|
|
400
|
+
ScalarGalaxy::Internal::Util.uri_origin(@base_url)
|
|
401
|
+
present = ScalarGalaxy::Internal::Util.decode_query(to.query)
|
|
402
|
+
missing = left ? {} : sent.reject { |key, _| present.key?(key) }
|
|
403
|
+
unless missing.empty?
|
|
404
|
+
appended = ScalarGalaxy::Internal::Util.encode_query(missing)
|
|
405
|
+
to = to.dup
|
|
406
|
+
to.query = to.query.to_s.empty? ? appended : "#{to.query}&#{appended}"
|
|
407
|
+
end
|
|
408
|
+
{**redirected, url: to, error_url: redact_url(to), auth_query_sent: sent, left_origin: left}
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# @api private
|
|
412
|
+
#
|
|
413
|
+
# A literal rather than `self.class.name`: a class name is namespaced (`Acme::Client`), and
|
|
414
|
+
# `:` is not a token character (RFC 9110 §5.6.2).
|
|
415
|
+
#
|
|
226
416
|
# @return [String]
|
|
227
|
-
private def user_agent = "
|
|
417
|
+
private def user_agent = "ScalarGalaxy/Ruby #{ScalarGalaxy::VERSION}"
|
|
228
418
|
|
|
229
419
|
# @api private
|
|
230
420
|
#
|
|
@@ -253,6 +443,8 @@ module ScalarGalaxy
|
|
|
253
443
|
#
|
|
254
444
|
# @option req [ScalarGalaxy::Internal::Type::Converter, Class, nil] :model
|
|
255
445
|
#
|
|
446
|
+
# @option req [Array<Array<String>>, nil] :security
|
|
447
|
+
#
|
|
256
448
|
# @param opts [Hash{Symbol=>Object}] .
|
|
257
449
|
#
|
|
258
450
|
# @option opts [String, nil] :idempotency_key
|
|
@@ -273,15 +465,28 @@ module ScalarGalaxy
|
|
|
273
465
|
|
|
274
466
|
path = ScalarGalaxy::Internal::Util.interpolate_path(uninterpolated_path)
|
|
275
467
|
|
|
276
|
-
query = ScalarGalaxy::Internal::Util.deep_merge(
|
|
277
|
-
|
|
468
|
+
query = ScalarGalaxy::Internal::Util.deep_merge(
|
|
469
|
+
auth_query_for(req[:security]),
|
|
470
|
+
req[:query].to_h,
|
|
471
|
+
opts[:extra_query].to_h
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
# The client's defaults, its credentials and the operation's own headers each may carry a
|
|
475
|
+
# `cookie`, and a plain merge would let the operation's cookie parameters replace the
|
|
476
|
+
# credential cookie. Those three are merged by cookie name instead; `extra_headers` still
|
|
477
|
+
# replaces the whole header, as it replaces any other.
|
|
478
|
+
sources =
|
|
479
|
+
[@headers, auth_headers_for(req[:security]), req[:headers].to_h].map do
|
|
480
|
+
ScalarGalaxy::Internal::Util.normalized_headers(_1)
|
|
481
|
+
end
|
|
482
|
+
merged = ScalarGalaxy::Internal::Util.normalized_headers(*sources)
|
|
483
|
+
# A local rather than an inline block: under a long module name the inline form is broken
|
|
484
|
+
# inside its braces, and the multi-line `{ }` block that leaves is a `Style` offense.
|
|
485
|
+
cookies = sources.map { _1["cookie"] }
|
|
486
|
+
cookie = ScalarGalaxy::Internal::Util.merge_cookies(*cookies)
|
|
487
|
+
merged["cookie"] = cookie unless cookie.nil?
|
|
278
488
|
headers =
|
|
279
|
-
ScalarGalaxy::Internal::Util.normalized_headers(
|
|
280
|
-
@headers,
|
|
281
|
-
auth_headers,
|
|
282
|
-
req[:headers].to_h,
|
|
283
|
-
opts[:extra_headers].to_h
|
|
284
|
-
)
|
|
489
|
+
ScalarGalaxy::Internal::Util.normalized_headers(merged, opts[:extra_headers].to_h)
|
|
285
490
|
|
|
286
491
|
if @idempotency_header && !headers.key?(@idempotency_header) &&
|
|
287
492
|
(!Net::HTTP::IDEMPOTENT_METHODS_.include?(method.to_s.upcase) || opts.key?(:idempotency_key))
|
|
@@ -317,6 +522,7 @@ module ScalarGalaxy
|
|
|
317
522
|
{
|
|
318
523
|
method: method,
|
|
319
524
|
url: url,
|
|
525
|
+
error_url: redact_url(url),
|
|
320
526
|
headers: headers,
|
|
321
527
|
body: encoded,
|
|
322
528
|
max_retries: opts.fetch(:max_retries, @max_retries),
|
|
@@ -331,20 +537,8 @@ module ScalarGalaxy
|
|
|
331
537
|
#
|
|
332
538
|
# @return [Float]
|
|
333
539
|
private def retry_delay(headers, retry_count:)
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return span if span
|
|
337
|
-
|
|
338
|
-
retry_header = headers["retry-after"]
|
|
339
|
-
return span if (span = Float(retry_header, exception: false))
|
|
340
|
-
|
|
341
|
-
span =
|
|
342
|
-
retry_header&.then do
|
|
343
|
-
Time.httpdate(_1) - Time.now
|
|
344
|
-
rescue ArgumentError
|
|
345
|
-
nil
|
|
346
|
-
end
|
|
347
|
-
return span if span
|
|
540
|
+
asked = self.class.retry_after(headers)
|
|
541
|
+
return asked if asked
|
|
348
542
|
|
|
349
543
|
scale = retry_count**2
|
|
350
544
|
jitter = 1 - (0.25 * rand)
|
|
@@ -391,18 +585,34 @@ module ScalarGalaxy
|
|
|
391
585
|
end
|
|
392
586
|
headers = ScalarGalaxy::Internal::Util.normalized_headers(response&.each_header&.to_h)
|
|
393
587
|
|
|
588
|
+
redirect = status.is_a?(Integer) && self.class.redirect?(status, headers: headers)
|
|
589
|
+
|
|
394
590
|
case status
|
|
395
591
|
in ..299
|
|
396
592
|
[status, response, stream]
|
|
397
|
-
in
|
|
593
|
+
in Integer if redirect && redirect_count >= self.class::MAX_REDIRECTS
|
|
398
594
|
self.class.reap_connection!(status, stream: stream)
|
|
399
595
|
|
|
400
596
|
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
|
|
401
|
-
raise ScalarGalaxy::Errors::APIConnectionError.new(
|
|
402
|
-
|
|
597
|
+
raise ScalarGalaxy::Errors::APIConnectionError.new(
|
|
598
|
+
url: url,
|
|
599
|
+
response: response,
|
|
600
|
+
message: message
|
|
601
|
+
)
|
|
602
|
+
in Integer if redirect
|
|
403
603
|
self.class.reap_connection!(status, stream: stream)
|
|
404
604
|
|
|
405
|
-
request =
|
|
605
|
+
request =
|
|
606
|
+
reauthorize_redirect(
|
|
607
|
+
request,
|
|
608
|
+
self.class.follow_redirect(
|
|
609
|
+
request,
|
|
610
|
+
status: status,
|
|
611
|
+
response_headers: headers,
|
|
612
|
+
base_url: @base_url,
|
|
613
|
+
credential_headers: auth_header_keys
|
|
614
|
+
)
|
|
615
|
+
)
|
|
406
616
|
send_request(
|
|
407
617
|
request,
|
|
408
618
|
redirect_count: redirect_count + 1,
|
|
@@ -411,23 +621,32 @@ module ScalarGalaxy
|
|
|
411
621
|
)
|
|
412
622
|
in ScalarGalaxy::Errors::APIConnectionError if retry_count >= max_retries
|
|
413
623
|
raise status
|
|
414
|
-
|
|
624
|
+
# Any other 3xx — a 304 answering a conditional request, a 300, a redirect status without a
|
|
625
|
+
# `location` — is the server's answer rather than a hop, so it fails as a status error, like a 4xx.
|
|
626
|
+
in (300..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: headers)
|
|
627
|
+
# Read only for the error's `body`, so only its first `MAX_ERROR_BODY_BYTES` are kept and
|
|
628
|
+
# the rest is never read: a server cannot make an error cost more than that in memory.
|
|
415
629
|
decoded =
|
|
416
630
|
Kernel.then do
|
|
417
|
-
ScalarGalaxy::Internal::Util.decode_content(
|
|
631
|
+
ScalarGalaxy::Internal::Util.decode_content(
|
|
632
|
+
headers,
|
|
633
|
+
stream: stream,
|
|
634
|
+
suppress_error: true,
|
|
635
|
+
truncate_at: ScalarGalaxy::Internal::Util::MAX_ERROR_BODY_BYTES
|
|
636
|
+
)
|
|
418
637
|
ensure
|
|
419
638
|
self.class.reap_connection!(status, stream: stream)
|
|
420
639
|
end
|
|
421
640
|
|
|
422
641
|
raise ScalarGalaxy::Errors::APIStatusError.for(
|
|
423
|
-
url: url,
|
|
642
|
+
url: request.fetch(:error_url, url),
|
|
424
643
|
status: status,
|
|
425
644
|
headers: headers,
|
|
426
645
|
body: decoded,
|
|
427
646
|
request: nil,
|
|
428
647
|
response: response
|
|
429
648
|
)
|
|
430
|
-
in
|
|
649
|
+
in 300.. | ScalarGalaxy::Errors::APIConnectionError
|
|
431
650
|
self.class.reap_connection!(status, stream: stream)
|
|
432
651
|
|
|
433
652
|
delay = retry_delay(response || {}, retry_count: retry_count)
|
|
@@ -493,15 +712,29 @@ module ScalarGalaxy
|
|
|
493
712
|
# Don't send the current retry count in the headers if the caller modified the header defaults.
|
|
494
713
|
send_retry_header = request.fetch(:headers)["x-scalar-retry-count"] == "0"
|
|
495
714
|
status, response, stream =
|
|
496
|
-
|
|
715
|
+
begin
|
|
716
|
+
send_request(request, redirect_count: 0, retry_count: 0, send_retry_header: send_retry_header)
|
|
717
|
+
rescue ScalarGalaxy::Errors::APIError => e
|
|
718
|
+
# Connection, timeout and redirect errors are raised with the dialed URL; their messages
|
|
719
|
+
# carry none, so redacting the attribute is enough.
|
|
720
|
+
e.url = redact_url(e.url)
|
|
721
|
+
raise
|
|
722
|
+
end
|
|
497
723
|
|
|
498
724
|
headers = ScalarGalaxy::Internal::Util.normalized_headers(response.each_header.to_h)
|
|
499
|
-
|
|
725
|
+
# Only an operation declared to return raw bytes reads its body whole; every other buffered
|
|
726
|
+
# body is capped, since the response's own content type cannot be trusted to say which it is.
|
|
727
|
+
decoded =
|
|
728
|
+
ScalarGalaxy::Internal::Util.decode_content(
|
|
729
|
+
headers,
|
|
730
|
+
stream: stream,
|
|
731
|
+
download: model == StringIO
|
|
732
|
+
)
|
|
500
733
|
case req
|
|
501
734
|
in stream: Class => st
|
|
502
735
|
st.new(
|
|
503
736
|
model: model,
|
|
504
|
-
url: url,
|
|
737
|
+
url: request.fetch(:error_url, url),
|
|
505
738
|
status: status,
|
|
506
739
|
headers: headers,
|
|
507
740
|
response: response,
|
|
@@ -520,9 +753,8 @@ module ScalarGalaxy
|
|
|
520
753
|
#
|
|
521
754
|
# @return [String]
|
|
522
755
|
def inspect
|
|
523
|
-
# rubocop:disable Layout/LineLength
|
|
756
|
+
# rubocop:disable-next Layout/LineLength
|
|
524
757
|
"#<#{self.class.name}:0x#{object_id.to_s(16)} base_url=#{@base_url} max_retries=#{@max_retries} timeout=#{@timeout}>"
|
|
525
|
-
# rubocop:enable Layout/LineLength
|
|
526
758
|
end
|
|
527
759
|
|
|
528
760
|
define_sorbet_constant!(:RequestComponents) do
|
|
@@ -550,7 +782,11 @@ module ScalarGalaxy
|
|
|
550
782
|
),
|
|
551
783
|
page:
|
|
552
784
|
T.nilable(
|
|
553
|
-
T::Class[
|
|
785
|
+
T::Class[
|
|
786
|
+
ScalarGalaxy::Internal::Type::BasePage[
|
|
787
|
+
ScalarGalaxy::Internal::Type::BaseModel
|
|
788
|
+
]
|
|
789
|
+
]
|
|
554
790
|
),
|
|
555
791
|
stream: T.nilable(T::Class[T.anything]),
|
|
556
792
|
model: T.nilable(ScalarGalaxy::Internal::Type::Converter::Input),
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ScalarGalaxy
|
|
4
|
+
module Internal
|
|
5
|
+
module Transport
|
|
6
|
+
# @api private
|
|
7
|
+
#
|
|
8
|
+
# A fixed-size, thread-safe pool of lazily created connections. `PooledNetRequester` keeps one per
|
|
9
|
+
# origin, so `size` caps the concurrent requests to that origin.
|
|
10
|
+
class ConnectionPool
|
|
11
|
+
# @api private
|
|
12
|
+
#
|
|
13
|
+
# Checks a connection out for the duration of the block and back in afterwards, even when the
|
|
14
|
+
# block raises.
|
|
15
|
+
#
|
|
16
|
+
# @param timeout [Float] seconds to wait for a connection when every one is checked out
|
|
17
|
+
# @param blk [Proc]
|
|
18
|
+
#
|
|
19
|
+
# @raise [Timeout::Error] when no connection frees up within `timeout`
|
|
20
|
+
# @yieldparam [Object]
|
|
21
|
+
# @return [Object] the block's result
|
|
22
|
+
def with(timeout:, &blk)
|
|
23
|
+
# Interrupts (`Thread#raise`, `Timeout.timeout`) are held back while a connection changes
|
|
24
|
+
# hands, so none can land between the checkout and the `ensure` and leak the connection. The
|
|
25
|
+
# block itself runs with them enabled again.
|
|
26
|
+
::Thread.handle_interrupt(Object => :never) do
|
|
27
|
+
conn, pid = checkout(timeout)
|
|
28
|
+
begin
|
|
29
|
+
::Thread.handle_interrupt(Object => :immediate) { blk.call(conn) }
|
|
30
|
+
ensure
|
|
31
|
+
checkin(conn, pid)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @api private
|
|
37
|
+
#
|
|
38
|
+
# @param timeout [Float]
|
|
39
|
+
#
|
|
40
|
+
# @raise [Timeout::Error]
|
|
41
|
+
# @return [Array(Object, Integer)] the connection, and the process it was checked out in
|
|
42
|
+
private def checkout(timeout)
|
|
43
|
+
# The deadline is on the monotonic clock, as the requester's own is, so a wall-clock jump can
|
|
44
|
+
# neither stretch nor cut the wait.
|
|
45
|
+
deadline = ScalarGalaxy::Internal::Util.monotonic_secs + timeout
|
|
46
|
+
@mutex.synchronize do
|
|
47
|
+
reset_after_fork
|
|
48
|
+
loop do
|
|
49
|
+
# An idle connection or a free slot is handed out even once the deadline has passed: the
|
|
50
|
+
# timeout bounds the wait for one, and the requester raises its own timeout from there
|
|
51
|
+
# before it uses the connection. Most recently returned first, so the connection most
|
|
52
|
+
# likely to still be alive is the one reused.
|
|
53
|
+
return @idle.pop, @pid unless @idle.empty?
|
|
54
|
+
|
|
55
|
+
if @created < @size
|
|
56
|
+
# Created under the lock so two threads cannot both take the last slot. That is cheap:
|
|
57
|
+
# the block only builds the client object, and the requester connects it later.
|
|
58
|
+
conn = @create.call
|
|
59
|
+
@created += 1
|
|
60
|
+
return conn, @pid
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
remaining = deadline - ScalarGalaxy::Internal::Util.monotonic_secs
|
|
64
|
+
unless remaining.positive?
|
|
65
|
+
raise Timeout::Error, "Waited #{timeout} seconds for one of #{@size} pooled connections"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# `wait` releases the mutex while it sleeps and takes it back before returning, whether a
|
|
69
|
+
# check-in, the timeout or a spurious wakeup woke it. So the loop looks at the pool again
|
|
70
|
+
# instead of trusting the reason, and only ever waits for what is left of the deadline.
|
|
71
|
+
@available.wait(@mutex, remaining)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @api private
|
|
77
|
+
#
|
|
78
|
+
# @param conn [Object]
|
|
79
|
+
# @param pid [Integer]
|
|
80
|
+
private def checkin(conn, pid)
|
|
81
|
+
close = nil
|
|
82
|
+
@mutex.synchronize do
|
|
83
|
+
reset_after_fork
|
|
84
|
+
# A connection checked out before a fork is the parent's: `reset_after_fork` has already
|
|
85
|
+
# stopped counting it, so it is dropped rather than handed to the child's next request.
|
|
86
|
+
if pid == @pid && @close
|
|
87
|
+
close = @close
|
|
88
|
+
# Its slot is free again, so the next checkout can build a connection in its place.
|
|
89
|
+
@created -= 1
|
|
90
|
+
elsif pid == @pid
|
|
91
|
+
@idle.push(conn)
|
|
92
|
+
end
|
|
93
|
+
@available.signal
|
|
94
|
+
end
|
|
95
|
+
# A pool shut down while this connection was out keeps nothing; closed outside the lock.
|
|
96
|
+
begin
|
|
97
|
+
close&.call(conn)
|
|
98
|
+
rescue StandardError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @api private
|
|
104
|
+
#
|
|
105
|
+
# Closes every idle connection with `close` and every connection still checked out as it comes
|
|
106
|
+
# back, so a pool the owner has let go of holds no socket open. The pool still works after
|
|
107
|
+
# this, one connection per checkout, for a caller that looked it up just before.
|
|
108
|
+
#
|
|
109
|
+
# @param close [Proc] closes one connection
|
|
110
|
+
#
|
|
111
|
+
# @yieldparam [Object]
|
|
112
|
+
def shutdown(&close)
|
|
113
|
+
idle =
|
|
114
|
+
@mutex.synchronize do
|
|
115
|
+
@close = close
|
|
116
|
+
# A forked child does not own the parent's connections, so it closes none of them.
|
|
117
|
+
taken = @pid == Process.pid ? @idle.slice!(0..) : []
|
|
118
|
+
@created -= taken.size
|
|
119
|
+
taken
|
|
120
|
+
end
|
|
121
|
+
idle.each do |conn|
|
|
122
|
+
close.call(conn)
|
|
123
|
+
rescue StandardError
|
|
124
|
+
nil
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# @api private
|
|
129
|
+
#
|
|
130
|
+
# A forked child inherits the parent's open sockets. Reusing one would interleave both
|
|
131
|
+
# processes' requests on a single connection, so the first checkout or check-in in a new
|
|
132
|
+
# process forgets every connection and lets the pool fill up again from empty. The forgotten
|
|
133
|
+
# ones are not closed either: closing a TLS connection writes to the socket the parent is still
|
|
134
|
+
# using.
|
|
135
|
+
private def reset_after_fork
|
|
136
|
+
return if @pid == Process.pid
|
|
137
|
+
|
|
138
|
+
@idle.clear
|
|
139
|
+
@created = 0
|
|
140
|
+
@pid = Process.pid
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# @api private
|
|
144
|
+
#
|
|
145
|
+
# @param size [Integer]
|
|
146
|
+
# @param blk [Proc] builds one connection, called lazily as the pool fills up
|
|
147
|
+
def initialize(size:, &blk)
|
|
148
|
+
@size = size
|
|
149
|
+
@create = blk
|
|
150
|
+
@mutex = Mutex.new
|
|
151
|
+
@available = ConditionVariable.new
|
|
152
|
+
@idle = []
|
|
153
|
+
@created = 0
|
|
154
|
+
@pid = Process.pid
|
|
155
|
+
@close = nil
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|