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

Type webhook methods #307

Merged
merged 6 commits into from
Jul 31, 2024
Merged

Type webhook methods #307

merged 6 commits into from
Jul 31, 2024

Conversation

tribble
Copy link

@tribble tribble commented Jul 31, 2024

Description

Add type hints to verify_event. Unfortunately, this lead to a bunch of duplication, comments inline.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

@tribble tribble requested a review from a team as a code owner July 31, 2024 00:46
@tribble tribble requested a review from mattgd July 31, 2024 00:46
@tribble tribble changed the base branch from main to beta-5.0 July 31, 2024 00:47
Comment on lines +51 to +59
class WebhookModel(WorkOSModel, Generic[EventPayload]):
"""Representation of an Webhook delivered via Webhook.
Attributes:
OBJECT_FIELDS (list): List of fields an Webhook is comprised of.
"""

id: str
data: EventPayload
created_at: str
Copy link
Author

@tribble tribble Jul 31, 2024

Choose a reason for hiding this comment

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

Super-sad panda 😿

There is a difference between our webhooks and events. Each event contains object: 'event'. Our webhooks do not.

A mini future us project should be to add this object attribute to webhook bodies so that they're identical and we can remove this sort of duplication.

import hashlib

WebhookPayload = Union[bytes, bytearray]
WebhookTypeAdapter: TypeAdapter[Webhook] = TypeAdapter(Webhook)
Copy link
Author

Choose a reason for hiding this comment

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

Webhook is a discriminated union, not a model. So we need to create a TypeAdapter to validate the incoming payload.

Webhooks.verify_header(self, payload, sig_header, secret, tolerance)
event = json.loads(payload, object_pairs_hook=OrderedDict)
Copy link
Author

Choose a reason for hiding this comment

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

Got rid of this and just relying on pydantic's JSON parser (looks like jiter)

@tribble
Copy link
Author

tribble commented Jul 31, 2024

The webhooks tests still need fixing. The content of the webhook is incorrect, I need to replace it with a valid body and corresponding valid hash.

@tribble
Copy link
Author

tribble commented Jul 31, 2024

@mattgd Updated tests! Ready for review now.

Comment on lines +53 to +54
Attributes:
OBJECT_FIELDS (list): List of fields an Webhook is comprised of.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment can go since OBJECT_FIELDS doesn't exist anymore.

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 plan to do a comment/docstring sweep across all the resources as a follow-up. Though, I haven't seen any place where these docstrings are surfaced to the developer. Do you know how they're usually used?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see them in my editor, albeit poorly formatted:
image

Copy link
Author

Choose a reason for hiding this comment

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

Interesting. It seems like they're more useful the the actual SDK methods. Not to much for the resources/models. I have to dig into the packages to actually see the docstrings for our resources.
CleanShot 2024-07-31 at 10 52 18

Comment on lines 61 to 67
def verify_header(
self,
event_body: WebhookPayload,
event_signature: str,
secret: str,
tolerance: Optional[int] = None,
):
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a task for now, but does needing to wrap verify_header() in a try/except seem intuitive to you?

Copy link
Author

Choose a reason for hiding this comment

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

Good question. It seems like the blessed path is to use verify_event (which actually shouldn't be called verify_event, it's really a deserialization). It makes sense for that to raise when the header can't be verified.

So what's the use case for using verify_header directly? Bypassing deserialization and parsing the webhook into a raw dict?

Copy link
Author

Choose a reason for hiding this comment

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

I'm going to add a linear task to settle this.

Copy link
Author

Choose a reason for hiding this comment

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

Comment on lines 61 to 67
def verify_header(
self,
event_body: WebhookPayload,
event_signature: str,
secret: str,
tolerance: Optional[int] = None,
):
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll want an explicit -> None return here.

@tribble tribble merged commit 37f36c6 into beta-5.0 Jul 31, 2024
5 checks passed
@tribble tribble deleted the type-webhooks-methods branch July 31, 2024 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants