Skip to content
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 7 commits into from
Nov 21, 2023

Conversation

tiwarishubham635
Copy link
Contributor

@tiwarishubham635 tiwarishubham635 commented Nov 15, 2023

Fixes

Added region field in sendgrid options. The default region is "global" and the default host is "api.sendgrid.com". When the region is set to "eu", the host should change to "api.eu.sendgrid.com".

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

If you have questions, please file a support ticket.

sendgrid.go Outdated Show resolved Hide resolved
sendgrid_test.go Outdated Show resolved Hide resolved
sendgrid_test.go Outdated Show resolved Hide resolved
func setDataResidency(options options) (string, error) {
currentHost := options.Host
defaultHost := allowedRegionsHostMap["global"]
if currentHost != defaultHost { // for testing, the hostname can be different

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean hostname can be different? For example if its api.sendgrid.com by default and I am calling setDataResidency('eu'), shouldn't the hostname change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test cases, we set the URL of the fakeServer as the hostname (something like 127.0.0.1:5000) In that case if we return the default host (api.sendgrid.com) the test would fail. That's why I added this check.

// SetDataResidency : Set region for sendgrid.
func SetDataResidencyGlobal() {
message := buildHelloEmail()
request := buildSendgridObj("global")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call setDataResidency() method on the request object too? Right now, I guess the only way to set it is via the constructor, no?

@manisha1997
Copy link

LGTM

Copy link

@gladysmae08 gladysmae08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR

@@ -55,6 +61,38 @@ func requestNew(options options) rest.Request {
}
}

func ExtractEndpoint(link string) (string, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any reason to export this outside of the sendgrid package? If not, we should make it private

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

caling this exportPath seems more appropriate too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we do not need to export it outside, making it private.


func SetDataResidency(request rest.Request, region string) (rest.Request, error) {
regionalHost, isPresent := allowedRegionsHostMap[region]
if isPresent {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's generally better to wrap the !ok in the if block since it's a shorter path. You'll be able to avoid the nested if blocks and makes the code more readable. So something like

regionalHost, ok := allowedRegionsHostMap[region]
if !ok {
  return error
}

do other stuff here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense. Will correct it

sendgrid_test.go Outdated
@@ -81,6 +81,53 @@ func TestGetRequestSubuser(t *testing.T) {
ShouldHaveHeaders(&request, t)
}

func Test_test_set_residency_eu(t *testing.T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should try to follow the Go convention of naming test functions and proper casing. You can refer to the other existing tests for example, but generally, it should be Test<FunctionNameBeingTested>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, will correct the casing and naming of the test cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for different scenarios in a single function to be tested, we can add them with an underscore like the test cases like TestRequestRetry_rateLimit and TestRequestRetry_rateLimit_noHeader.
Similarly, I think we can also have test cases named - TestSetDataResidency_eu and TestSetDataResidency_global. Will that be okay?

Copy link

@gladysmae08 gladysmae08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR 2

return request, nil
}

func SetDataResidency(request rest.Request, region string) (rest.Request, error) {

Choose a reason for hiding this comment

The 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 host field

return parsedURL.Path, nil
}

func SetHost(request rest.Request, host string) (rest.Request, error) {

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

@tiwarishubham635 tiwarishubham635 merged commit e7f240a into main Nov 21, 2023
18 checks passed
@tiwarishubham635 tiwarishubham635 deleted the set_data_residency branch November 21, 2023 05:56
qhenkart pushed a commit to qhenkart/sendgrid-go that referenced this pull request Mar 15, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants