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

small functional improvements #662

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open

small functional improvements #662

wants to merge 3 commits into from

Conversation

robko23
Copy link

@robko23 robko23 commented Jan 22, 2025

Summary

Hi, first of all - amazing work what you've done on the next branch. The api is absolutely amazing, but it still has some rough edges.
I've done one integration on 0.40 and now I'm building another integration and I've decided to use the next branch, so that I can test its ergonomics and help to improve it.

I would consider this PR a draft for now, because I'm just starting with the integration and I probably will send more patches if something doesn't work or doesn't seem right.

For now this PR contains two small commits - everything is explained in detail in the commit message as to why I've done it, but here is a list of things I've done (I will be editing this list with the new patches as I create them):

  • export StripeRequest trait - allow .customize() to be used outside this crate
  • change Idempotency-Key to idempotency-key - caused panic on runtime

Checklist

`.customize()` function was not accesible from outside of the crate
because trait StripeRequest was not exported anywhere

Signed-off-by: Robin Ilyk <[email protected]>
Copy link
Collaborator

@mzeitlin11 mzeitlin11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing this @robko23, there are definitely lots of improvements that can be made!

I know this is in draft, but I'd be happy to merge this with the added assert! either changed to an error or removed since the other 2 changes should be pretty uncontroversial improvements.

