#kad_webhooks

1 messages · Page 1 of 1 (latest)

silver perchBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1369302510619332672

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

gentle pawn
#

Hey ther,e what event are you parsing, specifically?

storm mauve
#

hmm perhaps I just need to use StripeClient.construct_event?

#

This is for a webhook

#

handling all the incoming events. Is Webhook.event pre SDK12 and StripeClient.construct_event for SDK 12 object style?

#

No, same outcome

gentle pawn
#

giving me an attribute error trying to access say .id.
You said you encounter an error accessing .id -- but where, in what context?

#

Can you share a snippet of your webhook event handler code showing me what you're trying to do?

storm mauve
#

The standard webhook handler I think. I am not getting the error live as I have not run this, but I am getting it in VSCode from Pylance/Pyright. That last x = event_obj.id is just there to illustrate the issue. It gives "cannot access attribute "id" for class Dict[str, Any]". Some result running Pyright (most recent version as of a week ago) at the shell.

try:
    event = stripe.StripeClient(api_key=api_key).construct_event(
        payload=payload, sig_header = signature_header, secret = webhook_secret
    )
except ValueError as e:
    raise BadRequestError(f"Invalid payload from Stripe  {e}")
except stripe.SignatureVerificationError:
    raise BadRequestError("Invalid signature on Stripe request")

event_obj = event.data.object
x = event_obj.id
#

Tyoe hints say that event is an Event, data is a Data, and object is a dict[str, Any]

gentle pawn
#

Oh, so this is type validation

#

the issue is that the payload/data shape changes depending on the event type

#

You likely need to give you code a hint about the object/event type expected there

storm mauve
#

yes, event_obj should be a proper Stripe class no? id is a common so should be available no matter what?

#

or do i need to cast? you all have an example where accessing ID works, but hmm it is after checking the event.type so maybe that is the needed hint? let me see.

#

No, same issue
if event.type == "checkout.session.completed":
x = event_obj.id

#

So what is your best practice for dealing with this? You suggested type hints? Seems unsafe, but if that is the best thing to do....

gentle pawn
#

So I'm not sure what your type help is doing, exactly, or how that works with the stripe object types produced

storm mauve
#

Is it possible your snippits would fail static type analysis?

#

But yes, that and the example I posted led to think I was somehow doing something wrong, somehow getting older SDK functionality where object was a pure dict

#

I would actually need flat out cast here it seems, bad:
event_obj: stripe.StripeObject = event.data.object
gives a dict[str, Any] is not assignable to a StripeObject

gentle pawn
#

Something seems like its not working quite right -- the SDK team monitors issue on the github repos for actual bugs/problems with the SDK, and this seems like there's some confusion

#

One last thing to try before you do file an issue, can you try adding a conditional on event.type?

#

eg:

# Handle the event
  if event.type == 'payment_intent.succeeded':
    payment_intent = event.data.object # contains a stripe.PaymentIntent
    print payment_intent.id

or similar, for any specific event type

#

It's possible that event type logic drives the type validation for the payload

storm mauve
#

I did I think at 6:48 above, but will try your exact one

#

Same issue (after adjusting for Python 3 and adding () to the print)

gentle pawn
#

Ok, odd

#

Yes, please file an issue on the repo about this for the SDK team to evaluate, this does seem like it should work (or we should be more clear about how to use it.

storm mauve
#

ok thanks