Skip to content

Commit a737548

Browse files
jen-obyrneepels
authored andcommitted
Add new endpoint to update conversation webhooks
* ENG-129 Adding new UpdateWebhook for conversations - includes a new struct for the updateRequest - adding test to make sure it works * ENG-129 Adding status to webhook Adding new property to the WebhookUpdateRequest so customers can enabled/disable webhooks * ENG-129 Fix spelling mistake in ReadMe * ENG-129 Omiting empty properties if the user is only updating certain fields. Updating the comment against the new endpoint to reflect this information * Update comments Fix typo and add full stops at the end of the sentence * bump version
1 parent fd48d63 commit a737548

File tree

8 files changed

+77
-2
lines changed

8 files changed

+77
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Complete documentation, instructions, and examples are available at:
7070

7171
Upgrading
7272
---------
73-
If you're upgrading from older versions, please read the [Messagebird `go-rest-api` uprading guide](UPGRADING.md).
73+
If you're upgrading from older versions, please read the [Messagebird `go-rest-api` upgrading guide](UPGRADING.md).
7474

7575
License
7676
-------

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
const (
2727
// ClientVersion is used in User-Agent request header to provide server with API level.
28-
ClientVersion = "5.1.1"
28+
ClientVersion = "5.3.0"
2929

3030
// Endpoint points you to MessageBird REST API.
3131
Endpoint = "https://rest.messagebird.com"

conversation/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ type Webhook struct {
189189
ChannelID string
190190
Events []WebhookEvent
191191
URL string
192+
Status WebhookStatus
192193
CreatedDatetime *time.Time
193194
UpdatedDatetime *time.Time
194195
}
@@ -202,6 +203,17 @@ const (
202203
WebhookEventMessageUpdated WebhookEvent = "message.updated"
203204
)
204205

206+
// WebhookStatus indicates what state a Webhook is in.
207+
// At the moment there are only 2 statuses; enabled or disabled.
208+
type WebhookStatus string
209+
210+
const (
211+
// WebhookStatusEnabled indictates that the webhook is enabled.
212+
WebhookStatusEnabled WebhookStatus = "enabled"
213+
// WebhookStatusDisabled indictates that the webhook is disabled.
214+
WebhookStatusDisabled WebhookStatus = "disabled"
215+
)
216+
205217
// request does the exact same thing as Client.Request. It does, however,
206218
// prefix the path with the Conversation API's root. This ensures the client
207219
// doesn't "handle" this for us: by default, it uses the REST API.

conversation/testdata/webhookObject.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"conversation.created",
77
"message.updated"
88
],
9+
"status": "enabled",
910
"createdDatetime": "2018-08-24T14:24:04Z",
1011
"updatedDatetime": null
1112
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"events":["conversation.updated"],"url":"https://example.com/mynewwebhookurl","status":"disabled"}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": "whid",
3+
"url": "https://example.com/mynewwebhookurl",
4+
"channelId": "chid",
5+
"events": [
6+
"conversation.updated"
7+
],
8+
"status": "disabled",
9+
"createdDatetime": "2018-08-24T14:24:04Z",
10+
"updatedDatetime": "2019-07-02T12:00:00Z"
11+
}

conversation/webhook.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type WebhookCreateRequest struct {
1212
URL string `json:"url"`
1313
}
1414

15+
type WebhookUpdateRequest struct {
16+
Events []WebhookEvent `json:"events,omitempty"`
17+
URL string `json:"url,omitempty"`
18+
Status WebhookStatus `json:"status,omitempty"`
19+
}
20+
1521
// CreateWebhook registers a webhook that is invoked when something interesting
1622
// happens.
1723
func CreateWebhook(c *messagebird.Client, req *WebhookCreateRequest) (*Webhook, error) {
@@ -50,3 +56,14 @@ func ReadWebhook(c *messagebird.Client, id string) (*Webhook, error) {
5056

5157
return webhook, nil
5258
}
59+
60+
// UpdateWebhook updates a single webhook based on its ID with any values set in WebhookUpdateRequest.
61+
// Do not set any values that should not be updated.
62+
func UpdateWebhook(c *messagebird.Client, id string, req *WebhookUpdateRequest) (*Webhook, error) {
63+
webhook := &Webhook{}
64+
if err := request(c, webhook, http.MethodPatch, webhooksPath+"/"+id, req); err != nil {
65+
return nil, err
66+
}
67+
68+
return webhook, nil
69+
}

conversation/webhook_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,36 @@ func TestReadWebhook(t *testing.T) {
8585

8686
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/webhooks/whid")
8787
}
88+
89+
func TestUpdateWebhook(t *testing.T) {
90+
mbtest.WillReturnTestdata(t, "webhookUpdatedObject.json", http.StatusOK)
91+
client := mbtest.Client(t)
92+
93+
webhookUpdateRequest := &WebhookUpdateRequest{
94+
Events: []WebhookEvent{
95+
WebhookEventConversationUpdated,
96+
},
97+
URL: "https://example.com/mynewwebhookurl",
98+
Status: WebhookStatusDisabled,
99+
}
100+
101+
webhook, err := UpdateWebhook(client, "whid", webhookUpdateRequest)
102+
if err != nil {
103+
t.Fatalf("unexpected error updating Webhook: %s", err)
104+
}
105+
106+
if webhook.URL != "https://example.com/mynewwebhookurl" {
107+
t.Fatalf("Expected https://example.com/mynewwebhookurl, got %s", webhook.URL)
108+
}
109+
110+
if webhook.UpdatedDatetime == nil {
111+
t.Fatalf("Expected the UpdatedDatetime value to be added, but was nil")
112+
}
113+
114+
if webhook.Status != WebhookStatusDisabled {
115+
t.Fatalf("Expected status to be disabled, was %s", webhook.Status)
116+
}
117+
118+
mbtest.AssertEndpointCalled(t, http.MethodPatch, "/v1/webhooks/whid")
119+
mbtest.AssertTestdata(t, "webhookUpdateRequest.json", mbtest.Request.Body)
120+
}

0 commit comments

Comments
 (0)