Skip to content

Commit 7922352

Browse files
author
sethgrid
committed
Multiple Webhook Support
SendGrid now allows for multiple webhooks and this PR pulls in the non-breaking API changes to support it. Webhook resources will now take an ID parameter to target specific webhooks. When a webhook resource is NOT used with an ID, the API falls back to legacy behavior assuming there is only one webhook, and this defaults to the oldest webhook available. Users should update their services to reference webhooks by ID.
1 parent 2fe1459 commit 7922352

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

examples/user/user.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@
196196
"unsubscribe": True,
197197
"url": "url"
198198
}
199-
response = sg.client.user.webhooks.event.settings.patch(request_body=data)
199+
webhook_id = "some-webhook-uuid"
200+
response = sg.client.user.webhooks.event.settings._(webhook_id).patch(request_body=data)
200201
print(response.status_code)
201202
print(response.body)
202203
print(response.headers)
@@ -205,7 +206,8 @@
205206
# Retrieve Event Webhook settings #
206207
# GET /user/webhooks/event/settings #
207208

208-
response = sg.client.user.webhooks.event.settings.get()
209+
webhook_id = "some-webhook-uuid"
210+
response = sg.client.user.webhooks.event.settings._(webhook_id).get()
209211
print(response.status_code)
210212
print(response.body)
211213
print(response.headers)
@@ -214,8 +216,10 @@
214216
# Test Event Notification Settings #
215217
# POST /user/webhooks/event/test #
216218

219+
webhook_id = "some-webhook-uuid"
217220
data = {
218-
"url": "url"
221+
"url": "url",
222+
"id": webhook_id
219223
}
220224
response = sg.client.user.webhooks.event.test.post(request_body=data)
221225
print(response.status_code)

test/integ/test_sendgrid.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,19 +1992,36 @@ def test_user_webhooks_event_settings_patch(self):
19921992
"unsubscribe": True,
19931993
"url": "url"
19941994
}
1995+
webhook_id = "some-webhook-uuid"
19951996
headers = {'X-Mock': 200}
1996-
response = self.sg.client.user.webhooks.event.settings.patch(
1997+
response = self.sg.client.user.webhooks.event.settings._(webhook_id).patch(
19971998
request_body=data, request_headers=headers)
19981999
self.assertEqual(response.status_code, 200)
19992000

2000-
def test_user_webhooks_event_settings_get(self):
2001+
# legacy webhook API only allowed for a single webhook. When no ID is provided,
2002+
# backwards compatiblity ensures we will get the oldest webhook back.
2003+
# Going forward, users should use settings._(webhook_id) in all calls.
2004+
def test_user_webhooks_event_settings_get_legacy_no_id(self):
20012005
headers = {'X-Mock': 200}
2006+
webhook_id = "some-webhook-uuid"
20022007
response = self.sg.client.user.webhooks.event.settings.get(
20032008
request_headers=headers)
2009+
repr(response)
2010+
self.assertEqual(response.status_code, 200)
2011+
2012+
def test_user_webhooks_event_settings_get(self):
2013+
headers = {'X-Mock': 200}
2014+
webhook_id = "some-webhook-uuid"
2015+
self.assertTrue(False, "not true")
2016+
response = self.sg.client.user.webhooks.event.settings._(webhook_id).get(
2017+
request_headers=headers)
2018+
self.assertEqual(repr(response), "I got a response")
2019+
20042020
self.assertEqual(response.status_code, 200)
20052021

20062022
def test_user_webhooks_event_test_post(self):
20072023
data = {
2024+
"id": "some-webhook-id",
20082025
"url": "url"
20092026
}
20102027
headers = {'X-Mock': 204}

0 commit comments

Comments
 (0)