Skip to content

feat: added data residency for eu and global regions - #469

Merged
tiwarishubham635 merged 7 commits into
mainfrom
set_data_residency
Nov 21, 2023
Merged

tiwarishubham635 merged 7 commits into
mainfrom
set_data_residency

Conversation

@tiwarishubham635

@tiwarishubham635 tiwarishubham635 commented Nov 15, 2023 •

Copy link
Copy Markdown
Contributor

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.

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

Copy link
Copy Markdown

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
Copy Markdown
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.

Comment thread examples/dataresidency/setRegion.go Outdated
// SetDataResidency : Set region for sendgrid.
func SetDataResidencyGlobal() {
message := buildHelloEmail()
request := buildSendgridObj("global")

Copy link
Copy Markdown

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
Copy Markdown

LGTM

@gladysmae08 gladysmae08 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CR

Comment thread base_interface.go Outdated
}
}

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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
Copy Markdown
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.

Comment thread base_interface.go Outdated

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

Copy link
Copy Markdown

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
Copy Markdown
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

Comment thread sendgrid_test.go Outdated
ShouldHaveHeaders(&request, t)
}

func Test_test_set_residency_eu(t *testing.T) {

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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?

@gladysmae08 gladysmae08 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CR 2

Comment thread base_interface.go
return request, nil
}

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

Copy link
Copy Markdown

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

Comment thread base_interface.go
return parsedURL.Path, nil
}

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

Copy link
Copy Markdown

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
@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

⌂ 换网址