terminal-shop 1.6.0 → 1.8.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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -1
  3. data/lib/terminal-shop/base_client.rb +36 -46
  4. data/lib/terminal-shop/base_model.rb +88 -128
  5. data/lib/terminal-shop/base_page.rb +18 -18
  6. data/lib/terminal-shop/client.rb +1 -3
  7. data/lib/terminal-shop/errors.rb +12 -17
  8. data/lib/terminal-shop/extern.rb +1 -4
  9. data/lib/terminal-shop/models/region.rb +15 -0
  10. data/lib/terminal-shop/models/subscription.rb +6 -0
  11. data/lib/terminal-shop/models/view_init_response.rb +3 -13
  12. data/lib/terminal-shop/pooled_net_requester.rb +19 -21
  13. data/lib/terminal-shop/request_options.rb +4 -11
  14. data/lib/terminal-shop/resources/address.rb +0 -5
  15. data/lib/terminal-shop/resources/app.rb +0 -5
  16. data/lib/terminal-shop/resources/card.rb +0 -6
  17. data/lib/terminal-shop/resources/cart.rb +0 -7
  18. data/lib/terminal-shop/resources/email.rb +0 -2
  19. data/lib/terminal-shop/resources/order.rb +0 -4
  20. data/lib/terminal-shop/resources/product.rb +0 -3
  21. data/lib/terminal-shop/resources/profile.rb +0 -3
  22. data/lib/terminal-shop/resources/subscription.rb +0 -5
  23. data/lib/terminal-shop/resources/token.rb +0 -5
  24. data/lib/terminal-shop/resources/view.rb +0 -2
  25. data/lib/terminal-shop/util.rb +44 -71
  26. data/lib/terminal-shop/version.rb +1 -1
  27. data/lib/terminal-shop.rb +2 -0
  28. data/manifest.yaml +1 -0
  29. data/rbi/lib/terminal-shop/base_client.rbi +23 -0
  30. data/rbi/lib/terminal-shop/base_model.rbi +178 -52
  31. data/rbi/lib/terminal-shop/base_page.rbi +1 -2
  32. data/rbi/lib/terminal-shop/client.rbi +11 -0
  33. data/rbi/lib/terminal-shop/errors.rbi +33 -0
  34. data/rbi/lib/terminal-shop/extern.rbi +1 -1
  35. data/rbi/lib/terminal-shop/models/address.rbi +10 -0
  36. data/rbi/lib/terminal-shop/models/address_create_params.rbi +8 -0
  37. data/rbi/lib/terminal-shop/models/address_create_response.rbi +1 -0
  38. data/rbi/lib/terminal-shop/models/address_get_response.rbi +1 -0
  39. data/rbi/lib/terminal-shop/models/address_list_response.rbi +1 -0
  40. data/rbi/lib/terminal-shop/models/app.rbi +5 -0
  41. data/rbi/lib/terminal-shop/models/app_create_response.rbi +2 -0
  42. data/rbi/lib/terminal-shop/models/app_get_response.rbi +1 -0
  43. data/rbi/lib/terminal-shop/models/app_list_response.rbi +1 -0
  44. data/rbi/lib/terminal-shop/models/card.rbi +8 -0
  45. data/rbi/lib/terminal-shop/models/card_collect_response.rbi +4 -0
  46. data/rbi/lib/terminal-shop/models/card_create_params.rbi +2 -0
  47. data/rbi/lib/terminal-shop/models/card_create_response.rbi +1 -0
  48. data/rbi/lib/terminal-shop/models/card_get_response.rbi +1 -0
  49. data/rbi/lib/terminal-shop/models/card_list_response.rbi +1 -0
  50. data/rbi/lib/terminal-shop/models/cart.rbi +19 -0
  51. data/rbi/lib/terminal-shop/models/cart_convert_response.rbi +1 -0
  52. data/rbi/lib/terminal-shop/models/cart_get_response.rbi +1 -0
  53. data/rbi/lib/terminal-shop/models/cart_set_address_params.rbi +1 -0
  54. data/rbi/lib/terminal-shop/models/cart_set_card_params.rbi +1 -0
  55. data/rbi/lib/terminal-shop/models/cart_set_item_params.rbi +2 -0
  56. data/rbi/lib/terminal-shop/models/cart_set_item_response.rbi +1 -0
  57. data/rbi/lib/terminal-shop/models/email_create_params.rbi +1 -0
  58. data/rbi/lib/terminal-shop/models/order.rbi +28 -0
  59. data/rbi/lib/terminal-shop/models/order_create_params.rbi +3 -0
  60. data/rbi/lib/terminal-shop/models/order_create_response.rbi +1 -0
  61. data/rbi/lib/terminal-shop/models/order_get_response.rbi +1 -0
  62. data/rbi/lib/terminal-shop/models/order_list_response.rbi +1 -0
  63. data/rbi/lib/terminal-shop/models/product.rbi +12 -6
  64. data/rbi/lib/terminal-shop/models/product_get_response.rbi +1 -0
  65. data/rbi/lib/terminal-shop/models/product_list_response.rbi +1 -0
  66. data/rbi/lib/terminal-shop/models/product_variant.rbi +4 -0
  67. data/rbi/lib/terminal-shop/models/profile.rbi +8 -0
  68. data/rbi/lib/terminal-shop/models/profile_me_response.rbi +1 -0
  69. data/rbi/lib/terminal-shop/models/profile_update_response.rbi +1 -0
  70. data/rbi/lib/terminal-shop/models/region.rbi +15 -0
  71. data/rbi/lib/terminal-shop/models/subscription.rbi +18 -11
  72. data/rbi/lib/terminal-shop/models/subscription_get_response.rbi +1 -0
  73. data/rbi/lib/terminal-shop/models/subscription_list_response.rbi +1 -0
  74. data/rbi/lib/terminal-shop/models/token.rbi +5 -0
  75. data/rbi/lib/terminal-shop/models/token_create_response.rbi +3 -0
  76. data/rbi/lib/terminal-shop/models/token_get_response.rbi +2 -0
  77. data/rbi/lib/terminal-shop/models/token_list_response.rbi +1 -0
  78. data/rbi/lib/terminal-shop/models/view_init_response.rbi +5 -13
  79. data/rbi/lib/terminal-shop/pooled_net_requester.rbi +10 -3
  80. data/rbi/lib/terminal-shop/request_options.rbi +21 -2
  81. data/rbi/lib/terminal-shop/resources/address.rbi +22 -2
  82. data/rbi/lib/terminal-shop/resources/app.rbi +14 -2
  83. data/rbi/lib/terminal-shop/resources/card.rbi +22 -3
  84. data/rbi/lib/terminal-shop/resources/cart.rbi +23 -3
  85. data/rbi/lib/terminal-shop/resources/email.rbi +6 -1
  86. data/rbi/lib/terminal-shop/resources/order.rbi +17 -2
  87. data/rbi/lib/terminal-shop/resources/product.rbi +7 -1
  88. data/rbi/lib/terminal-shop/resources/profile.rbi +2 -0
  89. data/rbi/lib/terminal-shop/resources/subscription.rbi +21 -2
  90. data/rbi/lib/terminal-shop/resources/token.rbi +14 -2
  91. data/rbi/lib/terminal-shop/resources/view.rbi +2 -0
  92. data/rbi/lib/terminal-shop/util.rbi +55 -8
  93. data/rbi/lib/terminal-shop/version.rbi +1 -1
  94. data/sig/terminal-shop/base_client.rbs +6 -1
  95. data/sig/terminal-shop/base_model.rbs +8 -6
  96. data/sig/terminal-shop/errors.rbs +4 -4
  97. data/sig/terminal-shop/models/region.rbs +12 -0
  98. data/sig/terminal-shop/models/subscription.rbs +1 -1
  99. data/sig/terminal-shop/models/view_init_response.rbs +3 -12
  100. data/sig/terminal-shop/pooled_net_requester.rbs +7 -2
  101. data/sig/terminal-shop/util.rbs +4 -4
  102. data/sig/terminal-shop/version.rbs +1 -1
  103. metadata +7 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d27c9b969e5f710ddd5ec412d957b321c294ef271556e868cbcdd6bd7b294d50
