Skip to content

Commit

Permalink
fix: added setters for host and data residency
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 committed Nov 17, 2023
1 parent 3bf0e0f commit 6bf20e2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 77 deletions.
52 changes: 28 additions & 24 deletions base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"compress/gzip"
"context"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"time"

Expand All @@ -32,7 +31,6 @@ type options struct {
Endpoint string
Host string
Subuser string
Region string
}

// Client is the Twilio SendGrid Go client
Expand All @@ -57,36 +55,42 @@ func requestNew(options options) rest.Request {
requestHeaders["On-Behalf-Of"] = options.Subuser
}

host, err := setDataResidency(options)
if err == nil {
options.Host = host
} else {
fmt.Println(err)
log.Println(err)
}

return rest.Request{
BaseURL: options.baseURL(),
Headers: requestHeaders,
}
}

func setDataResidency(options options) (string, error) {
currentHost := options.Host
defaultHost := allowedRegionsHostMap["global"]
if currentHost != defaultHost { // for testing, the hostname can be different
return currentHost, nil
func ExtractEndpoint(link string) (string, error) {
parsedURL, err := url.Parse(link)
if err != nil {
return "", err
}

return parsedURL.Path, nil
}

func SetHost(request rest.Request, host string) (rest.Request, error) {
endpoint, err := ExtractEndpoint(request.BaseURL)
if err != nil {
return request, err
}
region := options.Region
if region != "" {
regionalHost, isPresent := allowedRegionsHostMap[region]
if isPresent {
return regionalHost, nil
} else {
return defaultHost, errors.New("error: region can only be \"eu\" or \"global\"")

request.BaseURL = host + endpoint
return request, nil
}

func SetDataResidency(request rest.Request, region string) (rest.Request, error) {
regionalHost, isPresent := allowedRegionsHostMap[region]
if isPresent {
request, err := SetHost(request, regionalHost)
if err != nil {
return request, err
}
return request, nil
}
return defaultHost, nil

return request, errors.New("error: region can only be \"eu\" or \"global\"")
}

// Send sends an email through Twilio SendGrid
Expand Down
47 changes: 30 additions & 17 deletions examples/dataresidency/setRegion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"github.com/sendgrid/rest"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"log"
"os"

"github.com/sendgrid/rest"
"github.com/sendgrid/sendgrid-go/helpers/mail"

"github.com/sendgrid/sendgrid-go"
)

Expand All @@ -15,29 +16,37 @@ var SAMPLE_EMAIL = "[email protected]"
// SetDataResidency : Set region for sendgrid.
func SetDataResidencyGlobal() {
message := buildHelloEmail()
request := buildSendgridObj("global")
request.Body = mail.GetRequestBody(message)
response, err := sendgrid.API(request)
request, err := buildSendgridObj("global")
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
request.Body = mail.GetRequestBody(message)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
}

func SetDataResidencyEu() {
message := buildHelloEmail()
request := buildSendgridObj("eu")
request.Body = mail.GetRequestBody(message)
response, err := sendgrid.API(request)
request, err := buildSendgridObj("eu")
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
request.Body = mail.GetRequestBody(message)
response, err := sendgrid.API(request)
if err != nil {
log.Println(err)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.Body)
fmt.Println(response.Headers)
}
}
}

Expand Down Expand Up @@ -76,10 +85,14 @@ func buildHelloEmail() *mail.SGMailV3 {
return message
}

func buildSendgridObj(region string) rest.Request {
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "", region)
func buildSendgridObj(region string) (rest.Request, error) {
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "")
request.Method = "POST"
return request
request, err := sendgrid.SetDataResidency(request, region)
if err != nil {
return request, err
}
return request, nil
}

func main() {
Expand Down
26 changes: 6 additions & 20 deletions sendgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,18 @@ type sendGridOptions struct {
Endpoint string
Host string
Subuser string
Region string
}

// GetRequest
// @return [Request] a default request object
func GetRequest(key, endpoint, host string, regionOptional ...string) rest.Request {
region := ""
if len(regionOptional) > 0 {
region = regionOptional[0]
}
return createSendGridRequest(sendGridOptions{key, endpoint, host, "", region})
func GetRequest(key, endpoint, host string) rest.Request {
return createSendGridRequest(sendGridOptions{key, endpoint, host, ""})
}

// GetRequestSubuser like GetRequest but with On-Behalf of Subuser
// @return [Request] a default request object
func GetRequestSubuser(key, endpoint, host, subuser string, regionOptional ...string) rest.Request {
region := ""
if len(regionOptional) > 0 {
region = regionOptional[0]
}
return createSendGridRequest(sendGridOptions{key, endpoint, host, subuser, region})
func GetRequestSubuser(key, endpoint, host, subuser string) rest.Request {
return createSendGridRequest(sendGridOptions{key, endpoint, host, subuser})
}

// createSendGridRequest create Request
Expand All @@ -41,7 +32,6 @@ func createSendGridRequest(sgOptions sendGridOptions) rest.Request {
sgOptions.Endpoint,
sgOptions.Host,
sgOptions.Subuser,
sgOptions.Region,
}

if options.Host == "" {
Expand All @@ -52,12 +42,8 @@ func createSendGridRequest(sgOptions sendGridOptions) rest.Request {
}

// NewSendClient constructs a new Twilio SendGrid client given an API key
func NewSendClient(key string, regionOptional ...string) *Client {
region := ""
if len(regionOptional) > 0 {
region = regionOptional[0]
}
request := GetRequest(key, "/v3/mail/send", "", region)
func NewSendClient(key string) *Client {
request := GetRequest(key, "/v3/mail/send", "")
request.Method = "POST"
return &Client{request}
}
42 changes: 26 additions & 16 deletions sendgrid_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package sendgrid

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -84,37 +82,49 @@ func TestGetRequestSubuser(t *testing.T) {
}

func Test_test_set_residency_eu(t *testing.T) {
request := GetRequest("API_KEY", "", "", "eu")
request := GetRequest("API_KEY", "", "")
request, err := SetDataResidency(request, "eu")
assert.Nil(t, err)
assert.Equal(t, "https://api.eu.sendgrid.com", request.BaseURL, "Host not correct as per the region")
}

func Test_test_set_residency_global(t *testing.T) {
request := GetRequest("API_KEY", "", "https://api.sendgrid.com", "global")
request := GetRequest("API_KEY", "", "https://api.sendgrid.com")
request, err := SetDataResidency(request, "global")
assert.Nil(t, err)
assert.Equal(t, "https://api.sendgrid.com", request.BaseURL, "Host not correct as per the region")
}

func Test_test_set_residency_override_host(t *testing.T) {
request := GetRequest("API_KEY", "", "https://api.sendgrid.com", "eu")
request := GetRequest("API_KEY", "", "https://test.api.com")
request, err := SetDataResidency(request, "eu")
assert.Nil(t, err)
assert.Equal(t, "https://api.eu.sendgrid.com", request.BaseURL, "Host not correct as per the region")
}

func Test_test_set_residency_default(t *testing.T) {
func Test_test_set_residency_override_data_residency(t *testing.T) {
request := GetRequest("API_KEY", "", "")
assert.Equal(t, "https://api.sendgrid.com", request.BaseURL, "Host not correct as per the region")
request, err := SetDataResidency(request, "eu")
assert.Nil(t, err)
request, err = SetHost(request, "https://test.api.com")
assert.Nil(t, err)
assert.Equal(t, "https://test.api.com", request.BaseURL, "Host not correct as per the region")
}

func Test_test_set_residency_incorrect_region(t *testing.T) {
var buffer bytes.Buffer
log.SetOutput(&buffer)
request := GetRequest("API_KEY", "", "", "foo")
request := GetRequest("API_KEY", "", "")
_, err := SetDataResidency(request, "foo")
assert.NotNil(t, err, "error: region can only be \"eu\" or \"global\"")
}

log.SetOutput(new(bytes.Buffer))
capturedOutput := buffer.String()
func Test_test_set_residency_null_region(t *testing.T) {
request := GetRequest("API_KEY", "", "")
_, err := SetDataResidency(request, "")
assert.NotNil(t, err, "error: region can only be \"eu\" or \"global\"")
}

expectedErrorMessage := "error: region can only be \"eu\" or \"global\""
if !strings.Contains(capturedOutput, expectedErrorMessage) {
t.Errorf("Expected error message '%s' not found on setting invalid region", expectedErrorMessage)
}
func Test_test_set_residency_default_region(t *testing.T) {
request := GetRequest("API_KEY", "", "")
assert.Equal(t, "https://api.sendgrid.com", request.BaseURL, "Host not correct as per the region")
}

Expand Down

0 comments on commit 6bf20e2

Please sign in to comment.