Skip to content

Commit

Permalink
fix: unwrap webhooks into payload, not event
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Mar 3, 2023
1 parent 58a036f commit c442601
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ lithic.cards.create(

We provide helper methods for verifying that a webhook request came from Lithic, and not a malicious third party.

You can use `lithic.webhooks.verify_signature(body, headers, secret?) -> None` or `lithic.webhooks.unwrap(body, headers, secret?) -> Event` like so:
You can use `lithic.webhooks.verify_signature(body, headers, secret?) -> None` or `lithic.webhooks.unwrap(body, headers, secret?) -> Payload` like so:

```py
@app.post('/my-webhook-handler')
async def handler(request: Request):
body = await request.body()
secret = os.environ['LITHIC_WEBHOOK_SECRET'] # env var used by default; explicit here.
event = client.webhooks.unwrap(body, request.headers, secret)
print(event.token, event.payload) # event is an instance of the Event model
payload = client.webhooks.unwrap(body, request.headers, secret)
print(payload)
```

This example is written for [FastAPI](https://fastapi.tiangolo.com/), but usage is similar no matter what web framework you use.
Expand Down
9 changes: 4 additions & 5 deletions src/lithic/resources/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .._types import HeadersLike
from .._utils import removeprefix
from .._resource import SyncAPIResource, AsyncAPIResource
from ..types.event import Event

__all__ = ["Webhooks", "AsyncWebhooks"]

Expand All @@ -24,10 +23,10 @@ def unwrap(
headers: HeadersLike,
*,
secret: str | None = None,
) -> Event:
) -> object:
"""Validates that the given payload was sent by Lithic and parses the payload."""
self.verify_signature(payload=payload, headers=headers, secret=secret)
return Event.construct(**json.loads(payload))
return json.loads(payload)

def verify_signature(
self,
Expand Down Expand Up @@ -119,10 +118,10 @@ def unwrap(
headers: HeadersLike,
*,
secret: str | None = None,
) -> Event:
) -> object:
"""Validates that the given payload was sent by Lithic and parses the payload."""
self.verify_signature(payload=payload, headers=headers, secret=secret)
return Event.construct(**json.loads(payload))
return json.loads(payload)

def verify_signature(
self,
Expand Down

0 comments on commit c442601

Please sign in to comment.