Skip to content
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 idempotency key in post requests #342

Closed
evforde opened this issue Feb 16, 2023 · 1 comment · Fixed by #363
Closed

Support idempotency key in post requests #342

evforde opened this issue Feb 16, 2023 · 1 comment · Fixed by #363

Comments

@evforde
Copy link

evforde commented Feb 16, 2023

Is your feature request related to a problem? Please describe.

Stripe supports providing an Idempotency-Key header on POST (and other retry-able requests) in order to allow safely retrying a request when a connection error occurs. The async-stripe library doesn't appear to support this yet, and for good reason - it is tricky to implement :)

Describe the solution you'd like

The idempotency key header is set per request, but since the client methods are largely invoked via util methods on the autogenerated resource types, there's not a simple backwards-compatible way to specify an idempotency key per request.

A few ideas:

  • For the params arguments, like CreateSubscription, add an idempotency_key value that isn't serialized in the HTTP body but is rather serialized in the headers. Bleh
  • Make a util function on Client that allows generating a wrapper around a client that includes an idempotency key. Syntax might look something like
let subscription = Subscription::create(client.with_idempotency_key(key), params).await?;

We would have to be careful to make it hard to accidentally set an idempotency key for the entire client in a way that would make it easy to accidentally send the same idempotency key on another request. Tricky

  • Create a similar wrapper around all params. This might feel the most syntactically correct, but is difficult to implement
let subscription = Subscription::create(client, params.with_idempotency_key(key)).await?;

We'd probably have to make all params-like structs implement a trait that provides this with_idempotency_key util. Oof

  • Add a hard-coded Subscription::create_with_idempotency_key(client, params, key) for all use cases I want to use
  • Something else I am too obtuse to think of

Any interest in any of these methods? If so, I'm happy to take a stab at implementing :)

Thanks all for your help!

Describe alternatives you've considered

No response

Additional context

No response

@arlyon
Copy link
Owner

arlyon commented Feb 20, 2023

Have a look at strategies, does this handle your use case? If you use the retry strategy, a key will automatically be generated for you and used across retries. If you would rather handle this yourself, you can create your own or use https://docs.rs/async-stripe/0.18.3/stripe/enum.RequestStrategy.html#method.idempotent_with_uuid.

I think the real issue is that we should do a better job documenting that API.

@arlyon arlyon mentioned this issue Apr 6, 2023
2 tasks
stripe-openapi-bot pushed a commit that referenced this issue Apr 6, 2023
# [0.21.0](v0.20.2...v0.21.0) (2023-04-06)

### Bug Fixes

* **#342:** add documentation regarding idempotency to the main readme ([d28f7df](d28f7df)), closes [#342](#342)

### Features

* use codegen version of WebhookEvent rather than overriding manually ([8347a6d](8347a6d))
jwiesler pushed a commit to jwiesler/async-stripe that referenced this issue Apr 12, 2024
# 1.0.0 (2024-04-12)

### Bug Fixes

