-
Notifications
You must be signed in to change notification settings - Fork 548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn if idempotency_key is in params hash #545
Conversation
721b156
to
5a421b7
Compare
5a421b7
to
c321924
Compare
@andrewyang-stripe Thanks! I really like the test addition. A couple notes:
ptal @andrewyang-stripe |
Perhaps I can create a helper method called |
So in this case
As discussed IRL, although we only test it in the one place here, you're allowed to pass this in for any request, and it's as general as something like |
I've figured out where to write the check for |
ensure | ||
$stderr = old_stderr | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thinking, but I think having tests for both idempotency key and API key for create/update is a bit overkill. Let's take these two out okay?
Maybe you can generalize the name of the other with something like just refer to a general "opt" instead of api_key
or idempotency_key
specifically.
test/stripe/api_resource_test.rb
Outdated
to_return(body: JSON.generate({ :count => 1, :data => [API_FIXTURES.fetch(:charge)] })) | ||
Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil }) | ||
end | ||
|
||
should "putting idempotency_key correctly as an opt should not trigger a warning" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the naming of these test cases should be readable from the "should".
"should putting idempotency_key correctly ..." doesn't make sense. Maybe something like "should trigger a warning ...".
lib/stripe/api_operations/request.rb
Outdated
@@ -5,6 +5,8 @@ module ClassMethods | |||
OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :client, :stripe_account, :stripe_version] | |||
|
|||
def request(method, url, params={}, opts={}) | |||
Util.validate_params!(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we're only going to be using this helper in one place, lets move it back to request.rb
okay?
lib/stripe/util.rb
Outdated
@@ -2,6 +2,8 @@ | |||
|
|||
module Stripe | |||
module Util | |||
@invalid_params = Set.new [:idempotency_key, :api_key, :stripe_account] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets make this a constant and give it a slightly more descriptive name, okay? KNOWN_OPTS
or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and can we also alphabetize the keys in the list and also move it to the private
section of the class? You can use private_const :CONST
to protect it.
lib/stripe/util.rb
Outdated
@@ -213,6 +215,14 @@ def self.check_string_argument!(key) | |||
key | |||
end | |||
|
|||
def self.validate_params!(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we call this something a little more appropriate like warn_on_opts_in_params
? Lets remove the bang (!
) too. This is generally used to show that the method may throw an error, which isn't the case here.
Added a couple change requests, but looking good. Thanks! |
Made the change requests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes! I left a couple more comments, but I think we're almost there.
test/stripe/api_resource_test.rb
Outdated
ensure | ||
$stderr = old_stderr | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's get rid of this test too. I think just the two below give us enough coverage.
lib/stripe/api_operations/request.rb
Outdated
@@ -3,8 +3,19 @@ module APIOperations | |||
module Request | |||
module ClassMethods | |||
OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :client, :stripe_account, :stripe_version] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this line gives us another hint for an opt
that we can warn on! What do you think about adding stripe_version
to the set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should OPTS_KEYS_TO_PERSIST
and KNOWN_OPTS
be merged?
lib/stripe/api_operations/request.rb
Outdated
@@ -3,8 +3,19 @@ module APIOperations | |||
module Request | |||
module ClassMethods | |||
OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :client, :stripe_account, :stripe_version] | |||
KNOWN_OPTS = Set[:api_key, :idempotency_key, :stripe_account] | |||
|
|||
def warn_on_opts_in_params(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move both this method and the constant above down to a new private
section in the file? Lets also put a private_constant :KNOWN_OPTS
under the KNOWN_OPTS
declaration.
fa6a046
to
10b7828
Compare
|
||
private | ||
|
||
KNOWN_OPTS = Set[:api_key, :idempotency_key, :stripe_account, :stripe_version] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the private
block will affect visibility for constants, so can put in the private_constant
tag like we talked about?
Very close! ptal @andrew-stripe |
10b7828
to
7a9ae05
Compare
Fixed. ptal @brandur-stripe |
Great! After you rebase, then I think we're good to go. |
7a9ae05
to
e66eac4
Compare
Fixes #544. Also add a test case to ensure that there's a warning in stderr when
idempotency_key
is in theparams
hash, and not in theopts
hash.