| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
from typing import Mapping, cast |
| 5 |
|
| 6 |
from ..._models import construct_type |
| 7 |
from ..._resource import SyncAPIResource, AsyncAPIResource |
| 8 |
from ..._exceptions import AnthropicError |
| 9 |
from ...types.beta.beta_webhook_event import BetaWebhookEvent |
| 10 |
|
| 11 |
__all__ = ["Webhooks", "AsyncWebhooks"] |
| 12 |
|
| 13 |
|
| 14 |
class Webhooks(SyncAPIResource): |
| 15 |
def parse_unverified(self, payload: str) -> BetaWebhookEvent: |
| 16 |
"""Parses a webhook payload into an event without verifying its signature. |
| 17 |
|
| 18 |
Prefer |
| 19 |
`unwrap()` unless you have already verified the signature yourself. |
| 20 |
""" |
| 21 |
return cast( |
| 22 |
BetaWebhookEvent, |
| 23 |
construct_type( |
| 24 |
type_=BetaWebhookEvent, |
| 25 |
value=json.loads(payload), |
| 26 |
), |
| 27 |
) |
| 28 |
|
| 29 |
def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | bytes | None = None) -> BetaWebhookEvent: |
| 30 |
""" |
| 31 |
Verifies the webhook signature from the `webhook-id`, `webhook-timestamp` and |
| 32 |
`webhook-signature` headers using your webhook signing key, then parses the |
| 33 |
payload into an event. Fails if the signature is missing or invalid. |
| 34 |
""" |
| 35 |
try: |
| 36 |
from standardwebhooks import Webhook |
| 37 |
except ImportError as exc: |
| 38 |
raise AnthropicError("You need to install `anthropic[webhooks]` to use this method") from exc |
| 39 |
|
| 40 |
if key is None: |
| 41 |
key = self._client.webhook_key |
| 42 |
if key is None: |
| 43 |
raise ValueError( |
| 44 |
"Cannot verify a webhook without a key on either the client's webhook_key or passed in as an argument" |
| 45 |
) |
| 46 |
|
| 47 |
if not isinstance(headers, dict): |
| 48 |
headers = dict(headers) |
| 49 |
|
| 50 |
Webhook(key).verify(payload, headers) |
| 51 |
|
| 52 |
return cast( |
| 53 |
BetaWebhookEvent, |
| 54 |
construct_type( |
| 55 |
type_=BetaWebhookEvent, |
| 56 |
value=json.loads(payload), |
| 57 |
), |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
class AsyncWebhooks(AsyncAPIResource): |
| 62 |
def parse_unverified(self, payload: str) -> BetaWebhookEvent: |
| 63 |
"""Parses a webhook payload into an event without verifying its signature. |
| 64 |
|
| 65 |
Prefer |
| 66 |
`unwrap()` unless you have already verified the signature yourself. |
| 67 |
""" |
| 68 |
return cast( |
| 69 |
BetaWebhookEvent, |
| 70 |
construct_type( |
| 71 |
type_=BetaWebhookEvent, |
| 72 |
value=json.loads(payload), |
| 73 |
), |
| 74 |
) |
| 75 |
|
| 76 |
def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | bytes | None = None) -> BetaWebhookEvent: |
| 77 |
""" |
| 78 |
Verifies the webhook signature from the `webhook-id`, `webhook-timestamp` and |
| 79 |
`webhook-signature` headers using your webhook signing key, then parses the |
| 80 |
payload into an event. Fails if the signature is missing or invalid. |
| 81 |
""" |
| 82 |
try: |
| 83 |
from standardwebhooks import Webhook |
| 84 |
except ImportError as exc: |
| 85 |
raise AnthropicError("You need to install `anthropic[webhooks]` to use this method") from exc |
| 86 |
|
| 87 |
if key is None: |
| 88 |
key = self._client.webhook_key |
| 89 |
if key is None: |
| 90 |
raise ValueError( |
| 91 |
"Cannot verify a webhook without a key on either the client's webhook_key or passed in as an argument" |
| 92 |
) |
| 93 |
|
| 94 |
if not isinstance(headers, dict): |
| 95 |
headers = dict(headers) |
| 96 |
|
| 97 |
Webhook(key).verify(payload, headers) |
| 98 |
|
| 99 |
return cast( |
| 100 |
BetaWebhookEvent, |
| 101 |
construct_type( |
| 102 |
type_=BetaWebhookEvent, |
| 103 |
value=json.loads(payload), |
| 104 |
), |
| 105 |
) |