forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added data residency for eu and global regions (sendgrid#469)
* feat: added data residency for eu and global regions * feat: added data residency for eu and global regions * fix: added setters for host and data residency * chore: corrected the naming of test cases * chore: added inline documentation for SetDataResidency and SetHost * chore: added comment for SetDataResidency
- Loading branch information
1 parent
748756d
commit 489a749
Showing
3 changed files
with
197 additions
and
0 deletions.
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
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