-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added data residency for eu and global regions #469
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
464307b
feat: added data residency for eu and global regions
tiwarishubham635 f632e61
feat: added data residency for eu and global regions
tiwarishubham635 3bf0e0f
Merge remote-tracking branch 'origin/set_data_residency' into set_dat…
tiwarishubham635 6bf20e2
fix: added setters for host and data residency
tiwarishubham635 db85c0a
chore: corrected the naming of test cases
tiwarishubham635 bbd252b
chore: added inline documentation for SetDataResidency and SetHost
tiwarishubham635 6de1585
chore: added comment for SetDataResidency
tiwarishubham635 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"context" | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
|
@@ -20,6 +21,11 @@ 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 | ||
|
@@ -55,6 +61,50 @@ 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add comments to document what these function does, especially how it manipulates the |
||
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/sendgrid/rest" | ||
"github.com/sendgrid/sendgrid-go/helpers/mail" | ||
|
||
"github.com/sendgrid/sendgrid-go" | ||
) | ||
|
||
var SAMPLE_EMAIL = "[email protected]" | ||
|
||
// SetDataResidency : Set region for sendgrid. | ||
func SetDataResidencyGlobal() { | ||
message := buildHelloEmail() | ||
request, err := buildSendgridObj("global") | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
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, err := buildSendgridObj("eu") | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
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 SetDataResidencyDefault() { | ||
message := buildHelloEmail() | ||
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "") | ||
request.Method = "POST" | ||
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 buildHelloEmail() *mail.SGMailV3 { | ||
// Note that when you use this constructor an initial personalization object | ||
// is created for you. It can be accessed via | ||
// mail.personalization.get(0) as it is a List object | ||
|
||
from := mail.NewEmail("test_user", SAMPLE_EMAIL) | ||
subject := "Sending with Twilio SendGrid is Fun" | ||
to := mail.NewEmail("test_user", SAMPLE_EMAIL) | ||
plainTextContent := "and easy to do anywhere, even with Go" | ||
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>" | ||
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent) | ||
email := mail.NewEmail("test_user", SAMPLE_EMAIL) | ||
|
||
p := mail.NewPersonalization() | ||
p.AddTos(email) | ||
message.AddPersonalizations(p) | ||
|
||
return message | ||
} | ||
|
||
func buildSendgridObj(region string) (rest.Request, error) { | ||
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "") | ||
request.Method = "POST" | ||
request, err := sendgrid.SetDataResidency(request, region) | ||
if err != nil { | ||
return request, err | ||
} | ||
return request, nil | ||
} | ||
|
||
func main() { | ||
// add your function calls here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public functions should be documented