Skip to content

Commit 09ae375

Browse files
committed
feat(webhooks): allow key parameter to accept bytes in unwrap method
1 parent 4727908 commit 09ae375

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/imagekitio/resources/webhooks.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def unsafe_unwrap(self, payload: str) -> UnsafeUnwrapWebhookEvent:
2525
),
2626
)
2727

28-
def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | None = None) -> UnwrapWebhookEvent:
28+
def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | bytes | None = None) -> UnwrapWebhookEvent:
2929
try:
3030
from standardwebhooks import Webhook
3131
except ImportError as exc:
@@ -41,7 +41,11 @@ def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | None =
4141
if not isinstance(headers, dict):
4242
headers = dict(headers)
4343

44-
encoded_key = base64.b64encode(key.encode("utf-8")).decode("ascii")
44+
if isinstance(key, str):
45+
key_bytes = key.encode("utf-8")
46+
else:
47+
key_bytes = key
48+
encoded_key = base64.b64encode(key_bytes).decode("ascii")
4549

4650
Webhook(encoded_key).verify(payload, headers)
4751

@@ -64,7 +68,7 @@ def unsafe_unwrap(self, payload: str) -> UnsafeUnwrapWebhookEvent:
6468
),
6569
)
6670

67-
def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | None = None) -> UnwrapWebhookEvent:
71+
def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | bytes | None = None) -> UnwrapWebhookEvent:
6872
try:
6973
from standardwebhooks import Webhook
7074
except ImportError as exc:
@@ -80,7 +84,11 @@ def unwrap(self, payload: str, *, headers: Mapping[str, str], key: str | None =
8084
if not isinstance(headers, dict):
8185
headers = dict(headers)
8286

83-
encoded_key = base64.b64encode(key.encode("utf-8")).decode("ascii")
87+
if isinstance(key, str):
88+
key_bytes = key.encode("utf-8")
89+
else:
90+
key_bytes = key
91+
encoded_key = base64.b64encode(key_bytes).decode("ascii")
8492

8593
Webhook(encoded_key).verify(payload, headers)
8694

0 commit comments

Comments
 (0)