4
- data.tar.gz: 218d19b29f7565febb370d1637548988b7ead29a5fb9d95d6626b7289f8aba43
3
+ metadata.gz: 606d2ca50e2cc108dcbc274b0cc1ce406225f2b58b99c38ab143a3123bbd8108
4
+ data.tar.gz: e6905a7f6cc83c547df50855ce3fe79ffbf372c4ce7c038e960e0751e5c9df26
5
5
  SHA512:
6
- metadata.gz: ae88083b381eafbe8668224bfa390bfa003f5156d024a0021266608a446989fe0927200b72e6698d34cd3babcb6c9d6a4c03a5d56ca02374a3adb054d8fae486
7
- data.tar.gz: 4e23af5ad9f4360516ef5e46a8adb635048eec1b201bb90dc7e51933e72c1a3873d66febe5287fe755a497010298053bf888ee992a1914068f5f4c57673597d5
6
+ metadata.gz: 260c58e58cbe1f73ed1aef212cca96d125c740e5ab40b1152f3d1ba5798404ba1093b5f46b1412d3a05a5ea34d7bdd41fa39d72940495380c2232ef1b3af3e5f
7
+ data.tar.gz: 2a7887b08b9703fd060d71a70a41be8fcf3408d83da436d68a1e0e1910a75e8041e80f5aaed27f00e00472691cb4908309b80d438fb4993ebd47de5cd6c6a07d
data/README.md CHANGED
@@ -6,7 +6,7 @@ It is generated with [Stainless](https://www.stainless.com/).
6
6
 
7
7
  ## Documentation
8
8
 
9
- Documentation for the most recent release of this gem can be found [on RubyDoc](https://gemdocs.org/gems/terminal-shop/latest).
9
+ Documentation for released of this gem can be found [on RubyDoc](https://gemdocs.org/gems/terminal-shop).
10
10
 
11
11
  The underlying REST API documentation can be found on [terminal.shop](https://terminal.shop/docs).
12
12
 
@@ -118,6 +118,8 @@ What this means is that while you can use Sorbet to type check your code statica
118
118
 
119
119
  Due to limitations with the Sorbet type system, where a method otherwise can take an instance of `TerminalShop::BaseModel` class, you will need to use the `**` splat operator to pass the arguments:
120
120
 
121
+ Please follow Sorbet's [setup guides](https://sorbet.org/docs/adopting) for best experience.
122
+
121
123
  ```ruby
122
124
  model = ProductListParams.new
123
125
 
@@ -1,10 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TerminalShop
4
- # @private
4
+ # @api private
5
5
  #
6
6
  # @abstract
7
- #
8
7
  class BaseClient
9
8
  # from whatwg fetch spec
10
9
  MAX_REDIRECTS = 20
@@ -21,12 +20,11 @@ module TerminalShop
21
20
  # rubocop:enable Style/MutableConstant
22
21
 
23
22
  class << self
24
- # @private
23
+ # @api private
25
24
  #
26
25
  # @param req [Hash{Symbol=>Object}]
27
26
  #
28
27
  # @raise [ArgumentError]
29
- #
30
28
  def validate!(req)
31
29
  keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :stream, :model, :options]
32
30
  case req
@@ -41,13 +39,12 @@ module TerminalShop
41
39
  end
42
40
  end
43
41
 
44
- # @private
42
+ # @api private
45
43
  #
46
44
  # @param status [Integer]
47
45
  # @param headers [Hash{String=>String}, Net::HTTPHeader]
48
46
  #
49
47
  # @return [Boolean]
50
- #
51
48
  def should_retry?(status, headers:)
52
49
  coerced = TerminalShop::Util.coerce_boolean(headers["x-should-retry"])
53
50
  case [coerced, status]
@@ -65,7 +62,7 @@ module TerminalShop
65
62
  end
66
63
  end
67
64
 
68
- # @private
65
+ # @api private
69
66
  #
70
67
  # @param request [Hash{Symbol=>Object}] .
71
68
  #
@@ -86,7 +83,6 @@ module TerminalShop
86
83
  # @param response_headers [Hash{String=>String}, Net::HTTPHeader]
87
84
  #
88
85
  # @return [Hash{Symbol=>Object}]
89
- #
90
86
  def follow_redirect(request, status:, response_headers:)
91
87
  method, url, headers = request.fetch_values(:method, :url, :headers)
92
88
  location =
@@ -128,14 +124,27 @@ module TerminalShop
128
124
 
129
125
  request
130
126
  end
127
+
128
+ # @api private
129
+ #
130
+ # @param status [Integer, TerminalShop::APIConnectionError]
131
+ # @param stream [Enumerable, nil]
132
+ def reap_connection!(status, stream:)
133
+ case status
134
+ in (..199) | (300..499)
135
+ stream&.each { next }
136
+ in TerminalShop::APIConnectionError | (500..)
137
+ TerminalShop::Util.close_fused!(stream)
138
+ else
139
+ end
140
+ end
131
141
  end
132
142
 
133
- # @private
134
- #
143
+ # @api private
135
144
  # @return [TerminalShop::PooledNetRequester]
136
145
  attr_accessor :requester
137
146
 
138
- # @private
147
+ # @api private
139
148
  #
140
149
  # @param base_url [String]
141
150
  # @param timeout [Float]
@@ -144,7 +153,6 @@ module TerminalShop
144
153
  # @param max_retry_delay [Float]
145
154
  # @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}]
146
155
  # @param idempotency_header [String, nil]
147
- #
148
156
  def initialize(
149
157
  base_url:,
150
158
  timeout: 0.0,
@@ -171,19 +179,17 @@ module TerminalShop
171
179
  @max_retry_delay = max_retry_delay
172
180
  end
173
181
 
174
- # @private
182
+ # @api private
175
183
  #
176
184
  # @return [Hash{String=>String}]
177
- #
178
185
  private def auth_headers = {}
179
186
 
180
- # @private
187
+ # @api private
181
188
  #
182
189
  # @return [String]
183
- #
184
190
  private def generate_idempotency_key = "stainless-ruby-retry-#{SecureRandom.uuid}"
185
191
 
186
- # @private
192
+ # @api private
187
193
  #
188
194
  # @param req [Hash{Symbol=>Object}] .
189
195
  #
@@ -220,7 +226,6 @@ module TerminalShop
220
226
  # @option opts [Float, nil] :timeout
221
227
  #
222
228
  # @return [Hash{Symbol=>Object}]
223
- #
224
229
  private def build_request(req, opts)
225
230
  method, uninterpolated_path = req.fetch_values(:method, :path)
226
231
 
@@ -271,13 +276,12 @@ module TerminalShop
271
276
  }
272
277
  end
273
278
 
274
- # @private
279
+ # @api private
275
280
  #
276
281
  # @param headers [Hash{String=>String}]
277
282
  # @param retry_count [Integer]
278
283
  #
279
284
  # @return [Float]
280
- #
281
285
  private def retry_delay(headers, retry_count:)
282
286
  # Non-standard extension
283
287
  span = Float(headers["retry-after-ms"], exception: false)&.then { _1 / 1000 }
@@ -298,7 +302,7 @@ module TerminalShop
298
302
  (@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
299
303
  end
300
304
 
301
- # @private
305
+ # @api private
302
306
  #
303
307
  # @param request [Hash{Symbol=>Object}] .
304
308
  #
@@ -322,7 +326,6 @@ module TerminalShop
322
326
  #
323
327
  # @raise [TerminalShop::APIError]
324
328
  # @return [Array(Integer, Net::HTTPResponse, Enumerable)]
325
- #
326
329
  private def send_request(request, redirect_count:, retry_count:, send_retry_header:)
327
330
  url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
328
331
  input = {**request.except(:timeout), deadline: TerminalShop::Util.monotonic_secs + timeout}
@@ -332,28 +335,23 @@ module TerminalShop
332
335
  end
333
336
 
334
337
  begin
335
- response, stream = @requester.execute(input)
336
- status = Integer(response.code)
338
+ status, response, stream = @requester.execute(input)
337
339
  rescue TerminalShop::APIConnectionError => e
338
340
  status = e
339
341
  end
340
342
 
341
- # normally we want to drain the response body and reuse the HTTP session by clearing the socket buffers
342
- # unless we hit a server error
343
- srv_fault = (500...).include?(status)
344
-
345
343
  case status
346
344
  in ..299
347
345
  [status, response, stream]
348
346
  in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
349
- message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
347
+ self.class.reap_connection!(status, stream: stream)
350
348
 
351
- stream.each { next }
349
+ message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
352
350
  raise TerminalShop::APIConnectionError.new(url: url, message: message)
353
351
  in 300..399
354
- request = self.class.follow_redirect(request, status: status, response_headers: response)
352
+ self.class.reap_connection!(status, stream: stream)
355
353
 
356
- stream.each { next }
354
+ request = self.class.follow_redirect(request, status: status, response_headers: response)
357
355
  send_request(
358
356
  request,
359
357
  redirect_count: redirect_count + 1,
@@ -363,12 +361,10 @@ module TerminalShop
363
361
  in TerminalShop::APIConnectionError if retry_count >= max_retries
364
362
  raise status
365
363
  in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: response)
366
- decoded = TerminalShop::Util.decode_content(response, stream: stream, suppress_error: true)
367
-
368
- if srv_fault
369
- TerminalShop::Util.close_fused!(stream)
370
- else
371
- stream.each { next }
364
+ decoded = Kernel.then do
365
+ TerminalShop::Util.decode_content(response, stream: stream, suppress_error: true)
366
+ ensure
367
+ self.class.reap_connection!(status, stream: stream)
372
368
  end
373
369
 
374
370
  raise TerminalShop::APIStatusError.for(
@@ -379,13 +375,9 @@ module TerminalShop
379
375
  response: response
380
376
  )
381
377
  in (400..) | TerminalShop::APIConnectionError
382
- delay = retry_delay(response, retry_count: retry_count)
378
+ self.class.reap_connection!(status, stream: stream)
383
379
 
384
- if srv_fault
385
- TerminalShop::Util.close_fused!(stream)
386
- else
387
- stream&.each { next }
388
- end
380
+ delay = retry_delay(response, retry_count: retry_count)
389
381
  sleep(delay)
390
382
 
391
383
  send_request(
@@ -424,7 +416,6 @@ module TerminalShop
424
416
  #
425
417
  # @raise [TerminalShop::APIError]
426
418
  # @return [Object]
427
- #
428
419
  def request(req)
429
420
  self.class.validate!(req)
430
421
  model = req.fetch(:model) { TerminalShop::Unknown }
@@ -455,7 +446,6 @@ module TerminalShop
455
446
  end
456
447
 
457
448
  # @return [String]
458
- #
459
449
  def inspect
460
450
  # rubocop:disable Layout/LineLength
461
451
  base_url = TerminalShop::Util.unparse_uri(@base_url)