Skip to content

Commit

Permalink
Merge pull request #1395 from stripe/latest-codegen-beta
Browse files Browse the repository at this point in the history
Update generated code for beta
  • Loading branch information
stripe-openapi[bot] committed May 9, 2024
2 parents 65c85f4 + 5669ab4 commit 6a4d2d1
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
## 11.4.0-beta.1 - 2024-05-02
* [#1386](https://github.com/stripe/stripe-ruby/pull/1386) Update generated code for beta

## 11.3.0 - 2024-05-02
* [#1387](https://github.com/stripe/stripe-ruby/pull/1387) Update generated code

* [#1392](https://github.com/stripe/stripe-ruby/pull/1392) Deprecate Ruby methods based on OpenAPI spec
- Mark as deprecated the `approve` and `decline` methods in `lib/stripe/resources/issuing/authorization.rb`. Instead, [respond directly to the webhook request to approve an authorization](https://stripe.com/docs/issuing/controls/real-time-authorizations#authorization-handling).
* [#1391](https://github.com/stripe/stripe-ruby/pull/1391) Add Ruby 3.3 to CI test matrix

## 11.3.0-beta.1 - 2024-04-18
* [#1383](https://github.com/stripe/stripe-ruby/pull/1383) Update generated code for beta
Expand Down
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ group :development do
# The latest version of rubocop is only compatible with Ruby 2.7+
gem "rubocop", "1.57.2" if RUBY_VERSION >= "2.7"

# jaro_winkler 1.5.5 installation fails for jruby
# don't install on truffleruby
gem "jaro_winkler", "1.5.4" unless RUBY_ENGINE == "truffleruby"

gem "sorbet"
gem "tapioca"

Expand Down
2 changes: 1 addition & 1 deletion OPENAPI_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1005
v1016
24 changes: 22 additions & 2 deletions lib/stripe/api_operations/nested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
end
end

# rubocop:disable Metrics/MethodLength
private def define_operation(
resource,
operation,
Expand All @@ -53,12 +54,30 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
)
end
when :retrieve
# TODO: (Major) Split params_or_opts to params and opts and get rid of the complicated way to add params
define_singleton_method(:"retrieve_#{resource}") \
do |id, nested_id, opts = {}|
do |id, nested_id, params_or_opts = {}, definitely_opts = nil|
opts = nil
params = nil
if definitely_opts.nil?
unrecognized_key = params_or_opts.keys.find { |k| !Util::OPTS_USER_SPECIFIED.include?(k) }
if unrecognized_key
raise ArgumentError,
"Unrecognized request option: #{unrecognized_key}. Did you mean to specify this as " \
"retrieve params? " \
"If so, you must explicitly pass an opts hash as a fourth argument. " \
"For example: .retrieve(#{id}, #{nested_id}, {#{unrecognized_key}: 'foo'}, {})"
end

opts = params_or_opts
else
opts = definitely_opts
params = params_or_opts
end
request_stripe_object(
method: :get,
path: send(resource_url_method, id, nested_id),
params: {},
params: params,
opts: opts
)
end
Expand Down Expand Up @@ -96,6 +115,7 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
raise ArgumentError, "Unknown operation: #{operation.inspect}"
end
end
# rubocop:enable Metrics/MethodLength
end
end
end
20 changes: 20 additions & 0 deletions lib/stripe/resources/treasury/outbound_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ def return_outbound_payment(params = {}, opts = {})
opts: opts
)
end

# Updates a test mode created OutboundPayment with tracking details. The OutboundPayment must not be cancelable, and cannot be in the canceled or failed states.
def self.update(id, params = {}, opts = {})
request_stripe_object(
method: :post,
path: format("/v1/test_helpers/treasury/outbound_payments/%<id>s", { id: CGI.escape(id) }),
params: params,
opts: opts
)
end

# Updates a test mode created OutboundPayment with tracking details. The OutboundPayment must not be cancelable, and cannot be in the canceled or failed states.
def update(params = {}, opts = {})
@resource.request_stripe_object(
method: :post,
path: format("/v1/test_helpers/treasury/outbound_payments/%<id>s", { id: CGI.escape(@resource["id"]) }),
params: params,
opts: opts
)
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/stripe/resources/treasury/outbound_transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ def return_outbound_transfer(params = {}, opts = {})
opts: opts
)
end

# Updates a test mode created OutboundTransfer with tracking details. The OutboundTransfer must not be cancelable, and cannot be in the canceled or failed states.
def self.update(outbound_transfer, params = {}, opts = {})
request_stripe_object(
method: :post,
path: format("/v1/test_helpers/treasury/outbound_transfers/%<outbound_transfer>s", { outbound_transfer: CGI.escape(outbound_transfer) }),
params: params,
opts: opts
)
end

# Updates a test mode created OutboundTransfer with tracking details. The OutboundTransfer must not be cancelable, and cannot be in the canceled or failed states.
def update(params = {}, opts = {})
@resource.request_stripe_object(
method: :post,
path: format("/v1/test_helpers/treasury/outbound_transfers/%<outbound_transfer>s", { outbound_transfer: CGI.escape(@resource["id"]) }),
params: params,
opts: opts
)
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def initialize(config_arg = {})
@system_profiler = SystemProfiler.new
@last_request_metrics = nil

# The following attribute is only used to log whether or not
# StripeClient#request has been called. To be removed in a
# future major version.
@usage = []

@config = case config_arg
when Hash
Stripe.config.reverse_duplicate_merge(config_arg)
Expand Down Expand Up @@ -186,6 +191,7 @@ def connection_manager
# charge, resp = client.request { Charge.create }
#
def request
@usage = ["stripe_client_request"]
old_stripe_client = self.class.current_thread_context.active_client
self.class.current_thread_context.active_client = self

Expand All @@ -200,6 +206,7 @@ def request
res = yield
[res, self.class.current_thread_context.last_responses[object_id]]
ensure
@usage = []
self.class.current_thread_context.active_client = old_stripe_client
self.class.current_thread_context.last_responses.delete(object_id)
end
Expand Down Expand Up @@ -606,7 +613,7 @@ def self.maybe_gc_connection_managers
if config.enable_telemetry? && context.request_id
request_duration_ms = (request_duration * 1000).to_i
@last_request_metrics =
StripeRequestMetrics.new(context.request_id, request_duration_ms, usage: usage)
StripeRequestMetrics.new(context.request_id, request_duration_ms, usage: usage + @usage)
end

# We rescue all exceptions from a request so that we have an easy spot to
Expand Down
30 changes: 30 additions & 0 deletions test/stripe/api_operations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ def self.object_name
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with only opts" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { stripe_account: "acct_123" })
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with both opts and params" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id?expand[]=reverse")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] }, { stripe_account: "acct_123" })
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with params and explicitly empty opts" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id?expand[]=reverse")
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] }, {})
assert_equal "bar", nested_resource.foo
end