@@ -109,7 +109,9 @@ impl Client {
let mut last_error = StripeError::ClientError("invalid strategy".into());

if let Some(key) = strategy.get_key() {
req_builder = req_builder.header(HeaderName::from_static("Idempotency-Key"), key);
const HEADER_NAME: HeaderName = HeaderName::from_static("idempotency-key");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's a bad panic! Think this must've happened because the old usage of http-types instead of hyper/http supported this, and there must be no test coverage of idempotency key usage

@@ -109,7 +109,9 @@ impl Client {
let mut last_error = StripeError::ClientError("invalid strategy".into());

if let Some(key) = strategy.get_key() {
req_builder = req_builder.header(HeaderName::from_static("Idempotency-Key"), key);
const HEADER_NAME: HeaderName = HeaderName::from_static("idempotency-key");
assert!(!key.is_empty(), "idempotency key is empty");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would be better to return an error here, could add a variant to StripeError. Even better would be to validate on construction of RequestStrategy, but that's not currently possible without hiding more of the internal representation there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I put that assertion here because I wanted to narrow down the out of bound index and then I left it there because it seemed a good sanity check.
However I also think that having a proper error would be better, so I created a commit that adds a newtype for the idempotency key. It is basically a string that cannot be empty or longer than 255 chars. It has its own error, because I think its mostly isolated from the rest of the logic. Having it mostly transparent struct also doesn't hide any implementation and representation

robko23 added a commit to robko23/async-stripe that referenced this pull request Jan 23, 2025
idempotency key cannot be empty and cannot be longer than 255 characters
and having asserts before their usage is not the best error handling
strategy. having it in a newtype guarantees that every use of this key
is valid and no assert or runtime panic is needed.

Refs: arlyon#662 (comment)
Refs: https://docs.stripe.com/api/idempotent_requests
Signed-off-by: Robin Ilyk <[email protected]>
@robko23
Copy link
Author

robko23 commented Jan 23, 2025

I've removed the assert and if you want to I can mark it as ready and you can merge it, but there will be more small changes from me as I use it more. I didn't want to overwhelm you with these one line PRs so my idea was to pack it in a single PR with many small commits. But if you don't mind this, I can break it down to more PRs

robko23 added a commit to robko23/async-stripe that referenced this pull request Jan 23, 2025
idempotency key cannot be empty and cannot be longer than 255 characters
and having asserts before their usage is not the best error handling
strategy. having it in a newtype guarantees that every use of this key
is valid and no assert or runtime panic is needed.

Refs: arlyon#662 (comment)
Refs: https://docs.stripe.com/api/idempotent_requests
Signed-off-by: Robin Ilyk <[email protected]>
@mzeitlin11
Copy link
Collaborator

Sorry for the delay here @robko23! I think the newtype approach is the right one generally, and I think that commit will be useful at a later point, but I'd imagine there will be more rounds of iteration on RequestStrategy going forward, so would like to hold off on breaking changes there since I think larger changes will come and don't want to have users testing out the next branch need to migrate their code too much.

We can also make an issue to start tracking / discussing what the most useful form of RequestStrategy might be given the new structure of having custom clients in the next branch.

I'd be happy to merge if you don't mind just reverting the RequestStrategy changes and the original assert, so that this just fixes the bug.

@robko23
Copy link
Author

robko23 commented Feb 9, 2025

Okay, so I cherry picked the commit with the newtype and I will create a new pull request with it. Then I removed this from this branch.

Then I changed the commit with the assert and removed it, but I still kept the const HEADER_NAME

@robko23 robko23 marked this pull request as ready for review February 9, 2025 07:40
@robko23 robko23 changed the title DRAFT: small functional improvements small functional improvements Feb 9, 2025
robko23 added a commit to robko23/async-stripe that referenced this pull request Feb 9, 2025
idempotency key cannot be empty and cannot be longer than 255 characters
and having asserts before their usage is not the best error handling
strategy. having it in a newtype guarantees that every use of this key
is valid and no assert or runtime panic is needed.

Refs: arlyon#662 (comment)
Refs: arlyon#662 (comment)
Refs: https://docs.stripe.com/api/idempotent_requests
Signed-off-by: Robin Ilyk <[email protected]>
Copy link
Collaborator

@mzeitlin11 mzeitlin11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @robko23! CI failure should be unrelated, I have a fix in #660, which I will pull out separately.

@mzeitlin11
Copy link
Collaborator

Ok, sorry for the annoyance, but CI should be good now, so if you can rebase I'll merge it!

@arlyon
Copy link
Owner

arlyon commented Feb 19, 2025

Hey thanks for this @robko23 ! I have been MIA for a little bit (was very ill for a couple of months the end of last year but I am back on form). @mzeitlin11 seems to be carrying the torch just fine in the mean time but I will stamp anyways.

EDIT: have updated the branch via a merge commit, will merge the PR if everything is green

@arlyon
Copy link
Owner

arlyon commented Feb 19, 2025

Seems like there is a genuine clippy lint here. Feel free to @ either myself or Matthew when this is addressed

robko23 and others added 2 commits February 20, 2025 08:15
http crate does not allow uppercase characters in header name and it was
causing crashes when hitting thing branch with error "index out of
bounds."

The error should happen at compile time, but for some reason the
HeaderName::from_static was executed at runtime, so this commit forces
it to be executed at compile time by extracing it into the const.

Signed-off-by: Robin Ilyk <[email protected]>
@robko23
Copy link
Author

robko23 commented Feb 20, 2025

hi @arlyon! Thanks for the response and I hope your health will be better in the future.

About the clippy lint, I have decided to ignore (#[allow]) it since it seems like it is false positive.
It took me a while to trigger the lint on my machine and I figured out you have older version of rustc on the CI. You are using 1.75.0, I have 1.84.1 and it does not trigger there.
The reason why it gets triggered on the older version is that HeaderName uses bytes::Bytes and that uses AtomicPtr which triggers the lint.
bytes::Bytes is by default ignored by the clippy lint and for some reason clippy doesn't figure out this relationship and triggers the lint.

See this, this and this for more information

robko23 added a commit to robko23/async-stripe that referenced this pull request Feb 20, 2025
idempotency key cannot be empty and cannot be longer than 255 characters
and having asserts before their usage is not the best error handling
strategy. having it in a newtype guarantees that every use of this key
is valid and no assert or runtime panic is needed.

Refs: arlyon#662 (comment)
Refs: arlyon#662 (comment)
Refs: https://docs.stripe.com/api/idempotent_requests
Signed-off-by: Robin Ilyk <[email protected]>
@kkudryaev
Copy link

Hi! Thank you for your thorough work. Could you please merge this as soon as possible? I need to use the idempotent request strategy. Thanks!

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 this pull request may close these issues.

4 participants