Skip to content

Commit

Permalink
feat: add support and example for secure webhook feature (#389)
Browse files Browse the repository at this point in the history
* eistats-1123: added support and example for secure webhook feature

* Updated securewebhook struct to have bool-pointer + added unittests

* created separate example file + added README

* updated link in README + updated comments in the example code

* removed return statement from SetEnable()
  • Loading branch information
brpat07 authored May 4, 2020
1 parent 49500d8 commit 9c30ae7
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 22 deletions.
57 changes: 57 additions & 0 deletions examples/securewebhook/securewebhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"log"
"os"

"github.com/sendgrid/rest"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/securewebhook"
)

// EnableSecureWebhook : Enables Signed Event Webhook.
// PATCH /user/webhooks/event/settings/signed
func EnableSecureWebhook() {
var err error
apiKey := os.Getenv("SENDGRID_API_KEY")
host := "https://api.sendgrid.com"
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/event/settings/signed", host)
request.Method = rest.Patch
s := securewebhook.NewSettings()
s.SetEnable(true)
request.Body, err = securewebhook.GetRequestBody(s)
if err != nil {
log.Println(err)
return
}
response, err := sendgrid.MakeRequest(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}

// GetPublicKeyForWebhook : Get Public Key for Event Webhook.
// Get /user/webhooks/event/settings/signed
func GetPublicKeyForWebhook() {
apiKey := os.Getenv("SENDGRID_API_KEY")
host := "https://api.sendgrid.com"
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/event/settings/signed", host)
request.Method = rest.Get
response, err := sendgrid.MakeRequest(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}

func main() {
// add your function calls here
}
44 changes: 22 additions & 22 deletions examples/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Updateyourpassword() {
request := sendgrid.GetRequest(apiKey, "/v3/user/password", host)
request.Method = "PUT"
request.Body = []byte(` {
"new_password": "new_password",
"new_password": "new_password",
"old_password": "old_password"
}`)
response, err := sendgrid.API(request)
Expand All @@ -108,8 +108,8 @@ func Updateausersprofile() {
request := sendgrid.GetRequest(apiKey, "/v3/user/profile", host)
request.Method = "PATCH"
request.Body = []byte(` {
"city": "Orange",
"first_name": "Example",
"city": "Orange",
"first_name": "Example",
"last_name": "User"
}`)
response, err := sendgrid.API(request)
Expand Down Expand Up @@ -147,7 +147,7 @@ func Cancelorpauseascheduledsend() {
request := sendgrid.GetRequest(apiKey, "/v3/user/scheduled_sends", host)
request.Method = "POST"
request.Body = []byte(` {
"batch_id": "YOUR_BATCH_ID",
"batch_id": "YOUR_BATCH_ID",
"status": "pause"
}`)
response, err := sendgrid.API(request)
Expand Down Expand Up @@ -239,7 +239,7 @@ func UpdateEnforcedTLSsettings() {
request := sendgrid.GetRequest(apiKey, "/v3/user/settings/enforced_tls", host)
request.Method = "PATCH"
request.Body = []byte(` {
"require_tls": true,
"require_tls": true,
"require_valid_cert": false
}`)
response, err := sendgrid.API(request)
Expand Down Expand Up @@ -314,18 +314,18 @@ func UpdateEventNotificationSettings() {
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/event/settings", host)
request.Method = "PATCH"
request.Body = []byte(` {
"bounce": true,
"click": true,
"deferred": true,
"delivered": true,
"dropped": true,
"enabled": true,
"group_resubscribe": true,
"group_unsubscribe": true,
"open": true,
"processed": true,
"spam_report": true,
"unsubscribe": true,
"bounce": true,
"click": true,
"deferred": true,
"delivered": true,
"dropped": true,
"enabled": true,
"group_resubscribe": true,
"group_unsubscribe": true,
"open": true,
"processed": true,
"spam_report": true,
"unsubscribe": true,
"url": "url"
}`)
response, err := sendgrid.API(request)
Expand Down Expand Up @@ -383,9 +383,9 @@ func Createaparsesetting() {
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/parse/settings", host)
request.Method = "POST"
request.Body = []byte(` {
"hostname": "myhostname.com",
"send_raw": false,
"spam_check": true,
"hostname": "myhostname.com",
"send_raw": false,
"spam_check": true,
"url": "http://email.myhosthame.com"
}`)
response, err := sendgrid.API(request)
Expand Down Expand Up @@ -423,8 +423,8 @@ func Updateaparsesetting() {
request := sendgrid.GetRequest(apiKey, "/v3/user/webhooks/parse/settings/{hostname}", host)
request.Method = "PATCH"
request.Body = []byte(` {
"send_raw": true,
"spam_check": false,
"send_raw": true,
"spam_check": false,
"url": "http://newdomain.com/parse"
}`)
response, err := sendgrid.API(request)
Expand Down
30 changes: 30 additions & 0 deletions helpers/securewebhook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**This helper allows you to quickly and easily enable/disable SecureWebhook feature or get the public key through Twilio SendGrid.**

## Dependencies

- [rest](https://github.com/sendgrid/rest)

# Quick Start

Run the [example](https://github.com/sendgrid/sendgrid-go/blob/master/examples/securewebhook/securewebhook.go) (make sure you have set your environment variable to include your SENDGRID_API_KEY).
```bash
go run examples/securewebhook/securewebhook.go
```

## Usage

- See the [example](https://github.com/sendgrid/sendgrid-go/blob/master/examples/securewebhook/securewebhook.go) for a complete working example.
- [Documentation](https://sendgrid.com/docs/for-developers/tracking-events/)

## Test

```bash
go test ./... -v
```

or

```bash
cd helpers/securewebhook
go test -v
```
29 changes: 29 additions & 0 deletions helpers/securewebhook/securewebhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package securewebhook

import (
"encoding/json"
)

// Settings ...
type Settings struct {
Enable *bool `json:"enabled,omitempty"`
}

// NewSettings ...
func NewSettings() *Settings {
return &Settings{}
}

// SetEnable ...
func (s *Settings) SetEnable(enable bool) {
s.Enable = &enable
}

// GetRequestBody ...
func GetRequestBody(s *Settings) ([]byte, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
return b, nil
}
42 changes: 42 additions & 0 deletions helpers/securewebhook/securewebhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package securewebhook

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSecureWebhookNewSettings(t *testing.T) {
assert.NotNil(t, NewSettings(), "NewSettings() shouldn't return nil")
}

func TestSecureWebhookSetEnable(t *testing.T) {
s := NewSettings()
assert.NotNil(t, NewSettings(), "NewSettings() shouldn't return nil")

s.SetEnable(true)
assert.Equal(t, true, *s.Enable, fmt.Sprintf("SecureWebhook.Enable should be 'true', got %v", *s.Enable))

s.SetEnable(false)
assert.Equal(t, false, *s.Enable, fmt.Sprintf("SecureWebhook.Enable should be 'false', got %v", *s.Enable))
}

func TestSecureWebhookGetRequestBody(t *testing.T) {
expectedJSONEnabled := []byte(`{"enabled":true}`)
expectedJSONDisabled := []byte(`{"enabled":false}`)

s := NewSettings()
assert.NotNil(t, NewSettings(), "NewSettings() shouldn't return nil")

s.SetEnable(false)
actualJSON, err := GetRequestBody(s)
require.NoError(t, err)
assert.Equal(t, expectedJSONDisabled, actualJSON, fmt.Sprintf("SecureWebhook.Enable should be '%b', got %b", expectedJSONDisabled, actualJSON))

s.SetEnable(true)
actualJSON, err = GetRequestBody(s)
require.NoError(t, err)
assert.Equal(t, expectedJSONEnabled, actualJSON, fmt.Sprintf("SecureWebhook.Enable should be '%b', got %b", expectedJSONEnabled, actualJSON))
}

0 comments on commit 9c30ae7

Please sign in to comment.