-
Notifications
You must be signed in to change notification settings - Fork 570
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
Support for passing arbitrary parameters #1057
Conversation
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.
added a few comments
foreach (KeyValuePair<string, string> pair in (Dictionary<string, string>) field.GetValue(obj)) | ||
{ | ||
var key = WebUtility.UrlEncode(pair.Key); | ||
RequestStringBuilder.ApplyParameterToRequestString(ref requestString, key, pair.Value); |
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.
Does this mean that you could pass extra_params[sub_params][subsub_value]
and it would work?
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.
The brackets would be URL-encoded. I think the API doesn't mind and would still interpret the value as a nested object, but not entirely sure tbh. Maybe URL-encoding the key is not the right thing to do here, and instead we'd make it the caller's responsibility to pass a properly URL-encoded key?
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.
hmmm that seems frustrating for users to have to do this (url encoding themselves). Maybe not supporting sub-hashes for now is fine then. Also we could do a plugin that says "oh this is a dictionary, let's properly encode those"
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'd rather keep the feature simple for now. If someone actually needs to use this method with nested objects we can think about it then.
|
||
namespace Stripe | ||
{ | ||
public class StripeParamsBase |
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 like the new name but should it be StripeBaseOptions
to match the rest of the class naming?
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.
Yeah, that's probably a better name.
@@ -33,6 +33,16 @@ public static string ApplyAllParameters(this StripeService service, object obj, | |||
RequestStringBuilder.ProcessPlugins(ref requestString, attribute, property, value, obj); | |||
} | |||
} | |||
|
|||
var field = obj.GetType().GetRuntimeField("ExtraParams"); | |||
if (field != null) |
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.
This was added temporarily right? Should we assert if you option doesn't have that value so that you know you forgot to inherit from StripeParamsBase
?
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.
Unless that breaks the reflection logic beyond saving, I think a better solution would be to change the ApplyAllParameters
signature to receive a StripeParamsBase
(or whatever name we choose for the parent class) instead of an object
.
But to answer your question, yes, all params classes should inherit from StripeParamsBase
and so there's no reason why ExtraParams
would not be available.
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 what I meant is that if we remove the if, then we can catch cases where someone creates a new Options class but forgets to inherit from the Base class.
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.
@ob-stripe Now that all class derive from StripeBaseOptions
should we remove the check? Or assert if field == null
as it should not happen?
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.
Yeah, an assert seems reasonable 👍
@ob-stripe flagging that I just pushed this: Move all top-level Options classes to StripeBaseOptions
|
567fce1
to
808c09a
Compare
44c24f5
to
44169ad
Compare
@@ -10,7 +10,7 @@ namespace Stripe.Infrastructure | |||
{ | |||
internal static class ParameterBuilder | |||
{ | |||
public static string ApplyAllParameters(this StripeService service, object obj, string url, bool isListMethod = false) | |||
public static string ApplyAllParameters(this StripeService service, StripeBaseOptions obj, string url, bool isListMethod = false) |
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 I actually went with what I mentioned before: the method now expects a StripeBaseOptions
instance instead of just an object
. That way if we forget to derive from StripeBaseOptions
the error will be immediately visible at compile time rather than at runtime. (I actually caught a few classes that you'd forgotten this way ;)
Plus, there's no need to muck with reflection to access ExtraParams
now, so the code is a bit cleaner.
ptal @remi-stripe |
44169ad
to
ba16843
Compare
ExtraParams.Add(key, value); | ||
} | ||
|
||
public Dictionary<string, string> ExtraParams = new Dictionary<string, string>(); |
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.
@ob-stripe did we want to figure out how to make that one not public before merging?
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.
LGTM
r? @remi-stripe
cc @stripe/api-libraries
This PR adds support for passing arbitrary parameters when making API requests. If the
Options
class inherit fromStripeParamsBase
, users can callAddExtraParam
to add arbitrary key/value pairs to the request.