-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbase_client.rb
More file actions
573 lines (521 loc) · 19.6 KB
/
base_client.rb
File metadata and controls
573 lines (521 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# frozen_string_literal: true
module Imagekitio
module Internal
module Transport
# @api private
#
# @abstract
class BaseClient
extend Imagekitio::Internal::Util::SorbetRuntimeSupport
# from whatwg fetch spec
MAX_REDIRECTS = 20
# rubocop:disable Style/MutableConstant
PLATFORM_HEADERS =
{
"x-stainless-arch" => Imagekitio::Internal::Util.arch,
"x-stainless-lang" => "ruby",
"x-stainless-os" => Imagekitio::Internal::Util.os,
"x-stainless-package-version" => Imagekitio::VERSION,
"x-stainless-runtime" => ::RUBY_ENGINE,
"x-stainless-runtime-version" => ::RUBY_ENGINE_VERSION
}
# rubocop:enable Style/MutableConstant
class << self
# @api private
#
# @param req [Hash{Symbol=>Object}]
#
# @raise [ArgumentError]
def validate!(req)
keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :stream, :model, :options]
case req
in Hash
req.each_key do |k|
unless keys.include?(k)
raise ArgumentError.new("Request `req` keys must be one of #{keys}, got #{k.inspect}")
end
end
else
raise ArgumentError.new("Request `req` must be a Hash or RequestOptions, got #{req.inspect}")
end
end
# @api private
#
# @param status [Integer]
# @param headers [Hash{String=>String}]
#
# @return [Boolean]
def should_retry?(status, headers:)
coerced = Imagekitio::Internal::Util.coerce_boolean(headers["x-should-retry"])
case [coerced, status]
in [true | false, _]
coerced
in [_, 408 | 409 | 429 | (500..)]
# retry on:
# 408: timeouts
# 409: locks
# 429: rate limits
# 500+: unknown errors
true
else
false
end
end
# @api private
#
# @param request [Hash{Symbol=>Object}] .
#
# @option request [Symbol] :method
#
# @option request [URI::Generic] :url
#
# @option request [Hash{String=>String}] :headers
#
# @option request [Object] :body
#
# @option request [Integer] :max_retries
#
# @option request [Float] :timeout
#
# @param status [Integer]
#
# @param response_headers [Hash{String=>String}]
#
# @return [Hash{Symbol=>Object}]
def follow_redirect(request, status:, response_headers:)
method, url, headers = request.fetch_values(:method, :url, :headers)
location =
Kernel.then do
URI.join(url, response_headers["location"])
rescue ArgumentError
message = "Server responded with status #{status} but no valid location header."
raise Imagekitio::Errors::APIConnectionError.new(
url: url,
response: response_headers,
message: message
)
end
request = {**request, url: location}
case [url.scheme, location.scheme]
in ["https", "http"]
message = "Tried to redirect to a insecure URL"
raise Imagekitio::Errors::APIConnectionError.new(
url: url,
response: response_headers,
message: message
)
else
nil
end
# from whatwg fetch spec
case [status, method]
in [301 | 302, :post] | [303, _]
drop = %w[content-encoding content-language content-length content-location content-type]
request = {
**request,
method: method == :head ? :head : :get,
headers: headers.except(*drop),
body: nil
}
else
end
# from undici
if Imagekitio::Internal::Util.uri_origin(url) != Imagekitio::Internal::Util.uri_origin(location)
drop = %w[authorization cookie host proxy-authorization]
request = {**request, headers: request.fetch(:headers).except(*drop)}
end
request
end
# @api private
#
# @param status [Integer, Imagekitio::Errors::APIConnectionError]
# @param stream [Enumerable<String>, nil]
def reap_connection!(status, stream:)
case status
in (..199) | (300..499)
stream&.each { next }
in Imagekitio::Errors::APIConnectionError | (500..)
Imagekitio::Internal::Util.close_fused!(stream)
else
end
end
end
# @return [URI::Generic]
attr_reader :base_url
# @return [Float]
attr_reader :timeout
# @return [Integer]
attr_reader :max_retries
# @return [Float]
attr_reader :initial_retry_delay
# @return [Float]
attr_reader :max_retry_delay
# @return [Hash{String=>String}]
attr_reader :headers
# @return [String, nil]
attr_reader :idempotency_header
# @api private
# @return [Imagekitio::Internal::Transport::PooledNetRequester]
attr_reader :requester
# @api private
#
# @param base_url [String]
# @param timeout [Float]
# @param max_retries [Integer]
# @param initial_retry_delay [Float]
# @param max_retry_delay [Float]
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}]
# @param idempotency_header [String, nil]
def initialize(
base_url:,
timeout: 0.0,
max_retries: 0,
initial_retry_delay: 0.0,
max_retry_delay: 0.0,
headers: {},
idempotency_header: nil
)
@requester = Imagekitio::Internal::Transport::PooledNetRequester.new
@headers = Imagekitio::Internal::Util.normalized_headers(
self.class::PLATFORM_HEADERS,
{
"accept" => "application/json",
"content-type" => "application/json",
"user-agent" => user_agent
},
headers
)
@base_url_components = Imagekitio::Internal::Util.parse_uri(base_url)
@base_url = Imagekitio::Internal::Util.unparse_uri(@base_url_components)
@idempotency_header = idempotency_header&.to_s&.downcase
@timeout = timeout
@max_retries = max_retries
@initial_retry_delay = initial_retry_delay
@max_retry_delay = max_retry_delay
end
# @api private
#
# @return [Hash{String=>String}]
private def auth_headers = {}
# @api private
#
# @return [String]
private def user_agent = "#{self.class.name}/Ruby #{Imagekitio::VERSION}"
# @api private
#
# @return [String]
private def generate_idempotency_key = "stainless-ruby-retry-#{SecureRandom.uuid}"
# @api private
#
# @param req [Hash{Symbol=>Object}] .
#
# @option req [Symbol] :method
#
# @option req [String, Array<String>] :path
#
# @option req [Hash{String=>Array<String>, String, nil}, nil] :query
#
# @option req [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}, nil] :headers
#
# @option req [Object, nil] :body
#
# @option req [Symbol, Integer, Array<Symbol, Integer>, Proc, nil] :unwrap
#
# @option req [Class<Imagekitio::Internal::Type::BasePage>, nil] :page
#
# @option req [Class<Imagekitio::Internal::Type::BaseStream>, nil] :stream
#
# @option req [Imagekitio::Internal::Type::Converter, Class, nil] :model
#
# @param opts [Hash{Symbol=>Object}] .
#
# @option opts [String, nil] :idempotency_key
#
# @option opts [Hash{String=>Array<String>, String, nil}, nil] :extra_query
#
# @option opts [Hash{String=>String, nil}, nil] :extra_headers
#
# @option opts [Object, nil] :extra_body
#
# @option opts [Integer, nil] :max_retries
#
# @option opts [Float, nil] :timeout
#
# @return [Hash{Symbol=>Object}]
private def build_request(req, opts)
method, uninterpolated_path = req.fetch_values(:method, :path)
path = Imagekitio::Internal::Util.interpolate_path(uninterpolated_path)
query = Imagekitio::Internal::Util.deep_merge(req[:query].to_h, opts[:extra_query].to_h)
headers = Imagekitio::Internal::Util.normalized_headers(
@headers,
auth_headers,
req[:headers].to_h,
opts[:extra_headers].to_h
)
if @idempotency_header &&
!headers.key?(@idempotency_header) &&
(!Net::HTTP::IDEMPOTENT_METHODS_.include?(method.to_s.upcase) || opts.key?(:idempotency_key))
headers[@idempotency_header] = opts.fetch(:idempotency_key) { generate_idempotency_key }
end
unless headers.key?("x-stainless-retry-count")
headers["x-stainless-retry-count"] = "0"
end
timeout = opts.fetch(:timeout, @timeout).to_f.clamp(0..)
unless headers.key?("x-stainless-timeout") || timeout.zero?
headers["x-stainless-timeout"] = timeout.to_s
end
headers.reject! { |_, v| v.to_s.empty? }
body =
case method
in :get | :head | :options | :trace
nil
else
Imagekitio::Internal::Util.deep_merge(*[req[:body], opts[:extra_body]].compact)
end
url = Imagekitio::Internal::Util.join_parsed_uri(
@base_url_components,
{**req, path: path, query: query}
)
headers, encoded = Imagekitio::Internal::Util.encode_content(headers, body)
{
method: method,
url: url,
headers: headers,
body: encoded,
max_retries: opts.fetch(:max_retries, @max_retries),
timeout: timeout
}
end
# @api private
#
# @param headers [Hash{String=>String}]
# @param retry_count [Integer]
#
# @return [Float]
private def retry_delay(headers, retry_count:)
# Non-standard extension
span = Float(headers["retry-after-ms"], exception: false)&.then { _1 / 1000 }
return span if span
retry_header = headers["retry-after"]
return span if (span = Float(retry_header, exception: false))
span = retry_header&.then do
Time.httpdate(_1) - Time.now
rescue ArgumentError
nil
end
return span if span
scale = retry_count**2
jitter = 1 - (0.25 * rand)
(@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
end
# @api private
#
# @param request [Hash{Symbol=>Object}] .
#
# @option request [Symbol] :method
#
# @option request [URI::Generic] :url
#
# @option request [Hash{String=>String}] :headers
#
# @option request [Object] :body
#
# @option request [Integer] :max_retries
#
# @option request [Float] :timeout
#
# @param redirect_count [Integer]
#
# @param retry_count [Integer]
#
# @param send_retry_header [Boolean]
#
# @raise [Imagekitio::Errors::APIError]
# @return [Array(Integer, Net::HTTPResponse, Enumerable<String>)]
def send_request(request, redirect_count:, retry_count:, send_retry_header:)
url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
input = {**request.except(:timeout), deadline: Imagekitio::Internal::Util.monotonic_secs + timeout}
if send_retry_header
headers["x-stainless-retry-count"] = retry_count.to_s
end
begin
status, response, stream = @requester.execute(input)
rescue Imagekitio::Errors::APIConnectionError => e
status = e
end
headers = Imagekitio::Internal::Util.normalized_headers(response&.each_header&.to_h)
case status
in ..299
[status, response, stream]
in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
self.class.reap_connection!(status, stream: stream)
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
raise Imagekitio::Errors::APIConnectionError.new(url: url, response: response, message: message)
in 300..399
self.class.reap_connection!(status, stream: stream)
request = self.class.follow_redirect(request, status: status, response_headers: headers)
send_request(
request,
redirect_count: redirect_count + 1,
retry_count: retry_count,
send_retry_header: send_retry_header
)
in Imagekitio::Errors::APIConnectionError if retry_count >= max_retries
raise status
in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: headers)
decoded = Kernel.then do
Imagekitio::Internal::Util.decode_content(headers, stream: stream, suppress_error: true)
ensure
self.class.reap_connection!(status, stream: stream)
end
raise Imagekitio::Errors::APIStatusError.for(
url: url,
status: status,
headers: headers,
body: decoded,
request: nil,
response: response
)
in (400..) | Imagekitio::Errors::APIConnectionError
self.class.reap_connection!(status, stream: stream)
delay = retry_delay(response || {}, retry_count: retry_count)
sleep(delay)
send_request(
request,
redirect_count: redirect_count,
retry_count: retry_count + 1,
send_retry_header: send_retry_header
)
end
end
# Execute the request specified by `req`. This is the method that all resource
# methods call into.
#
# @overload request(method, path, query: {}, headers: {}, body: nil, unwrap: nil, page: nil, stream: nil, model: Imagekitio::Internal::Type::Unknown, options: {})
#
# @param method [Symbol]
#
# @param path [String, Array<String>]
#
# @param query [Hash{String=>Array<String>, String, nil}, nil]
#
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}, nil]
#
# @param body [Object, nil]
#
# @param unwrap [Symbol, Integer, Array<Symbol, Integer>, Proc, nil]
#
# @param page [Class<Imagekitio::Internal::Type::BasePage>, nil]
#
# @param stream [Class<Imagekitio::Internal::Type::BaseStream>, nil]
#
# @param model [Imagekitio::Internal::Type::Converter, Class, nil]
#
# @param options [Imagekitio::RequestOptions, Hash{Symbol=>Object}, nil] .
#
# @option options [String, nil] :idempotency_key
#
# @option options [Hash{String=>Array<String>, String, nil}, nil] :extra_query
#
# @option options [Hash{String=>String, nil}, nil] :extra_headers
#
# @option options [Object, nil] :extra_body
#
# @option options [Integer, nil] :max_retries
#
# @option options [Float, nil] :timeout
#
# @raise [Imagekitio::Errors::APIError]
# @return [Object]
def request(req)
self.class.validate!(req)
model = req.fetch(:model) { Imagekitio::Internal::Type::Unknown }
opts = req[:options].to_h
unwrap = req[:unwrap]
Imagekitio::RequestOptions.validate!(opts)
request = build_request(req.except(:options), opts)
url = request.fetch(:url)
# Don't send the current retry count in the headers if the caller modified the header defaults.
send_retry_header = request.fetch(:headers)["x-stainless-retry-count"] == "0"
status, response, stream = send_request(
request,
redirect_count: 0,
retry_count: 0,
send_retry_header: send_retry_header
)
headers = Imagekitio::Internal::Util.normalized_headers(response.each_header.to_h)
decoded = Imagekitio::Internal::Util.decode_content(headers, stream: stream)
case req
in {stream: Class => st}
st.new(
model: model,
url: url,
status: status,
headers: headers,
response: response,
unwrap: unwrap,
stream: decoded
)
in {page: Class => page}
page.new(client: self, req: req, headers: headers, page_data: decoded)
else
unwrapped = Imagekitio::Internal::Util.dig(decoded, unwrap)
Imagekitio::Internal::Type::Converter.coerce(model, unwrapped)
end
end
# @api private
#
# @return [String]
def inspect
# rubocop:disable Layout/LineLength
"#<#{self.class.name}:0x#{object_id.to_s(16)} base_url=#{@base_url} max_retries=#{@max_retries} timeout=#{@timeout}>"
# rubocop:enable Layout/LineLength
end
define_sorbet_constant!(:RequestComponents) do
T.type_alias do
{
method: Symbol,
path: T.any(String, T::Array[String]),
query: T.nilable(T::Hash[String, T.nilable(T.any(T::Array[String], String))]),
headers: T.nilable(
T::Hash[String,
T.nilable(
T.any(
String,
Integer,
T::Array[T.nilable(T.any(String, Integer))]
)
)]
),
body: T.nilable(T.anything),
unwrap: T.nilable(
T.any(
Symbol,
Integer,
T::Array[T.any(Symbol, Integer)],
T.proc.params(arg0: T.anything).returns(T.anything)
)
),
page: T.nilable(T::Class[Imagekitio::Internal::Type::BasePage[Imagekitio::Internal::Type::BaseModel]]),
stream: T.nilable(T::Class[T.anything]),
model: T.nilable(Imagekitio::Internal::Type::Converter::Input),
options: T.nilable(Imagekitio::RequestOptions::OrHash)
}
end
end
define_sorbet_constant!(:RequestInput) do
T.type_alias do
{
method: Symbol,
url: URI::Generic,
headers: T::Hash[String, String],
body: T.anything,
max_retries: Integer,
timeout: Float
}
end
end
end
end
end
end