Skip to content

Commit aef95e6

Browse files
feat(client): add support for verifying signatures on incoming webhooks
1 parent ca2f936 commit aef95e6

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/openai/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
PermissionDeniedError,
3030
LengthFinishReasonError,
3131
UnprocessableEntityError,
32+
APIWebhookValidationError,
3233
APIResponseValidationError,
3334
InvalidWebhookSignatureError,
3435
ContentFilterFinishReasonError,
@@ -53,6 +54,7 @@
5354
"APITimeoutError",
5455
"APIConnectionError",
5556
"APIResponseValidationError",
57+
"APIWebhookValidationError",
5658
"BadRequestError",
5759
"AuthenticationError",
5860
"PermissionDeniedError",

src/openai/_exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def __init__(self, response: httpx.Response, body: object | None, *, message: st
7777
self.status_code = response.status_code
7878

7979

80+
class APIWebhookValidationError(APIError):
81+
pass
82+
83+
8084
class APIStatusError(APIError):
8185
"""Raised when an API response has a status code of 4xx or 5xx."""
8286

tests/api_resources/test_webhooks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ def test_verify_signature_multiple_signatures_all_invalid(self, client: openai.O
168168
with pytest.raises(InvalidWebhookSignatureError, match="The given webhook signature does not match"):
169169
client.webhooks.verify_signature(TEST_PAYLOAD, headers, secret=TEST_SECRET)
170170

171+
def test_method_unwrap(self, client: OpenAI) -> None:
172+
data = """{"id":"id","created_at":0,"data":{"id":"id"},"type":"batch.cancelled","object":"event"}"""
173+
try:
174+
_ = client.webhooks.unwrap(data)
175+
except Exception as e:
176+
raise AssertionError("Failed to unwrap webhook") from e
177+
171178

172179
class TestAsyncWebhooks:
173180
parametrize = pytest.mark.parametrize(

0 commit comments

Comments
 (0)