stripe-python
stripe-python copied to clipboard
"Client" object to encapsulate non-global usage of the library?
I'd really like to instantiate a stripe "client" object with appropriate configuration (api_key, stripe_account, http_client) and call methods on it (e.g. client.charge.list()
rather than stripe.Charge.list()
) to load resources. The current structure makes it possible, but awkward, to do this; I have to write a wrapper that encapsulates every classmethod in the entire library and passes it this configuration on every method that can load resources.
I'm using version 1.77.1.
It definitely would have been better if the library had been written this way originally — just practically speaking though, it might be a pretty large refactor getting it into that shape nowadays. I'm going to keep this open and mark as "future" in case we can tackle it at some point.
Thanks for your consideration!
+1 - now it is really hard to use this library as a part of bigger system with multiple Stripe clients, as api_key variable is global and not thread-safe.
I know we haven't given any updates on this, but just wanted to say that this is still on our radar.
For API keys specifically, you can specify the API key on a per request basis to maintain thread safety, by using the api_key
parameter:
charges = stripe.Charge.list(api_key='sk_...')
This will override the global API key.
Has there been any progress on this? Currently having to support multiple api keys, and not looking forward to passing the key to every call in our system.
@sambernard We don't have an update just yet for stripe-python but we are finalizing this change for stripe-php this week which you can see in https://github.com/stripe/stripe-php/pull/771. We do plan to roll this out to other libraries later this year though we don't have firm plans for stripe-python just yet.
I hate to be that guy, but... has there been any progress on this? 😬
@slafs No progress this year unfortunately. We did build this new abstraction in stripe-php earlier this year though and you can read more about this here. We're now working on this for stripe-java which is the library that needs it the most and we do hope to generalize this to stripe-ruby and stripe-python next year!
been a few years, bumping to see if there was any upcoming plans to make this change - thanks!
+1
Hello all, sorry for the long silence on this.
In stripe-python v8.0.0, we've just released a StripeClient
interface that supports configuration per client object and calling "service" methods via the client. You can refer to the v8 migration guide for instructions on how to switch to the client interface.
We would love for you to try it out! I'll be closing this issue since the original request for a non-global "client" object is complete. However, if you encounter any problems or have further feature requests around StripeClient, feel free to open a new issue.
The change that introduced StripeClient for reference: https://github.com/stripe/stripe-python/pull/1200