From ec79368cd5a8488bd14506812b1fa44dd65cf7e3 Mon Sep 17 00:00:00 2001 From: Shubham Date: Thu, 23 Nov 2023 14:26:36 +0530 Subject: [PATCH] chore: removed SetHost and shifted SetDataResidency to sendgrid.go (#470) --- base_interface.go | 50 ----------------------------------------------- sendgrid.go | 41 ++++++++++++++++++++++++++++++++++++++ sendgrid_test.go | 9 --------- 3 files changed, 41 insertions(+), 59 deletions(-) diff --git a/base_interface.go b/base_interface.go index 2f4e38c7..ae826811 100644 --- a/base_interface.go +++ b/base_interface.go @@ -6,7 +6,6 @@ import ( "context" "errors" "net/http" - "net/url" "strconv" "time" @@ -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 @@ -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) diff --git a/sendgrid.go b/sendgrid.go index 7192bec2..1c50451c 100644 --- a/sendgrid.go +++ b/sendgrid.go @@ -1,7 +1,9 @@ package sendgrid import ( + "errors" "github.com/sendgrid/rest" + "net/url" ) // sendGridOptions for CreateRequest @@ -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 { @@ -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 +} diff --git a/sendgrid_test.go b/sendgrid_test.go index 365d46e5..57b52709 100644 --- a/sendgrid_test.go +++ b/sendgrid_test.go @@ -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")