should "warns when attempting to retrieve and pass only params" do
exception = assert_raises(ArgumentError) do
MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] })
end
assert_match(/Unrecognized request option/, exception.message)
end

should "define an update method" do
stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
.with(body: { foo: "baz" })
Expand Down
12 changes: 12 additions & 0 deletions test/stripe/api_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ def no_op; end
ch = Stripe::Charge.retrieve("ch_123", "sk_test_local")
ch.refunds.create
end

should "use the per-object credential when making subsequent requests on the object" do
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
.with(headers: { "Authorization" => "Bearer sk_test_local", "Stripe-Account" => "acct_12345" })
.to_return(body: JSON.generate(charge_fixture))
stub_request(:delete, "#{Stripe.api_base}/v1/customers/cus_123")
.with(headers: { "Authorization" => "Bearer sk_test_local", "Stripe-Account" => "acct_12345" })
.to_return(body: "{}")

cus = Stripe::Customer.retrieve("cus_123", { api_key: "sk_test_local", stripe_account: "acct_12345" })
cus.delete
end
end
end

Expand Down
22 changes: 22 additions & 0 deletions test/stripe/list_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ class ListObjectTest < Test::Unit::TestCase
assert_equal expected, list.auto_paging_each.to_a
end

should "forward api key through #auto_paging_iter" do
arr = [
{ id: "ch_001" },
{ id: "ch_002" },
]
expected = Util.convert_to_stripe_object(arr, {})

stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with(headers: { "Authorization" => "Bearer sk_test_iter_forwards_options" })
.to_return(body: JSON.generate(data: [{ id: "ch_001" }], has_more: true, url: "/v1/charges",
object: "list"))
stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with(headers: { "Authorization" => "Bearer sk_test_iter_forwards_options" })
.with(query: { starting_after: "ch_001" })
.to_return(body: JSON.generate(data: [{ id: "ch_002" }], has_more: false, url: "/v1/charges",
object: "list"))

list = Stripe::Charge.list({}, { api_key: "sk_test_iter_forwards_options" })

assert_equal expected, list.auto_paging_each.to_a
end

should "provide #auto_paging_each that responds to a block" do
arr = [
{ id: 1 },
Expand Down
72 changes: 72 additions & 0 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,78 @@ class StripeClientTest < Test::Unit::TestCase
assert(!trace_payload["last_request_metrics"]["request_duration_ms"].nil?)
assert(trace_payload["last_request_metrics"]["usage"].nil?)
end

should "send metrics on #request" do
Stripe.enable_telemetry = true

trace_metrics_header = nil

stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with do |req|
trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
false
end.to_return(body: JSON.generate(object: "charge"))

client = StripeClient.new
client.request do
Charge.create
Charge.list
end

assert_not_nil(trace_metrics_header)
trace_payload = JSON.parse(trace_metrics_header)
assert_equal(["stripe_client_request"], trace_payload["last_request_metrics"]["usage"])
end

should "not send metrics outside of #request block" do
Stripe.enable_telemetry = true

trace_metrics_header = nil

stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with do |req|
trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
false
end.to_return(body: JSON.generate(object: "charge"))

client = StripeClient.new
client.request do
Charge.create
Charge.list
end

Charge.create
Charge.list

assert_not_nil(trace_metrics_header)
trace_payload = JSON.parse(trace_metrics_header)
assert_nil(trace_payload["last_request_metrics"]["usage"])
end

should "append stripe_client_request usage to existing usage" do
Stripe.enable_telemetry = true

trace_metrics_header = nil
stub_request(:get, "#{Stripe.api_base}/v1/charges")
.with do |req|
trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
false
end.to_return(body: JSON.generate(object: "charge"))

client = StripeClient.new

client.request do
cus = Customer.new("cus_xyz")
cus.description = "hello"
cus.save
Charge.list
end

assert_not_nil(trace_metrics_header)
trace_payload = JSON.parse(trace_metrics_header)

assert(trace_payload["last_request_metrics"]["usage"] == %w[save stripe_client_request])
end
end

context "instrumentation" do
Expand Down
54 changes: 54 additions & 0 deletions test/stripe/transfer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,59 @@ class TransferTest < Test::Unit::TestCase
assert reversals.data.is_a?(Array)
end
end

should "retrieve a reversal with expand" do
reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ expand: %w[transfer] },
{}
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand%5B%5D=transfer"
assert reversal.is_a?(Stripe::Reversal)
end

should "be retrievable with opts only" do
transfer_reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ stripe_account: "acct_123" }
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123" do |req|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "be retrievable with opts and params" do
transfer_reversal = Stripe::Transfer.retrieve_reversal("tr_123",
"trr_123",
{ expand: ["available"] },
{ stripe_account: "acct_123" })
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand[]=available" do |req|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "be retrievable with params and an explicitly empty opts" do
transfer_reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ expand: ["available"] },
{}
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand[]=available" do |req|
assert_nil(req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "warn you if you are attempting to pass only params" do
exception = assert_raises(ArgumentError) do
Stripe::Transfer.retrieve_reversal("tr_123", "trr_123", { expand: ["available"] })
end
assert_match(/Unrecognized request option/, exception.message)
end
end
end

0 comments on commit 6a4d2d1

Please sign in to comment.