* **arlyon#342:** add documentation regarding idempotency to the main readme ([d28f7df](d28f7df)), closes [arlyon#342](https://github.com/jwiesler/async-stripe/issues/342)
* add automated release config ([ac5a87a](ac5a87a))
* Add card to PaymentMethodDetails ([fe83da4](fe83da4))
* add every Stripe error code https://stripe.com/docs/error-codes ([57f06b6](57f06b6))
* add idempotency_error ([0e842f5](0e842f5))
* Add missing fields to PaymentIntent ([e968304](e968304))
* add missing prefix for CheckoutSessionItemId ([c6bdb57](c6bdb57))
* add plan prefix to PriceId ([6655058](6655058))
* add products to features list in cargo.toml and export from resourses ([d26860f](d26860f))
* Add sis_ id prefix for usage record summaries ([1042493](1042493))
* add support for atxi id prefix ([414d534](414d534))
* api changes to resolve overlapping imports ([6eb5b05](6eb5b05))
* avoid generating nested currency options ([f020df4](f020df4))
* **codegen:** generate terminal resource objects ([bf7e117](bf7e117))
* correctly generate types for optional lists ([63732cc](63732cc))
* **currency:** add BYN and MMK to currency list ([03cddce](03cddce))
* don't pin time-core for msrv ([00a9a1d](00a9a1d))
* export request strategy ([f350a34](f350a34))
* export SearchList ([a427943](a427943))
* expose the `auto_advance` field from the `FinalizeInvoiceParams` struct ([ae1a807](ae1a807))
* fixes an issue with missing renames of Self_ ([9c58038](9c58038))
* generate api version from the codegen ([3006688](3006688))
* introduce subtle and use it for constant time comparison ([dc11407](dc11407))
* make checkout depend on billing ([906b937](906b937))
* Manually return error ([2e135ee](2e135ee))
* Move feature check to `build.rs` ([8d06be0](8d06be0))
* nickname can be null. This fix the "invalid type: null, expected a string" error ([c8aff89](c8aff89))
* **pagination:** prevent infinite loop caused by clone ([bc20bd4](bc20bd4))
* **params:** do not print "next" while iterating ([9139ccd](9139ccd))
* **prefixes:** add pyr_ prefix to Refund object ([5bc477b](5bc477b))
* prevent freeze when hitting network errors in a retry strategy ([d423be2](d423be2))
* prevent panic on StatusCode conversion ([9b94228](9b94228))
* prevent publishing the benchmark crate ([e4be54d](e4be54d))
* prevent publishing the openapi crate ([6de846e](6de846e))
* reexport duplicate SubscriptionPaymentBehaviour ([dbcff41](dbcff41))
* Remove "Some()" from RequestError's display ([e609cad](e609cad))
* remove non-error and add __NonExhaustive ([08c923f](08c923f))
* rename redirect_url to return_url on ConfirmSetupIntent ([9da5a28](9da5a28)), closes [arlyon#504](https://github.com/jwiesler/async-stripe/issues/504)
* respect the required and optional fields for EventType and Metadata ([cdabd2e](cdabd2e))
* small issue with features ([51c2884](51c2884))
* support pdp_ as an id prefix for dispute objects ([be506d6](be506d6))
* typo in checkout.rs ([c55541b](c55541b))
* Update hyper-rustls to fix RUSTSEC-2023-0052 ([7c31f76](7c31f76))
* Use `HashMap::from` ([0c8dc9d](0c8dc9d))
* use get_query to fix bad query strings ([f032570](f032570))

### Features

* add CreditNote and CustomerBalanceTransaction ([dc82e59](dc82e59))
* add idempotent_with_uuid convenience method ([d9bc63b](d9bc63b))
* Add imports ([afd7aaf](afd7aaf))
* add paginator API ([2dc1a68](2dc1a68))
* Add path to errors ([1548ee4](1548ee4))
* add retrieve checkout session ([25bd0f5](25bd0f5))
* add steam API to list ([8160ee4](8160ee4))
* add support for connect tokens ([b7c5489](b7c5489))
* add support for webpki (alt. to native-certs) ([c994b1c](c994b1c))
* add tax calculation api behind a feature ([2266ed1](2266ed1))
* added finalize invoice ([c6333bc](c6333bc))
* fixed bool value ([c752f52](c752f52))
* fixed bool value ([153d63d](153d63d))
* fixed mutability ([86aabdd](86aabdd))
* fixed mutability ([5ad76c5](5ad76c5))
* fixed warnings ([30f0a1e](30f0a1e))
* fixed warnings ([e0d0bd8](e0d0bd8))
* fn to expire checkout sessions ([0368c79](0368c79))
* generate latest changes from OpenApi spec ([a3b0e4d](a3b0e4d))
* generate latest changes from OpenApi spec ([29a457d](29a457d))
* generate latest changes from OpenApi spec ([19ac377](19ac377))
* generate latest changes from OpenApi spec ([9b3a844](9b3a844))
* generate latest changes from OpenApi spec ([d0cbc71](d0cbc71))
* generate latest changes from OpenApi spec ([32e802e](32e802e))
* generate latest changes from OpenApi spec ([4c25649](4c25649))
* generate latest changes from OpenApi spec ([652a360](652a360))
* generate latest changes from OpenApi spec ([a76703c](a76703c))
* Generate latest changes from OpenApi spec ([7421960](7421960))
* Generate latest changes from OpenApi spec ([2fedb71](2fedb71))
* handle currency_options ([1bb8165](1bb8165))
* Ignore vscode folder ([0a4265d](0a4265d))
* impl Paginable for all list items ([e05872b](e05872b))
* Implement Balance retrieve ([7509c98](7509c98))
* improve api doc code to fix / add more coverage ([fecaa2e](fecaa2e))
* update stripe api ([2cba3fe](2cba3fe))
* updated ListPaginator to be generic over type T where T impl PaginableList instead of having separate SearchListPaginator and ListPaginator implementations for types SearchList and List. ([9d49602](9d49602))
* updated ListPaginator to be generic over type T where T impl PaginableList instead of having separate SearchListPaginator and ListPaginator implementations for types SearchList and List. ([411f82c](411f82c))
* use codegen version of WebhookEvent rather than overriding manually ([8347a6d](8347a6d))
* use non-generic inner function for send ([03ee440](03ee440))
* **webhook_events:** support construct_event with timestamp ([88fe501](88fe501))

### Reverts

* Revert "chore: pin dependencies to prevent msrv lapse" ([7826eff](7826eff))
* Revert "Remove dependency on chrono" ([e816e3b](e816e3b))
jwiesler pushed a commit to jwiesler/async-stripe that referenced this issue Apr 12, 2024
# 1.0.0 (2024-04-12)

### Bug Fixes

* **arlyon#342:** add documentation regarding idempotency to the main readme ([d28f7df](d28f7df)), closes [arlyon#342](https://github.com/jwiesler/async-stripe/issues/342)
* add automated release config ([ac5a87a](ac5a87a))
* Add card to PaymentMethodDetails ([fe83da4](fe83da4))
* add every Stripe error code https://stripe.com/docs/error-codes ([57f06b6](57f06b6))
* add idempotency_error ([0e842f5](0e842f5))
* Add missing fields to PaymentIntent ([e968304](e968304))
* add missing prefix for CheckoutSessionItemId ([c6bdb57](c6bdb57))
* add plan prefix to PriceId ([6655058](6655058))
* add products to features list in cargo.toml and export from resourses ([d26860f](d26860f))
* Add sis_ id prefix for usage record summaries ([1042493](1042493))
* add support for atxi id prefix ([414d534](414d534))
* api changes to resolve overlapping imports ([6eb5b05](6eb5b05))
* avoid generating nested currency options ([f020df4](f020df4))
* **codegen:** generate terminal resource objects ([bf7e117](bf7e117))
* correctly generate types for optional lists ([63732cc](63732cc))
* **currency:** add BYN and MMK to currency list ([03cddce](03cddce))
* don't pin time-core for msrv ([00a9a1d](00a9a1d))
* export request strategy ([f350a34](f350a34))
* export SearchList ([a427943](a427943))
* expose the `auto_advance` field from the `FinalizeInvoiceParams` struct ([ae1a807](ae1a807))
* fixes an issue with missing renames of Self_ ([9c58038](9c58038))
* generate api version from the codegen ([3006688](3006688))
* introduce subtle and use it for constant time comparison ([dc11407](dc11407))
* make checkout depend on billing ([906b937](906b937))
* Manually return error ([2e135ee](2e135ee))
* Move feature check to `build.rs` ([8d06be0](8d06be0))
* nickname can be null. This fix the "invalid type: null, expected a string" error ([c8aff89](c8aff89))
* **pagination:** prevent infinite loop caused by clone ([bc20bd4](bc20bd4))
* **params:** do not print "next" while iterating ([9139ccd](9139ccd))
* **prefixes:** add pyr_ prefix to Refund object ([5bc477b](5bc477b))
* prevent freeze when hitting network errors in a retry strategy ([d423be2](d423be2))
* prevent panic on StatusCode conversion ([9b94228](9b94228))
* prevent publishing the benchmark crate ([e4be54d](e4be54d))
* prevent publishing the openapi crate ([6de846e](6de846e))
* reexport duplicate SubscriptionPaymentBehaviour ([dbcff41](dbcff41))
* Remove "Some()" from RequestError's display ([e609cad](e609cad))
* remove non-error and add __NonExhaustive ([08c923f](08c923f))
* rename redirect_url to return_url on ConfirmSetupIntent ([9da5a28](9da5a28)), closes [arlyon#504](https://github.com/jwiesler/async-stripe/issues/504)
* respect the required and optional fields for EventType and Metadata ([cdabd2e](cdabd2e))
* small issue with features ([51c2884](51c2884))
* support pdp_ as an id prefix for dispute objects ([be506d6](be506d6))
* typo in checkout.rs ([c55541b](c55541b))
* Update hyper-rustls to fix RUSTSEC-2023-0052 ([7c31f76](7c31f76))
* Use `HashMap::from` ([0c8dc9d](0c8dc9d))
* use get_query to fix bad query strings ([f032570](f032570))

### Features

* add CreditNote and CustomerBalanceTransaction ([dc82e59](dc82e59))
* add idempotent_with_uuid convenience method ([d9bc63b](d9bc63b))
* Add imports ([afd7aaf](afd7aaf))
* add paginator API ([2dc1a68](2dc1a68))
* Add path to errors ([1548ee4](1548ee4))
* add retrieve checkout session ([25bd0f5](25bd0f5))
* add steam API to list ([8160ee4](8160ee4))
* add support for connect tokens ([b7c5489](b7c5489))
* add support for webpki (alt. to native-certs) ([c994b1c](c994b1c))
* add tax calculation api behind a feature ([2266ed1](2266ed1))
* added finalize invoice ([c6333bc](c6333bc))
* fixed bool value ([c752f52](c752f52))
* fixed bool value ([153d63d](153d63d))
* fixed mutability ([86aabdd](86aabdd))
* fixed mutability ([5ad76c5](5ad76c5))
* fixed warnings ([30f0a1e](30f0a1e))
* fixed warnings ([e0d0bd8](e0d0bd8))
* fn to expire checkout sessions ([0368c79](0368c79))
* generate latest changes from OpenApi spec ([a3b0e4d](a3b0e4d))
* generate latest changes from OpenApi spec ([29a457d](29a457d))
* generate latest changes from OpenApi spec ([19ac377](19ac377))
* generate latest changes from OpenApi spec ([9b3a844](9b3a844))
* generate latest changes from OpenApi spec ([d0cbc71](d0cbc71))
* generate latest changes from OpenApi spec ([32e802e](32e802e))
* generate latest changes from OpenApi spec ([4c25649](4c25649))
* generate latest changes from OpenApi spec ([652a360](652a360))
* generate latest changes from OpenApi spec ([a76703c](a76703c))
* Generate latest changes from OpenApi spec ([7421960](7421960))
* Generate latest changes from OpenApi spec ([2fedb71](2fedb71))
* handle currency_options ([1bb8165](1bb8165))
* Ignore vscode folder ([0a4265d](0a4265d))
* impl Paginable for all list items ([e05872b](e05872b))
* Implement Balance retrieve ([7509c98](7509c98))
* improve api doc code to fix / add more coverage ([fecaa2e](fecaa2e))
* update stripe api ([2cba3fe](2cba3fe))
* updated ListPaginator to be generic over type T where T impl PaginableList instead of having separate SearchListPaginator and ListPaginator implementations for types SearchList and List. ([9d49602](9d49602))
* updated ListPaginator to be generic over type T where T impl PaginableList instead of having separate SearchListPaginator and ListPaginator implementations for types SearchList and List. ([411f82c](411f82c))
* use codegen version of WebhookEvent rather than overriding manually ([8347a6d](8347a6d))
* use non-generic inner function for send ([03ee440](03ee440))
* **webhook_events:** support construct_event with timestamp ([88fe501](88fe501))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants