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

#4466 Enrich /webhooks/deliveries endpoint to include webhook endpoint schema #4802

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/polar/models/webhook_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from polar.kit.db.models.base import RecordModel
from polar.models.webhook_event import WebhookEvent
from polar.models.webhook_endpoint import WebhookEndpoint


class WebhookDelivery(RecordModel):
Expand All @@ -17,6 +18,10 @@ class WebhookDelivery(RecordModel):
index=True,
)

@declared_attr
def webhook_endpoint(cls) -> Mapped[WebhookEndpoint]:
return relationship("WebhookEndpoint", lazy="raise")

webhook_event_id: Mapped[UUID] = mapped_column(
Uuid,
ForeignKey("webhook_events.id", ondelete="CASCADE"),
Expand Down
3 changes: 3 additions & 0 deletions server/polar/webhook/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ class WebhookDelivery(TimestampedSchema):
webhook_event: WebhookEvent = Field(
description="The webhook event sent by this delivery."
)
webhook_endpoint: WebhookEndpoint = Field(
description="The webhook endpoint called by this delivery."
)
Comment on lines +131 to +133
Copy link
Author

Choose a reason for hiding this comment

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

Is returning the whole schema too much?
Should I be adding a webhook_endpoint_url: EndpointUrl instead and load it like:

[WebhookDeliverySchema.model_validate({**result, "webhook_endpoint_url": result.webhook_endpoint.url) for result in results],

5 changes: 4 additions & 1 deletion server/polar/webhook/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ async def list_deliveries(
readable_endpoints_statement.with_only_columns(WebhookEndpoint.id)
),
)
.options(joinedload(WebhookDelivery.webhook_event))
.options(
contains_eager(WebhookDelivery.webhook_endpoint),
joinedload(WebhookDelivery.webhook_event),
)
.order_by(desc(WebhookDelivery.created_at))
)

Expand Down
12 changes: 12 additions & 0 deletions server/tests/webhooks/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ async def test_user(
json = response.json()
assert len(json["items"]) == 1
assert json["items"][0]["id"] == str(webhook_delivery.id)
assert json["items"][0]["webhook_endpoint"]["id"] == str(
webhook_endpoint_organization.id
)
assert json["items"][0]["webhook_endpoint"]["url"] == str(
webhook_endpoint_organization.url
)

@pytest.mark.auth(
AuthSubjectFixture(subject="organization", scopes={Scope.webhooks_write})
Expand All @@ -266,3 +272,9 @@ async def test_organization(
json = response.json()
assert len(json["items"]) == 1
assert json["items"][0]["id"] == str(webhook_delivery.id)
assert json["items"][0]["webhook_endpoint"]["id"] == str(
webhook_endpoint_organization.id
)
assert json["items"][0]["webhook_endpoint"]["url"] == str(
webhook_endpoint_organization.url
)