Skip to content

Commit

Permalink
chore: removed SetHost and shifted SetDataResidency to sendgrid.go (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 authored and qhenkart committed Mar 15, 2024
1 parent 489a749 commit 4a92285
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 59 deletions.
50 changes: 0 additions & 50 deletions base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"time"

Expand All @@ -21,11 +20,6 @@ const (
rateLimitSleep = 1100
)

var allowedRegionsHostMap = map[string]string{
"eu": "https://api.eu.sendgrid.com",
"global": "https://api.sendgrid.com",
}

type options struct {
Auth string
Endpoint string
Expand Down Expand Up @@ -61,50 +55,6 @@ func requestNew(options options) rest.Request {
}
}

// extractEndpoint extracts the endpoint from a baseURL
func extractEndpoint(link string) (string, error) {
parsedURL, err := url.Parse(link)
if err != nil {
return "", err
}

return parsedURL.Path, nil
}

// SetHost changes the baseURL of the request with the host passed
/*
* This allows support for global and eu regions only. This set will likely expand in the future.
* Global should be the default
* Global region means the message should be sent through:
* HTTP: api.sendgrid.com
* EU region means the message should be sent through:
* HTTP: api.eu.sendgrid.com
*/
// @return [Request] the modified request object
func SetHost(request rest.Request, host string) (rest.Request, error) {
endpoint, err := extractEndpoint(request.BaseURL)
if err != nil {
return request, err
}

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

// SetDataResidency modifies the host as per the region
// @return [Request] the modified request object
func SetDataResidency(request rest.Request, region string) (rest.Request, error) {
regionalHost, present := allowedRegionsHostMap[region]
if !present {
return request, errors.New("error: region can only be \"eu\" or \"global\"")
}
request, err := SetHost(request, regionalHost)
if err != nil {
return request, err
}
return request, nil
}

// Send sends an email through Twilio SendGrid
func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
return cl.SendWithContext(context.Background(), email)
Expand Down
41 changes: 41 additions & 0 deletions sendgrid.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package sendgrid

import (
"errors"
"github.com/sendgrid/rest"
"net/url"
)

// sendGridOptions for CreateRequest
Expand All @@ -12,6 +14,12 @@ type sendGridOptions struct {
Subuser string
}

// sendgrid host map for different regions
var allowedRegionsHostMap = map[string]string{
"eu": "https://api.eu.sendgrid.com",
"global": "https://api.sendgrid.com",
}

// GetRequest
// @return [Request] a default request object
func GetRequest(key, endpoint, host string) rest.Request {
Expand Down Expand Up @@ -47,3 +55,36 @@ func NewSendClient(key string) *Client {
request.Method = "POST"
return &Client{request}
}

// extractEndpoint extracts the endpoint from a baseURL
func extractEndpoint(link string) (string, error) {
parsedURL, err := url.Parse(link)
if err != nil {
return "", err
}

return parsedURL.Path, nil
}

// SetDataResidency modifies the host as per the region
/*
* This allows support for global and eu regions only. This set will likely expand in the future.
* Global should be the default
* Global region means the message should be sent through:
* HTTP: api.sendgrid.com
* EU region means the message should be sent through:
* HTTP: api.eu.sendgrid.com
*/
// @return [Request] the modified request object
func SetDataResidency(request rest.Request, region string) (rest.Request, error) {
regionalHost, present := allowedRegionsHostMap[region]
if !present {
return request, errors.New("error: region can only be \"eu\" or \"global\"")
}
endpoint, err := extractEndpoint(request.BaseURL)
if err != nil {
return request, err
}
request.BaseURL = regionalHost + endpoint
return request, nil
}
9 changes: 0 additions & 9 deletions sendgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ func TestSetDataResidencyOverrideHost(t *testing.T) {
assert.Equal(t, "https://api.eu.sendgrid.com", request.BaseURL, "Host not correct as per the region")
}

func TestSetDataResidencyOverrideDataResidency(t *testing.T) {
request := GetRequest("API_KEY", "", "")
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 TestSetDataResidencyIncorrectRegion(t *testing.T) {
request := GetRequest("API_KEY", "", "")
_, err := SetDataResidency(request, "foo")
Expand Down

0 comments on commit 4a92285

Please sign in to comment.