terminal-shop 1.7.0 → 2.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 +4 -4
- data/README.md +17 -1
- data/lib/terminal-shop/base_client.rb +47 -56
- data/lib/terminal-shop/base_model.rb +300 -352
- data/lib/terminal-shop/base_page.rb +30 -31
- data/lib/terminal-shop/client.rb +2 -3
- data/lib/terminal-shop/errors.rb +12 -17
- data/lib/terminal-shop/extern.rb +1 -4
- data/lib/terminal-shop/models/product.rb +7 -3
- data/lib/terminal-shop/models/region.rb +7 -3
- data/lib/terminal-shop/models/subscription.rb +7 -3
- data/lib/terminal-shop/pooled_net_requester.rb +30 -21
- data/lib/terminal-shop/request_options.rb +5 -12
- data/lib/terminal-shop/resources/address.rb +0 -5
- data/lib/terminal-shop/resources/app.rb +0 -5
- data/lib/terminal-shop/resources/card.rb +0 -6
- data/lib/terminal-shop/resources/cart.rb +0 -7
- data/lib/terminal-shop/resources/email.rb +0 -2
- data/lib/terminal-shop/resources/order.rb +0 -4
- data/lib/terminal-shop/resources/product.rb +0 -3
- data/lib/terminal-shop/resources/profile.rb +0 -3
- data/lib/terminal-shop/resources/subscription.rb +0 -5
- data/lib/terminal-shop/resources/token.rb +0 -5
- data/lib/terminal-shop/resources/view.rb +0 -2
- data/lib/terminal-shop/util.rb +59 -77
- data/lib/terminal-shop/version.rb +1 -1
- data/lib/terminal-shop.rb +16 -0
- data/manifest.yaml +1 -0
- data/rbi/lib/terminal-shop/base_client.rbi +63 -41
- data/rbi/lib/terminal-shop/base_model.rbi +278 -129
- data/rbi/lib/terminal-shop/base_page.rbi +1 -4
- data/rbi/lib/terminal-shop/client.rbi +29 -30
- data/rbi/lib/terminal-shop/errors.rbi +12 -14
- data/rbi/lib/terminal-shop/extern.rbi +1 -1
- data/rbi/lib/terminal-shop/models/address.rbi +25 -51
- data/rbi/lib/terminal-shop/models/address_create_params.rbi +23 -46
- data/rbi/lib/terminal-shop/models/address_create_response.rbi +2 -6
- data/rbi/lib/terminal-shop/models/address_delete_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/address_delete_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/address_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/address_get_response.rbi +8 -6
- data/rbi/lib/terminal-shop/models/address_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/address_list_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/app.rbi +9 -24
- data/rbi/lib/terminal-shop/models/app_create_params.rbi +3 -13
- data/rbi/lib/terminal-shop/models/app_create_response.rbi +10 -20
- data/rbi/lib/terminal-shop/models/app_delete_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/app_delete_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/app_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/app_get_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/app_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/app_list_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/card.rbi +22 -36
- data/rbi/lib/terminal-shop/models/card_collect_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/card_collect_response.rbi +11 -14
- data/rbi/lib/terminal-shop/models/card_create_params.rbi +4 -7
- data/rbi/lib/terminal-shop/models/card_create_response.rbi +2 -6
- data/rbi/lib/terminal-shop/models/card_delete_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/card_delete_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/card_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/card_get_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/card_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/card_list_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/cart.rbi +53 -88
- data/rbi/lib/terminal-shop/models/cart_clear_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/cart_clear_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/cart_convert_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/cart_convert_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/cart_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/cart_get_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/cart_set_address_params.rbi +3 -7
- data/rbi/lib/terminal-shop/models/cart_set_address_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/cart_set_card_params.rbi +3 -7
- data/rbi/lib/terminal-shop/models/cart_set_card_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/cart_set_item_params.rbi +5 -13
- data/rbi/lib/terminal-shop/models/cart_set_item_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/email_create_params.rbi +3 -7
- data/rbi/lib/terminal-shop/models/email_create_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/order.rbi +80 -139
- data/rbi/lib/terminal-shop/models/order_create_params.rbi +7 -19
- data/rbi/lib/terminal-shop/models/order_create_response.rbi +2 -6
- data/rbi/lib/terminal-shop/models/order_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/order_get_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/order_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/order_list_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/product.rbi +52 -80
- data/rbi/lib/terminal-shop/models/product_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/product_get_response.rbi +8 -6
- data/rbi/lib/terminal-shop/models/product_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/product_list_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/product_variant.rbi +7 -18
- data/rbi/lib/terminal-shop/models/profile.rbi +20 -36
- data/rbi/lib/terminal-shop/models/profile_me_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/profile_me_response.rbi +8 -6
- data/rbi/lib/terminal-shop/models/profile_update_params.rbi +3 -13
- data/rbi/lib/terminal-shop/models/profile_update_response.rbi +8 -6
- data/rbi/lib/terminal-shop/models/region.rbi +10 -8
- data/rbi/lib/terminal-shop/models/subscription.rbi +35 -75
- data/rbi/lib/terminal-shop/models/subscription_create_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/subscription_create_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/subscription_delete_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/subscription_delete_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/subscription_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/subscription_get_response.rbi +8 -6
- data/rbi/lib/terminal-shop/models/subscription_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/subscription_list_response.rbi +4 -8
- data/rbi/lib/terminal-shop/models/token.rbi +8 -18
- data/rbi/lib/terminal-shop/models/token_create_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/token_create_response.rbi +11 -20
- data/rbi/lib/terminal-shop/models/token_delete_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/token_delete_response.rbi +1 -6
- data/rbi/lib/terminal-shop/models/token_get_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/token_get_response.rbi +6 -6
- data/rbi/lib/terminal-shop/models/token_list_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/token_list_response.rbi +5 -6
- data/rbi/lib/terminal-shop/models/view_init_params.rbi +1 -1
- data/rbi/lib/terminal-shop/models/view_init_response.rbi +37 -81
- data/rbi/lib/terminal-shop/pooled_net_requester.rbi +24 -6
- data/rbi/lib/terminal-shop/request_options.rbi +32 -57
- data/rbi/lib/terminal-shop/resources/address.rbi +26 -6
- data/rbi/lib/terminal-shop/resources/app.rbi +18 -6
- data/rbi/lib/terminal-shop/resources/card.rbi +27 -8
- data/rbi/lib/terminal-shop/resources/cart.rbi +29 -9
- data/rbi/lib/terminal-shop/resources/email.rbi +7 -2
- data/rbi/lib/terminal-shop/resources/order.rbi +20 -5
- data/rbi/lib/terminal-shop/resources/product.rbi +9 -3
- data/rbi/lib/terminal-shop/resources/profile.rbi +4 -2
- data/rbi/lib/terminal-shop/resources/subscription.rbi +26 -6
- data/rbi/lib/terminal-shop/resources/token.rbi +18 -6
- data/rbi/lib/terminal-shop/resources/view.rbi +3 -1
- data/rbi/lib/terminal-shop/util.rbi +78 -20
- data/rbi/lib/terminal-shop/version.rbi +1 -1
- data/sig/terminal-shop/base_client.rbs +20 -15
- data/sig/terminal-shop/base_model.rbs +36 -18
- data/sig/terminal-shop/client.rbs +8 -8
- data/sig/terminal-shop/errors.rbs +21 -21
- data/sig/terminal-shop/models/address.rbs +11 -15
- data/sig/terminal-shop/models/address_create_params.rbs +11 -16
- data/sig/terminal-shop/models/address_create_response.rbs +1 -6
- data/sig/terminal-shop/models/address_delete_params.rbs +1 -6
- data/sig/terminal-shop/models/address_delete_response.rbs +1 -6
- data/sig/terminal-shop/models/address_get_params.rbs +1 -6
- data/sig/terminal-shop/models/address_get_response.rbs +1 -6
- data/sig/terminal-shop/models/address_list_params.rbs +1 -6
- data/sig/terminal-shop/models/address_list_response.rbs +1 -6
- data/sig/terminal-shop/models/app.rbs +6 -5
- data/sig/terminal-shop/models/app_create_params.rbs +5 -10
- data/sig/terminal-shop/models/app_create_response.rbs +4 -12
- data/sig/terminal-shop/models/app_delete_params.rbs +1 -6
- data/sig/terminal-shop/models/app_delete_response.rbs +1 -6
- data/sig/terminal-shop/models/app_get_params.rbs +1 -5
- data/sig/terminal-shop/models/app_get_response.rbs +1 -5
- data/sig/terminal-shop/models/app_list_params.rbs +1 -5
- data/sig/terminal-shop/models/app_list_response.rbs +1 -6
- data/sig/terminal-shop/models/card.rbs +7 -16
- data/sig/terminal-shop/models/card_collect_params.rbs +1 -6
- data/sig/terminal-shop/models/card_collect_response.rbs +4 -12
- data/sig/terminal-shop/models/card_create_params.rbs +4 -6
- data/sig/terminal-shop/models/card_create_response.rbs +1 -6
- data/sig/terminal-shop/models/card_delete_params.rbs +1 -6
- data/sig/terminal-shop/models/card_delete_response.rbs +1 -6
- data/sig/terminal-shop/models/card_get_params.rbs +1 -5
- data/sig/terminal-shop/models/card_get_response.rbs +1 -6
- data/sig/terminal-shop/models/card_list_params.rbs +1 -5
- data/sig/terminal-shop/models/card_list_response.rbs +1 -6
- data/sig/terminal-shop/models/cart.rbs +20 -34
- data/sig/terminal-shop/models/cart_clear_params.rbs +1 -6
- data/sig/terminal-shop/models/cart_clear_response.rbs +1 -6
- data/sig/terminal-shop/models/cart_convert_params.rbs +1 -6
- data/sig/terminal-shop/models/cart_convert_response.rbs +1 -6
- data/sig/terminal-shop/models/cart_get_params.rbs +1 -5
- data/sig/terminal-shop/models/cart_get_response.rbs +1 -6
- data/sig/terminal-shop/models/cart_set_address_params.rbs +4 -9
- data/sig/terminal-shop/models/cart_set_address_response.rbs +1 -6
- data/sig/terminal-shop/models/cart_set_card_params.rbs +4 -6
- data/sig/terminal-shop/models/cart_set_card_response.rbs +1 -6
- data/sig/terminal-shop/models/cart_set_item_params.rbs +5 -10
- data/sig/terminal-shop/models/cart_set_item_response.rbs +1 -6
- data/sig/terminal-shop/models/email_create_params.rbs +4 -6
- data/sig/terminal-shop/models/email_create_response.rbs +1 -6
- data/sig/terminal-shop/models/order.rbs +31 -50
- data/sig/terminal-shop/models/order_create_params.rbs +6 -11
- data/sig/terminal-shop/models/order_create_response.rbs +1 -6
- data/sig/terminal-shop/models/order_get_params.rbs +1 -5
- data/sig/terminal-shop/models/order_get_response.rbs +1 -6
- data/sig/terminal-shop/models/order_list_params.rbs +1 -6
- data/sig/terminal-shop/models/order_list_response.rbs +1 -6
- data/sig/terminal-shop/models/product.rbs +20 -27
- data/sig/terminal-shop/models/product_get_params.rbs +1 -6
- data/sig/terminal-shop/models/product_get_response.rbs +1 -6
- data/sig/terminal-shop/models/product_list_params.rbs +1 -6
- data/sig/terminal-shop/models/product_list_response.rbs +1 -6
- data/sig/terminal-shop/models/product_variant.rbs +1 -5
- data/sig/terminal-shop/models/profile.rbs +8 -17
- data/sig/terminal-shop/models/profile_me_params.rbs +1 -6
- data/sig/terminal-shop/models/profile_me_response.rbs +1 -6
- data/sig/terminal-shop/models/profile_update_params.rbs +5 -10
- data/sig/terminal-shop/models/profile_update_response.rbs +1 -6
- data/sig/terminal-shop/models/region.rbs +4 -2
- data/sig/terminal-shop/models/subscription.rbs +15 -27
- data/sig/terminal-shop/models/subscription_create_params.rbs +1 -6
- data/sig/terminal-shop/models/subscription_create_response.rbs +1 -6
- data/sig/terminal-shop/models/subscription_delete_params.rbs +1 -6
- data/sig/terminal-shop/models/subscription_delete_response.rbs +1 -6
- data/sig/terminal-shop/models/subscription_get_params.rbs +1 -6
- data/sig/terminal-shop/models/subscription_get_response.rbs +1 -6
- data/sig/terminal-shop/models/subscription_list_params.rbs +1 -6
- data/sig/terminal-shop/models/subscription_list_response.rbs +3 -6
- data/sig/terminal-shop/models/token.rbs +1 -5
- data/sig/terminal-shop/models/token_create_params.rbs +1 -6
- data/sig/terminal-shop/models/token_create_response.rbs +4 -12
- data/sig/terminal-shop/models/token_delete_params.rbs +1 -6
- data/sig/terminal-shop/models/token_delete_response.rbs +1 -6
- data/sig/terminal-shop/models/token_get_params.rbs +1 -5
- data/sig/terminal-shop/models/token_get_response.rbs +1 -6
- data/sig/terminal-shop/models/token_list_params.rbs +1 -6
- data/sig/terminal-shop/models/token_list_response.rbs +1 -6
- data/sig/terminal-shop/models/view_init_params.rbs +1 -5
- data/sig/terminal-shop/models/view_init_response.rbs +15 -23
- data/sig/terminal-shop/pooled_net_requester.rbs +10 -3
- data/sig/terminal-shop/request_options.rbs +2 -2
- data/sig/terminal-shop/resources/address.rbs +22 -41
- data/sig/terminal-shop/resources/app.rbs +16 -34
- data/sig/terminal-shop/resources/card.rbs +18 -40
- data/sig/terminal-shop/resources/cart.rbs +22 -47
- data/sig/terminal-shop/resources/email.rbs +4 -8
- data/sig/terminal-shop/resources/order.rbs +13 -26
- data/sig/terminal-shop/resources/product.rbs +7 -16
- data/sig/terminal-shop/resources/profile.rbs +8 -16
- data/sig/terminal-shop/resources/subscription.rbs +21 -43
- data/sig/terminal-shop/resources/token.rbs +14 -32
- data/sig/terminal-shop/resources/view.rbs +3 -7
- data/sig/terminal-shop/util.rbs +9 -9
- data/sig/terminal-shop/version.rbs +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd8072a075bff0dc001f6aaabb37415cada6dfc11b82cb2362fd78400ef2fd69
|
4
|
+
data.tar.gz: 69a0df094ac931a412378137fe036c50b11d36ae628fe526e7392c985ef79466
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b29c0d30aa9ec41963e5aa21d24e2821741868cb591fbec9fd1e015eef059ec06ae2adda746de1c2584b2b20e8babd6382058d85ae89e6948e4998b484c0042a
|
7
|
+
data.tar.gz: cb9eb1e052ec4b94b46523bc698b689fc425fd6ff66b036337138bdfa59108acddcf59ce088876db204eb61284e81643a825ddf43f0d74835c28f52000aef7f9
|
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
|
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
|
|
@@ -112,18 +112,34 @@ terminal.product.list(request_options: {timeout: 5})
|
|
112
112
|
|
113
113
|
## Sorbet Support
|
114
114
|
|
115
|
+
**This library emits an intentional warning under the [`tapioca` toolchain](https://github.com/Shopify/tapioca)**. This is normal, and does not impact functionality.
|
116
|
+
|
115
117
|
This library is written with [Sorbet type definitions](https://sorbet.org/docs/rbi). However, there is no runtime dependency on the `sorbet-runtime`.
|
116
118
|
|
117
119
|
What this means is that while you can use Sorbet to type check your code statically, and benefit from the [Sorbet Language Server](https://sorbet.org/docs/lsp) in your editor, there is no runtime type checking and execution overhead from Sorbet itself.
|
118
120
|
|
119
121
|
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
122
|
|
123
|
+
Please follow Sorbet's [setup guides](https://sorbet.org/docs/adopting) for best experience.
|
124
|
+
|
121
125
|
```ruby
|
122
126
|
model = ProductListParams.new
|
123
127
|
|
124
128
|
terminal.product.list(**model)
|
125
129
|
```
|
126
130
|
|
131
|
+
## Advanced
|
132
|
+
|
133
|
+
### Concurrency & Connection Pooling
|
134
|
+
|
135
|
+
The `TerminalShop::Client` instances are thread-safe, and should be re-used across multiple threads. By default, each `Client` have their own HTTP connection pool, with a maximum number of connections equal to thread count.
|
136
|
+
|
137
|
+
When the maximum number of connections has been checked out from the connection pool, the `Client` will wait for an in use connection to become available. The queue time for this mechanism is accounted for by the per-request timeout.
|
138
|
+
|
139
|
+
Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
|
140
|
+
|
141
|
+
Currently, `TerminalShop::Client` instances are only fork-safe if there are no in-flight HTTP requests.
|
142
|
+
|
127
143
|
## Versioning
|
128
144
|
|
129
145
|
This package follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions. As the library is in initial development and has a major version of `0`, APIs may change at any time.
|
@@ -1,32 +1,31 @@
|
|
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
|
11
10
|
|
12
11
|
# rubocop:disable Style/MutableConstant
|
13
|
-
PLATFORM_HEADERS =
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
PLATFORM_HEADERS =
|
13
|
+
{
|
14
|
+
"x-stainless-arch" => TerminalShop::Util.arch,
|
15
|
+
"x-stainless-lang" => "ruby",
|
16
|
+
"x-stainless-os" => TerminalShop::Util.os,
|
17
|
+
"x-stainless-package-version" => TerminalShop::VERSION,
|
18
|
+
"x-stainless-runtime" => ::RUBY_ENGINE,
|
19
|
+
"x-stainless-runtime-version" => ::RUBY_ENGINE_VERSION
|
20
|
+
}
|
21
21
|
# rubocop:enable Style/MutableConstant
|
22
22
|
|
23
23
|
class << self
|
24
|
-
# @private
|
24
|
+
# @api private
|
25
25
|
#
|
26
26
|
# @param req [Hash{Symbol=>Object}]
|
27
27
|
#
|
28
28
|
# @raise [ArgumentError]
|
29
|
-
#
|
30
29
|
def validate!(req)
|
31
30
|
keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :stream, :model, :options]
|
32
31
|
case req
|
@@ -41,13 +40,12 @@ module TerminalShop
|
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
44
|
-
# @private
|
43
|
+
# @api private
|
45
44
|
#
|
46
45
|
# @param status [Integer]
|
47
46
|
# @param headers [Hash{String=>String}, Net::HTTPHeader]
|
48
47
|
#
|
49
48
|
# @return [Boolean]
|
50
|
-
#
|
51
49
|
def should_retry?(status, headers:)
|
52
50
|
coerced = TerminalShop::Util.coerce_boolean(headers["x-should-retry"])
|
53
51
|
case [coerced, status]
|
@@ -65,7 +63,7 @@ module TerminalShop
|
|
65
63
|
end
|
66
64
|
end
|
67
65
|
|
68
|
-
# @private
|
66
|
+
# @api private
|
69
67
|
#
|
70
68
|
# @param request [Hash{Symbol=>Object}] .
|
71
69
|
#
|
@@ -86,7 +84,6 @@ module TerminalShop
|
|
86
84
|
# @param response_headers [Hash{String=>String}, Net::HTTPHeader]
|
87
85
|
#
|
88
86
|
# @return [Hash{Symbol=>Object}]
|
89
|
-
#
|
90
87
|
def follow_redirect(request, status:, response_headers:)
|
91
88
|
method, url, headers = request.fetch_values(:method, :url, :headers)
|
92
89
|
location =
|
@@ -128,14 +125,27 @@ module TerminalShop
|
|
128
125
|
|
129
126
|
request
|
130
127
|
end
|
128
|
+
|
129
|
+
# @api private
|
130
|
+
#
|
131
|
+
# @param status [Integer, TerminalShop::APIConnectionError]
|
132
|
+
# @param stream [Enumerable, nil]
|
133
|
+
def reap_connection!(status, stream:)
|
134
|
+
case status
|
135
|
+
in (..199) | (300..499)
|
136
|
+
stream&.each { next }
|
137
|
+
in TerminalShop::APIConnectionError | (500..)
|
138
|
+
TerminalShop::Util.close_fused!(stream)
|
139
|
+
else
|
140
|
+
end
|
141
|
+
end
|
131
142
|
end
|
132
143
|
|
133
|
-
# @private
|
134
|
-
#
|
144
|
+
# @api private
|
135
145
|
# @return [TerminalShop::PooledNetRequester]
|
136
146
|
attr_accessor :requester
|
137
147
|
|
138
|
-
# @private
|
148
|
+
# @api private
|
139
149
|
#
|
140
150
|
# @param base_url [String]
|
141
151
|
# @param timeout [Float]
|
@@ -144,7 +154,6 @@ module TerminalShop
|
|
144
154
|
# @param max_retry_delay [Float]
|
145
155
|
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}]
|
146
156
|
# @param idempotency_header [String, nil]
|
147
|
-
#
|
148
157
|
def initialize(
|
149
158
|
base_url:,
|
150
159
|
timeout: 0.0,
|
@@ -171,19 +180,17 @@ module TerminalShop
|
|
171
180
|
@max_retry_delay = max_retry_delay
|
172
181
|
end
|
173
182
|
|
174
|
-
# @private
|
183
|
+
# @api private
|
175
184
|
#
|
176
185
|
# @return [Hash{String=>String}]
|
177
|
-
#
|
178
186
|
private def auth_headers = {}
|
179
187
|
|
180
|
-
# @private
|
188
|
+
# @api private
|
181
189
|
#
|
182
190
|
# @return [String]
|
183
|
-
#
|
184
191
|
private def generate_idempotency_key = "stainless-ruby-retry-#{SecureRandom.uuid}"
|
185
192
|
|
186
|
-
# @private
|
193
|
+
# @api private
|
187
194
|
#
|
188
195
|
# @param req [Hash{Symbol=>Object}] .
|
189
196
|
#
|
@@ -213,14 +220,13 @@ module TerminalShop
|
|
213
220
|
#
|
214
221
|
# @option opts [Hash{String=>String, nil}, nil] :extra_headers
|
215
222
|
#
|
216
|
-
# @option opts [
|
223
|
+
# @option opts [Object, nil] :extra_body
|
217
224
|
#
|
218
225
|
# @option opts [Integer, nil] :max_retries
|
219
226
|
#
|
220
227
|
# @option opts [Float, nil] :timeout
|
221
228
|
#
|
222
229
|
# @return [Hash{Symbol=>Object}]
|
223
|
-
#
|
224
230
|
private def build_request(req, opts)
|
225
231
|
method, uninterpolated_path = req.fetch_values(:method, :path)
|
226
232
|
|
@@ -271,13 +277,12 @@ module TerminalShop
|
|
271
277
|
}
|
272
278
|
end
|
273
279
|
|
274
|
-
# @private
|
280
|
+
# @api private
|
275
281
|
#
|
276
282
|
# @param headers [Hash{String=>String}]
|
277
283
|
# @param retry_count [Integer]
|
278
284
|
#
|
279
285
|
# @return [Float]
|
280
|
-
#
|
281
286
|
private def retry_delay(headers, retry_count:)
|
282
287
|
# Non-standard extension
|
283
288
|
span = Float(headers["retry-after-ms"], exception: false)&.then { _1 / 1000 }
|
@@ -298,7 +303,7 @@ module TerminalShop
|
|
298
303
|
(@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
|
299
304
|
end
|
300
305
|
|
301
|
-
# @private
|
306
|
+
# @api private
|
302
307
|
#
|
303
308
|
# @param request [Hash{Symbol=>Object}] .
|
304
309
|
#
|
@@ -322,7 +327,6 @@ module TerminalShop
|
|
322
327
|
#
|
323
328
|
# @raise [TerminalShop::APIError]
|
324
329
|
# @return [Array(Integer, Net::HTTPResponse, Enumerable)]
|
325
|
-
#
|
326
330
|
private def send_request(request, redirect_count:, retry_count:, send_retry_header:)
|
327
331
|
url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
|
328
332
|
input = {**request.except(:timeout), deadline: TerminalShop::Util.monotonic_secs + timeout}
|
@@ -332,28 +336,23 @@ module TerminalShop
|
|
332
336
|
end
|
333
337
|
|
334
338
|
begin
|
335
|
-
response, stream = @requester.execute(input)
|
336
|
-
status = Integer(response.code)
|
339
|
+
status, response, stream = @requester.execute(input)
|
337
340
|
rescue TerminalShop::APIConnectionError => e
|
338
341
|
status = e
|
339
342
|
end
|
340
343
|
|
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
344
|
case status
|
346
345
|
in ..299
|
347
346
|
[status, response, stream]
|
348
347
|
in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
|
349
|
-
|
348
|
+
self.class.reap_connection!(status, stream: stream)
|
350
349
|
|
351
|
-
|
350
|
+
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
|
352
351
|
raise TerminalShop::APIConnectionError.new(url: url, message: message)
|
353
352
|
in 300..399
|
354
|
-
|
353
|
+
self.class.reap_connection!(status, stream: stream)
|
355
354
|
|
356
|
-
|
355
|
+
request = self.class.follow_redirect(request, status: status, response_headers: response)
|
357
356
|
send_request(
|
358
357
|
request,
|
359
358
|
redirect_count: redirect_count + 1,
|
@@ -363,12 +362,10 @@ module TerminalShop
|
|
363
362
|
in TerminalShop::APIConnectionError if retry_count >= max_retries
|
364
363
|
raise status
|
365
364
|
in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: response)
|
366
|
-
decoded =
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
else
|
371
|
-
stream.each { next }
|
365
|
+
decoded = Kernel.then do
|
366
|
+
TerminalShop::Util.decode_content(response, stream: stream, suppress_error: true)
|
367
|
+
ensure
|
368
|
+
self.class.reap_connection!(status, stream: stream)
|
372
369
|
end
|
373
370
|
|
374
371
|
raise TerminalShop::APIStatusError.for(
|
@@ -379,13 +376,9 @@ module TerminalShop
|
|
379
376
|
response: response
|
380
377
|
)
|
381
378
|
in (400..) | TerminalShop::APIConnectionError
|
382
|
-
|
379
|
+
self.class.reap_connection!(status, stream: stream)
|
383
380
|
|
384
|
-
|
385
|
-
TerminalShop::Util.close_fused!(stream)
|
386
|
-
else
|
387
|
-
stream&.each { next }
|
388
|
-
end
|
381
|
+
delay = retry_delay(response, retry_count: retry_count)
|
389
382
|
sleep(delay)
|
390
383
|
|
391
384
|
send_request(
|
@@ -424,7 +417,6 @@ module TerminalShop
|
|
424
417
|
#
|
425
418
|
# @raise [TerminalShop::APIError]
|
426
419
|
# @return [Object]
|
427
|
-
#
|
428
420
|
def request(req)
|
429
421
|
self.class.validate!(req)
|
430
422
|
model = req.fetch(:model) { TerminalShop::Unknown }
|
@@ -445,7 +437,7 @@ module TerminalShop
|
|
445
437
|
decoded = TerminalShop::Util.decode_content(response, stream: stream)
|
446
438
|
case req
|
447
439
|
in { stream: Class => st }
|
448
|
-
st.new(model: model, url: url, status: status, response: response,
|
440
|
+
st.new(model: model, url: url, status: status, response: response, stream: decoded)
|
449
441
|
in { page: Class => page }
|
450
442
|
page.new(client: self, req: req, headers: response, page_data: decoded)
|
451
443
|
else
|
@@ -455,7 +447,6 @@ module TerminalShop
|
|
455
447
|
end
|
456
448
|
|
457
449
|
# @return [String]
|
458
|
-
#
|
459
450
|
def inspect
|
460
451
|
# rubocop:disable Layout/LineLength
|
461
452
|
base_url = TerminalShop::Util.unparse_uri(@base_url)
|